From 59201bdcf64f932f6c4bb9bd4e517f5d96a5afac Mon Sep 17 00:00:00 2001 From: azbcww <1121hatsuyuki@gmail.com> Date: Wed, 4 Oct 2023 16:13:06 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B5=E3=83=A0=E3=83=8D=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E6=83=85=E5=A0=B1=E3=82=92=E4=B8=80=E5=BA=A6=E3=81=A7=E5=8F=96?= =?UTF-8?q?=E5=BE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- repository/gorm/stamp.go | 36 ++++++++++++++++++++++++++++++++++++ repository/stamp.go | 16 ++++++++++++++++ router/v3/stamps.go | 7 ++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/repository/gorm/stamp.go b/repository/gorm/stamp.go index 78d2fa20d..94bb9a67d 100644 --- a/repository/gorm/stamp.go +++ b/repository/gorm/stamp.go @@ -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 +} diff --git a/repository/stamp.go b/repository/stamp.go index d9188a9fb..3564e926a 100644 --- a/repository/stamp.go +++ b/repository/stamp.go @@ -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 @@ -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) } diff --git a/router/v3/stamps.go b/router/v3/stamps.go index 6f2991ee0..966d627b4 100644 --- a/router/v3/stamps.go +++ b/router/v3/stamps.go @@ -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