Skip to content

Commit

Permalink
サムネイル情報を一度で取得
Browse files Browse the repository at this point in the history
  • Loading branch information
azbcww committed Oct 4, 2023
1 parent bf16962 commit 59201bd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
36 changes: 36 additions & 0 deletions repository/gorm/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,39 @@ func (r *stampRepository) GetStampStats(stampID uuid.UUID) (*repository.StampSta
}
return &stats, nil
}

func (r *stampRepository) StampThumbnailExists(stamps []*model.Stamp) (stampsWithThumb []repository.StampWithThumbnail, err error) {
IDs := make([]uuid.UUID, len(stamps))

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

for _, s := range(stamps) {
_, ok := tm[s.FileID]
stampsWithThumb = append(stampsWithThumb, repository.StampWithThumbnail{
ID: s.ID,
Name: s.Name,
CreatorID: s.CreatorID,
FileID: s.FileID,
IsUnicode: s.IsUnicode,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
HasThumbnail: ok,
})
}
return
}
16 changes: 16 additions & 0 deletions repository/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ type StampStats struct {
TotalCount int64 `json:"totalCount"`
}

type StampWithThumbnail struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
CreatorID uuid.UUID `json:"creatorId"`
FileID uuid.UUID `json:"fileId"`
IsUnicode bool `json:"isUnicode"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
HasThumbnail bool `json:"hasThumbnail"`
}

// StampType スタンプの種類
type StampType string

Expand Down Expand Up @@ -114,4 +125,9 @@ type StampRepository interface {
// stampIDにNILを渡した場合、(nil, ErrNilID)を返します。
// DBによるエラーを返すことがあります。
GetStampStats(stampID uuid.UUID) (*StampStats, error)
// StampThumbnailExists スタンプ情報にサムネイルの有無を示すbool値を付加した構造体の配列を返します
//
// 成功した場合、当該配列とnilを返します
// DBによるエラーを返すことがあります。
StampThumbnailExists(stamps []*model.Stamp) (stampsWithThumb []StampWithThumbnail, err error)
}
7 changes: 6 additions & 1 deletion router/v3/stamps.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ func (h *Handlers) GetStamps(c echo.Context) error {
return herror.InternalServerError(err)
}

return extension.ServeJSONWithETag(c, stamps)
stampsWithThumb, err := h.Repo.StampThumbnailExists(stamps)
if err != nil {
return herror.InternalServerError(err)
}

return extension.ServeJSONWithETag(c, stampsWithThumb)
}

// CreateStamp POST /stamps
Expand Down

0 comments on commit 59201bd

Please sign in to comment.