Skip to content

Commit

Permalink
refactor: streamline and use sqlc for media and resource media
Browse files Browse the repository at this point in the history
  • Loading branch information
litsynp committed Apr 27, 2024
1 parent 7254939 commit 5cf6695
Show file tree
Hide file tree
Showing 24 changed files with 485 additions and 384 deletions.
12 changes: 6 additions & 6 deletions cmd/server/handler/media_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ func NewMediaHandler(mediaService service.MediaService) *MediaHandler {
// @Tags media
// @Produce json
// @Param id path int true "미디어 ID"
// @Success 200 {object} media.MediaView
// @Success 200 {object} media.DetailView
// @Router /media/{id} [get]
func (h *MediaHandler) FindMediaByID(c echo.Context) error {
id, err := pnd.ParseIDFromPath(c, "id")
if err != nil {
return c.JSON(err.StatusCode, err)
}

found, err := h.mediaService.FindMediaByID(c.Request().Context(), *id)
found, err := h.mediaService.FindMediaByID(c.Request().Context(), int64(*id))
if err != nil {
return c.JSON(err.StatusCode, err)
}

return c.JSON(http.StatusOK, found.ToMediaView())
return c.JSON(http.StatusOK, found)
}

// UploadImage godoc
Expand All @@ -51,7 +51,7 @@ func (h *MediaHandler) FindMediaByID(c echo.Context) error {
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "이미지 파일"
// @Success 201 {object} media.MediaView
// @Success 201 {object} media.DetailView
// @Router /media/images [post]
func (h *MediaHandler) UploadImage(c echo.Context) error {
fileHeader, err := c.FormFile("file")
Expand Down Expand Up @@ -79,12 +79,12 @@ func (h *MediaHandler) UploadImage(c echo.Context) error {
return c.JSON(pndErr.StatusCode, pndErr)
}

res, err2 := h.mediaService.UploadMedia(c.Request().Context(), file, media.MediaTypeImage, fileHeader.Filename)
res, err2 := h.mediaService.UploadMedia(c.Request().Context(), file, media.TypeImage, fileHeader.Filename)
if err2 != nil {
return c.JSON(err2.StatusCode, err2)
}

return c.JSON(http.StatusCreated, res.ToMediaView())
return c.JSON(http.StatusCreated, res)
}

var supportedMimeTypes = []string{
Expand Down
7 changes: 7 additions & 0 deletions internal/common/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func IntToNullInt32(val int) sql.NullInt32 {
}
}

func Int64ToNullInt32(val int64) sql.NullInt32 {
return sql.NullInt32{
Int32: int32(val),
Valid: val != 0,
}
}

func IntPtrToNullInt32(val *int) sql.NullInt32 {
return sql.NullInt32{
Int32: int32(DerefOrEmpty(val)),
Expand Down
31 changes: 0 additions & 31 deletions internal/domain/media/media.go

This file was deleted.

22 changes: 22 additions & 0 deletions internal/domain/media/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package media

type Type string

const (
TypeImage Type = "image"
)

func (mt Type) String() string {
return string(mt)
}

type ViewForSOSPost struct {
ID int64 `field:"id" json:"id"`
MediaType Type `field:"media_type" json:"media_type"`
URL string `field:"url" json:"url"`
CreatedAt string `field:"created_at" json:"created_at"`
UpdatedAt string `field:"updated_at" json:"updated_at"`
DeletedAt string `field:"deleted_at" json:"deleted_at"`
}

type ViewListForSOSPost []*ViewForSOSPost
35 changes: 0 additions & 35 deletions internal/domain/media/resource_media.go

This file was deleted.

80 changes: 63 additions & 17 deletions internal/domain/media/view.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,80 @@
package media

type MediaView struct {
ID int `json:"id"`
MediaType MediaType `json:"mediaType"`
import (
"time"

databasegen "github.com/pet-sitter/pets-next-door-api/internal/infra/database/gen"
)

type DetailView struct {
ID int64 `json:"id"`
MediaType Type `json:"mediaType"`
URL string `json:"url"`
CreatedAt string `json:"createdAt"`
CreatedAt time.Time `json:"createdAt"`
}

type ListView []*DetailView

func ToDetailView(media databasegen.FindSingleMediaRow) *DetailView {
return &DetailView{
ID: int64(media.ID),
MediaType: Type(media.MediaType),
URL: media.Url,
CreatedAt: media.CreatedAt,
}
}

func ToDetailViewFromCreated(media databasegen.CreateMediaRow) *DetailView {
return &DetailView{
ID: int64(media.ID),
MediaType: Type(media.MediaType),
URL: media.Url,
CreatedAt: media.CreatedAt,
}
}

type MediaViewList []*MediaView
func ToDetailViewFromResourceMediaRows(resourceMedia databasegen.FindResourceMediaRow) *DetailView {
return &DetailView{
ID: int64(resourceMedia.MediaID),
MediaType: Type(resourceMedia.MediaType),
URL: resourceMedia.Url,
CreatedAt: resourceMedia.CreatedAt,
}
}

func (media *Media) ToMediaView() *MediaView {
return &MediaView{
func ToDetailViewFromViewForSOSPost(media ViewForSOSPost) *DetailView {
createdAt, err := time.Parse(time.RFC3339, media.CreatedAt)
if err != nil {
createdAt = time.Time{}
}

return &DetailView{
ID: media.ID,
MediaType: media.MediaType,
URL: media.URL,
CreatedAt: media.CreatedAt,
CreatedAt: createdAt,
}
}

func (mediaList *MediaList) ToMediaViewList() MediaViewList {
mediaViewList := make(MediaViewList, len(*mediaList))
for i, media := range *mediaList {
mediaViewList[i] = media.ToMediaView()
func ToListViewFromResourceMediaRows(resourceMediaList []databasegen.FindResourceMediaRow) ListView {
mediaViewList := make(ListView, len(resourceMediaList))
for i, resourceMedia := range resourceMediaList {
mediaViewList[i] = ToDetailViewFromResourceMediaRows(resourceMedia)
}
return mediaViewList
}

type ResourceMediaView struct {
ID int `field:"id"`
ResourceType ResourceType `field:"resource_type"`
ResourceID int `field:"resource_id"`
MediaID int `field:"media_id"`
func ToListViewFromViewListForSOSPost(mediaList ViewListForSOSPost) ListView {
mediaViewList := make(ListView, len(mediaList))
for i, media := range mediaList {
mediaViewList[i] = ToDetailViewFromViewForSOSPost(
ViewForSOSPost{
ID: media.ID,
MediaType: media.MediaType,
URL: media.URL,
CreatedAt: media.CreatedAt,
},
)
}
return mediaViewList
}
4 changes: 2 additions & 2 deletions internal/domain/pet/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type AddPetRequest struct {
BirthDate utils.Date `json:"birthDate" validate:"required"`
WeightInKg decimal.Decimal `json:"weightInKg" validate:"required"`
Remarks string `json:"remarks"`
ProfileImageID *int `json:"profileImageId"`
ProfileImageID *int64 `json:"profileImageId"`
}

type UpdatePetRequest struct {
Expand All @@ -29,5 +29,5 @@ type UpdatePetRequest struct {
BirthDate utils.Date `json:"birthDate" validate:"required"`
WeightInKg decimal.Decimal `json:"weightInKg" validate:"required"`
Remarks string `json:"remarks"`
ProfileImageID *int `json:"profileImageId"`
ProfileImageID *int64 `json:"profileImageId"`
}
11 changes: 11 additions & 0 deletions internal/domain/resourcemedia/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package resourcemedia

type ResourceType string

const (
SOSResourceType ResourceType = "sos_posts"
)

func (r ResourceType) String() string {
return string(r)
}
34 changes: 17 additions & 17 deletions internal/domain/sospost/sos_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type SOSPost struct {
CareType CareType `field:"care_type"`
CarerGender CarerGender `field:"carer_gender"`
RewardType RewardType `field:"reward_type"`
ThumbnailID int `field:"thumbnail_id"`
ThumbnailID int64 `field:"thumbnail_id"`
CreatedAt time.Time `field:"created_at"`
UpdatedAt time.Time `field:"updated_at"`
DeletedAt time.Time `field:"deleted_at"`
Expand All @@ -53,22 +53,22 @@ type SOSPostList struct {
}

type SOSPostInfo struct {
ID int `field:"id" json:"id"`
AuthorID int `field:"author" json:"author"`
Title string `field:"title" json:"title"`
Content string `field:"content" json:"content"`
Media media.MediaList `field:"media" json:"media"`
Conditions ConditionList `field:"conditions" json:"conditions"`
Pets pet.ViewListForSOSPost `field:"pets" json:"pets"`
Reward string `field:"reward" json:"reward"`
Dates SOSDatesList `field:"dates" json:"dates"`
CareType CareType `field:"careType" json:"careType"`
CarerGender CarerGender `field:"carerGender" json:"carerGender"`
RewardType RewardType `field:"rewardType" json:"rewardType"`
ThumbnailID int `field:"thumbnailId" json:"thumbnailId"`
CreatedAt time.Time `field:"createdAt" json:"createdAt"`
UpdatedAt time.Time `field:"updatedAt" json:"updatedAt"`
DeletedAt time.Time `field:"deletedAt" json:"deletedAt"`
ID int `field:"id" json:"id"`
AuthorID int `field:"author" json:"author"`
Title string `field:"title" json:"title"`
Content string `field:"content" json:"content"`
Media media.ViewListForSOSPost `field:"media" json:"media"`
Conditions ConditionList `field:"conditions" json:"conditions"`
Pets pet.ViewListForSOSPost `field:"pets" json:"pets"`
Reward string `field:"reward" json:"reward"`
Dates SOSDatesList `field:"dates" json:"dates"`
CareType CareType `field:"careType" json:"careType"`
CarerGender CarerGender `field:"carerGender" json:"carerGender"`
RewardType RewardType `field:"rewardType" json:"rewardType"`
ThumbnailID int64 `field:"thumbnailId" json:"thumbnailId"`
CreatedAt time.Time `field:"createdAt" json:"createdAt"`
UpdatedAt time.Time `field:"updatedAt" json:"updatedAt"`
DeletedAt time.Time `field:"deletedAt" json:"deletedAt"`
}

type SOSPostInfoList struct {
Expand Down
Loading

0 comments on commit 5cf6695

Please sign in to comment.