Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

レスポンスにサムネイル情報追加 #1929

Merged
merged 25 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e4494ee
レスポンスにサムネイル情報追加
azbcww Aug 13, 2023
bf16962
Revert "レスポンスにサムネイル情報追加"
azbcww Oct 2, 2023
59201bd
サムネイル情報を一度で取得
azbcww Oct 4, 2023
6407e0e
fix error
azbcww Oct 4, 2023
0c87c4a
Delete tree.txt
azbcww Oct 4, 2023
6fde0ca
StampWithThumbnail修正
azbcww Oct 18, 2023
e3d6c7c
test追加
azbcww Oct 18, 2023
ad78889
fix lint and change HasThumbnail to hasThumbnail
azbcww Oct 18, 2023
9efe74c
fix test
azbcww Oct 18, 2023
d87efdd
Revert "fix test"
azbcww Nov 27, 2023
f35c172
Revert "fix lint and change HasThumbnail to hasThumbnail"
azbcww Nov 27, 2023
5cff7cb
Revert "test追加"
azbcww Nov 27, 2023
d2b5d5c
Revert "StampWithThumbnail修正"
azbcww Nov 27, 2023
fd669b5
Revert "Delete tree.txt"
azbcww Nov 27, 2023
d65701a
Revert "fix error"
azbcww Nov 27, 2023
93faae0
Revert "サムネイル情報を一度で取得"
azbcww Nov 27, 2023
9069946
Revert "Revert "レスポンスにサムネイル情報追加""
azbcww Nov 27, 2023
96ed273
Revert "レスポンスにサムネイル情報追加"
azbcww Nov 27, 2023
634342d
convert GetAllStamps into GetAllStampsWithThumbnail
azbcww Nov 27, 2023
c70583c
put StampThumbnailExists into loadFilteredStamps
azbcww Nov 27, 2023
b2d6939
use make([]uuid.UUID, 0, len(stamps)) to define ts
azbcww Nov 27, 2023
b7a434d
correction of the part pointed out
azbcww Nov 28, 2023
c84387f
add test
azbcww Dec 1, 2023
d62fa79
edit swagger and fix nesting depth
azbcww Dec 3, 2023
47df0e8
edit OpenAPI schema
azbcww Dec 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/v3-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4765,6 +4765,9 @@ components:
isUnicode:
type: boolean
description: Unicode絵文字か
hasThumbnail:
type: boolean
description: サムネイルの有無
logica0419 marked this conversation as resolved.
Show resolved Hide resolved
required:
- id
- name
Expand All @@ -4773,6 +4776,7 @@ components:
- updatedAt
- fileId
- isUnicode
- hasThumbnail
PostStampRequest:
title: PostStampRequest
type: object
Expand Down
6 changes: 6 additions & 0 deletions model/stamps.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type Stamp struct {
File *FileMeta `gorm:"constraint:stamps_file_id_files_id_foreign,OnUpdate:CASCADE,OnDelete:NO ACTION;foreignKey:FileID" json:"-"`
}

// StampWithThumbnail サムネイル情報を付与したスタンプ構造体
type StampWithThumbnail struct {
*Stamp
HasThumbnail bool `json:"hasThumbnail"`
}

// TableName スタンプテーブル名を取得します
func (*Stamp) TableName() string {
return "stamps"
Expand Down
49 changes: 39 additions & 10 deletions repository/gorm/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type stampRepository struct {
db *gorm.DB
hub *hub.Hub
stamps *sc.Cache[struct{}, map[uuid.UUID]*model.Stamp]
perType *sc.Cache[repository.StampType, []*model.Stamp]
perType *sc.Cache[repository.StampType, []*model.StampWithThumbnail]
}

func makeStampRepository(db *gorm.DB, hub *hub.Hub) *stampRepository {
Expand All @@ -48,26 +48,55 @@ func (r *stampRepository) loadStamps(_ context.Context, _ struct{}) (map[uuid.UU
return stampsMap, nil
}

func (r *stampRepository) loadFilteredStamps(ctx context.Context, stampType repository.StampType) ([]*model.Stamp, error) {
func (r *stampRepository) loadFilteredStamps(ctx context.Context, stampType repository.StampType) ([]*model.StampWithThumbnail, error) {
stamps, err := r.stamps.Get(ctx, struct{}{})
if err != nil {
return nil, err
}
arr := make([]*model.Stamp, 0, len(stamps))
arr := make([]*model.StampWithThumbnail, 0, len(stamps))

IDs := make([]uuid.UUID, 0, len(stamps))
stampsWithThumbnail := make([]*model.StampWithThumbnail, 0, len(stamps))
for _, stamp := range stamps {
IDs = append(IDs, stamp.FileID)
}

thumbnails := make([]uuid.UUID, 0, len(stamps))
if err := r.db.
Table("files_thumbnails").
Select("file_id").
Where("file_id IN (?)", IDs).
Find(&thumbnails).
Error; err != nil {
return nil, err
}
thumbnailExists := make(map[uuid.UUID]struct{}, len(thumbnails))
for _, v := range thumbnails {
thumbnailExists[v] = struct{}{}
}

for _, stamp := range stamps {
_, ok := thumbnailExists[stamp.FileID]
stampsWithThumbnail = append(stampsWithThumbnail, &model.StampWithThumbnail{
Stamp: stamp,
HasThumbnail: ok,
})
}

if err != nil {
return nil, err
}
switch stampType {
case repository.StampTypeAll:
for _, s := range stamps {
arr = append(arr, s)
}
arr = append(arr, stampsWithThumbnail...)
case repository.StampTypeUnicode:
for _, s := range stamps {
for _, s := range stampsWithThumbnail {
if s.IsUnicode {
arr = append(arr, s)
}
}
case repository.StampTypeOriginal:
for _, s := range stamps {
for _, s := range stampsWithThumbnail {
if !s.IsUnicode {
arr = append(arr, s)
}
Expand Down Expand Up @@ -270,8 +299,8 @@ func (r *stampRepository) DeleteStamp(id uuid.UUID) (err error) {
return repository.ErrNotFound
}

// GetAllStamps implements StampRepository interface.
func (r *stampRepository) GetAllStamps(stampType repository.StampType) (stamps []*model.Stamp, err error) {
// GetAllStampsWithThumbnail implements StampRepository interface.
func (r *stampRepository) GetAllStampsWithThumbnail(stampType repository.StampType) (stampsWithThumbnail []*model.StampWithThumbnail, err error) {
return r.perType.Get(context.Background(), stampType)
}

Expand Down
44 changes: 38 additions & 6 deletions repository/gorm/stamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,51 @@ func TestRepositoryImpl_DeleteStamp(t *testing.T) {
})
}

func TestRepositoryImpl_GetAllStamps(t *testing.T) {
func TestRepositoryImpl_GetAllStampsWithThumbnail(t *testing.T) {
t.Parallel()
repo, assert, _ := setup(t, ex1)
repo, assert, require := setup(t, ex1)

n := 10

for i := 0; i < 10; i++ {
mustMakeStamp(t, repo, rand, uuid.Nil)
}

arr, err := repo.GetAllStamps(repository.StampTypeAll)
if assert.NoError(err) {
assert.Len(arr, n)
for i := 0; i < 10; i++ {
stamp := mustMakeStamp(t, repo, rand, uuid.Nil)
err := repo.DeleteFileMeta(stamp.FileID)
require.NoError(err)
}

t.Run("without thumbnail", func(t *testing.T) {
t.Parallel()
arr, err := repo.GetAllStampsWithThumbnail(repository.StampTypeAll)
if !assert.NoError(err) {
t.FailNow()
}
assert.Len(arr, n*2)
cnt := 0
for _, s := range arr {
if !s.HasThumbnail {
cnt++
}
}
assert.Equal(n, cnt)
})
t.Run("with thumbnail", func(t *testing.T) {
t.Parallel()
arr, err := repo.GetAllStampsWithThumbnail(repository.StampTypeAll)
if !assert.NoError(err) {
t.FailNow()
}
assert.Len(arr, n*2)
cnt := 0
for _, s := range arr {
if s.HasThumbnail {
cnt++
}
}
assert.Equal(n, cnt)
})
}

func TestRepositoryImpl_StampExists(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions repository/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ type StampRepository interface {
// 引数にuuid.Nilを指定した場合、ErrNilIDを返します。
// DBによるエラーを返すことがあります。
DeleteStamp(id uuid.UUID) (err error)
// GetAllStamps 全てのスタンプを取得します
// GetAllStampsWithThumbnail 全てのスタンプとサムネイルの有無を取得します
//
// 成功した場合、スタンプのIDでソートされた配列とnilを返します。
// DBによるエラーを返すことがあります。
GetAllStamps(stampType StampType) (stamps []*model.Stamp, err error)
GetAllStampsWithThumbnail(stampType StampType) (stamps []*model.StampWithThumbnail, err error)
// StampExists 指定したIDのスタンプが存在するかどうかを返します
//
// 存在する場合、trueとnilを返します。
Expand Down
4 changes: 2 additions & 2 deletions router/v1/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (c *EmojiCache) Purge() {

func emojiJSONGenerator(repo repository.Repository) func(_ context.Context, _ struct{}) ([]byte, error) {
return func(_ context.Context, _ struct{}) ([]byte, error) {
stamps, err := repo.GetAllStamps(repository.StampTypeAll)
stamps, err := repo.GetAllStampsWithThumbnail(repository.StampTypeAll)
if err != nil {
return nil, err
}
Expand All @@ -141,7 +141,7 @@ func emojiJSONGenerator(repo repository.Repository) func(_ context.Context, _ st

func emojiCSSGenerator(repo repository.Repository) func(_ context.Context, _ struct{}) ([]byte, error) {
return func(_ context.Context, _ struct{}) ([]byte, error) {
stamps, err := repo.GetAllStamps(repository.StampTypeAll)
stamps, err := repo.GetAllStampsWithThumbnail(repository.StampTypeAll)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion router/v3/stamps.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (h *Handlers) GetStamps(c echo.Context) error {
stampType = repository.StampTypeOriginal
}

stamps, err := h.Repo.GetAllStamps(stampType)
stamps, err := h.Repo.GetAllStampsWithThumbnail(stampType)
if err != nil {
return herror.InternalServerError(err)
}
Expand Down
Loading