Skip to content

Commit

Permalink
fix: update FindSosPostByID
Browse files Browse the repository at this point in the history
  • Loading branch information
barabobBOB committed Apr 14, 2024
1 parent 9d55238 commit be0aefd
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 69 deletions.
7 changes: 3 additions & 4 deletions cmd/server/handler/sos_post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,16 @@ func (h *SosPostHandler) FindSosPosts(c echo.Context) error {
// @Summary 게시글 ID로 돌봄급구 게시글을 조회합니다.
// @Description
// @Tags posts
// @Accept json
// @Produce json
// @Param id path string true "게시글 ID"
// @Param id path int true "게시글 ID"
// @Success 200 {object} sos_post.FindSosPostView
// @Router /posts/sos/{id} [get]
func (h *SosPostHandler) FindSosPostByID(c echo.Context) error {
SosPostID, err := pnd.ParseIDFromPath(c, "id")
id, err := pnd.ParseIDFromPath(c, "id")
if err != nil {
return c.JSON(err.StatusCode, err)
}
res, err := h.sosPostService.FindSosPostByID(c.Request().Context(), *SosPostID)
res, err := h.sosPostService.FindSosPostByID(c.Request().Context(), *id)
if err != nil {
return c.JSON(err.StatusCode, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func NewRouter(app *firebaseinfra.FirebaseApp) *echo.Echo {
postApiGroup := apiRouteGroup.Group("/posts")
{
postApiGroup.POST("/sos", sosPostHandler.WriteSosPost)
postApiGroup.GET("/sos/{id}", sosPostHandler.FindSosPostByID)
postApiGroup.GET("/sos/:id", sosPostHandler.FindSosPostByID)
postApiGroup.GET("/sos", sosPostHandler.FindSosPosts)
postApiGroup.PUT("/sos", sosPostHandler.UpdateSosPost)
postApiGroup.GET("/sos/conditions", conditionHandler.FindConditions)
Expand Down
103 changes: 65 additions & 38 deletions internal/postgres/sos_post_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func FindSosPosts(ctx context.Context, tx *database.Tx, page int, size int, sort
sosPostList := sos_post.NewSosPostInfoList(page, size)
for rows.Next() {
sosPost := sos_post.SosPostInfo{}
var datesData, petsData, mediaData, conditionsData string
var datesData, petsData, mediaData, conditionsData []byte
if err := rows.Scan(
&sosPost.ID,
&sosPost.Title,
Expand All @@ -269,19 +269,18 @@ func FindSosPosts(ctx context.Context, tx *database.Tx, page int, size int, sort
return nil, pnd.FromPostgresError(err)
}

if err := json.Unmarshal([]byte(datesData), &sosPost.Dates); err != nil {
if err := json.Unmarshal(datesData, &sosPost.Dates); err != nil {
return nil, pnd.FromPostgresError(err)
}
if err := json.Unmarshal([]byte(petsData), &sosPost.Pets); err != nil {
if err := json.Unmarshal(petsData, &sosPost.Pets); err != nil {
return nil, pnd.FromPostgresError(err)
}
if err := json.Unmarshal([]byte(mediaData), &sosPost.Media); err != nil {
if err := json.Unmarshal(mediaData, &sosPost.Media); err != nil {
return nil, pnd.FromPostgresError(err)
}
if err := json.Unmarshal([]byte(conditionsData), &sosPost.Conditions); err != nil {
if err := json.Unmarshal(conditionsData, &sosPost.Conditions); err != nil {
return nil, pnd.FromPostgresError(err)
}
fmt.Println(sosPost.ID)
sosPostList.Items = append(sosPostList.Items, sosPost)
}
if err := rows.Err(); err != nil {
Expand Down Expand Up @@ -358,14 +357,14 @@ func FindSosPostsByAuthorID(ctx context.Context, tx *database.Tx, authorID int,

rows, err := tx.QueryContext(ctx, query, authorID, size+1, (page-1)*size)
if err != nil {
return &sos_post.SosPostInfoList{}, pnd.FromPostgresError(err)
return nil, pnd.FromPostgresError(err)
}
defer rows.Close()

sosPostList := sos_post.NewSosPostInfoList(page, size)
for rows.Next() {
sosPost := sos_post.SosPostInfo{}
var datesData, petsData, mediaData, conditionsData string
var datesData, petsData, mediaData, conditionsData []byte

if err := rows.Scan(
&sosPost.ID,
Expand All @@ -386,16 +385,16 @@ func FindSosPostsByAuthorID(ctx context.Context, tx *database.Tx, authorID int,
return nil, pnd.FromPostgresError(err)
}

if err := json.Unmarshal([]byte(datesData), &sosPost.Dates); err != nil {
if err := json.Unmarshal(datesData, &sosPost.Dates); err != nil {
return nil, pnd.FromPostgresError(err)
}
if err := json.Unmarshal([]byte(petsData), &sosPost.Pets); err != nil {
if err := json.Unmarshal(petsData, &sosPost.Pets); err != nil {
return nil, pnd.FromPostgresError(err)
}
if err := json.Unmarshal([]byte(mediaData), &sosPost.Media); err != nil {
if err := json.Unmarshal(mediaData, &sosPost.Media); err != nil {
return nil, pnd.FromPostgresError(err)
}
if err := json.Unmarshal([]byte(conditionsData), &sosPost.Conditions); err != nil {
if err := json.Unmarshal(conditionsData, &sosPost.Conditions); err != nil {
return nil, pnd.FromPostgresError(err)
}

Expand All @@ -409,45 +408,73 @@ func FindSosPostsByAuthorID(ctx context.Context, tx *database.Tx, authorID int,
return sosPostList, nil
}

func FindSosPostByID(ctx context.Context, tx *database.Tx, id int) (*sos_post.SosPost, *pnd.AppError) {
const query = `
SELECT
id,
author_id,
title,
content,
reward,
care_type,
carer_gender,
reward_type,
thumbnail_id,
created_at,
updated_at
FROM
sos_posts
WHERE
id = $1 AND
deleted_at IS NULL
`
func FindSosPostByID(ctx context.Context, tx *database.Tx, id int) (*sos_post.SosPostInfo, *pnd.AppError) {
query := fmt.Sprintf(`
SELECT
v_sos_posts.id,
v_sos_posts.title,
v_sos_posts.content,
v_sos_posts.reward,
v_sos_posts.reward_type,
v_sos_posts.care_type,
v_sos_posts.carer_gender,
v_sos_posts.thumbnail_id,
v_sos_posts.author_id,
v_sos_posts.created_at,
v_sos_posts.updated_at,
v_sos_posts.dates,
v_pets.pets_info,
v_media.media_info,
v_conditions.conditions_info
FROM
v_sos_posts
LEFT JOIN v_pets ON v_sos_posts.id = v_pets.sos_post_id
LEFT JOIN v_media ON v_sos_posts.id = v_media.sos_post_id
LEFT JOIN v_conditions ON v_sos_posts.id = v_conditions.sos_post_id
WHERE
v_sos_posts.id = $1;
sosPost := &sos_post.SosPost{}
if err := tx.QueryRowContext(ctx, query, id).Scan(
`,
)

row := tx.QueryRowContext(ctx, query, id)

sosPost := sos_post.SosPostInfo{}

var datesData, petsData, mediaData, conditionsData []byte
if err := row.Scan(
&sosPost.ID,
&sosPost.AuthorID,
&sosPost.Title,
&sosPost.Content,
&sosPost.Reward,
&sosPost.RewardType,
&sosPost.CareType,
&sosPost.CarerGender,
&sosPost.RewardType,
&sosPost.ThumbnailID,
&sosPost.AuthorID,
&sosPost.CreatedAt,
&sosPost.UpdatedAt,
); err != nil {
&datesData,
&petsData,
&mediaData,
&conditionsData); err != nil {
return nil, pnd.FromPostgresError(err)
}

return sosPost, nil
if err := json.Unmarshal(datesData, &sosPost.Dates); err != nil {
return nil, pnd.FromPostgresError(err)
}
if err := json.Unmarshal(petsData, &sosPost.Pets); err != nil {
return nil, pnd.FromPostgresError(err)
}
if err := json.Unmarshal(mediaData, &sosPost.Media); err != nil {
return nil, pnd.FromPostgresError(err)
}
if err := json.Unmarshal(conditionsData, &sosPost.Conditions); err != nil {
return nil, pnd.FromPostgresError(err)
}

return &sosPost, nil
}

func UpdateSosPost(ctx context.Context, tx *database.Tx, request *sos_post.UpdateSosPostRequest) (*sos_post.SosPost, *pnd.AppError) {
Expand Down
30 changes: 5 additions & 25 deletions internal/service/sos_post_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,26 +145,6 @@ func (service *SosPostService) FindSosPostByID(ctx context.Context, id int) (*so
return nil, err
}

mediaData, err := postgres.FindResourceMediaByResourceID(ctx, tx, sosPost.ID, string(media.SosResourceType))
if err != nil {
return nil, err
}

conditions, err := postgres.FindConditionByID(ctx, tx, sosPost.ID)
if err != nil {
return nil, err
}

pets, err := postgres.FindPetsByID(ctx, tx, sosPost.ID)
if err != nil {
return nil, err
}

dates, err := postgres.FindDatesBySosPostID(ctx, tx, sosPost.ID)
if err != nil {
return nil, err
}

author, err := postgres.FindUserByID(ctx, tx, sosPost.AuthorID, true)
if err != nil {
return nil, err
Expand All @@ -174,12 +154,12 @@ func (service *SosPostService) FindSosPostByID(ctx context.Context, id int) (*so
return nil, err
}

return sosPost.ToFindSosPostView(
return sosPost.ToFindSosPostInfoView(
author.ToUserWithoutPrivateInfo(),
mediaData.ToMediaViewList(),
conditions.ToConditionViewList(),
pets.ToPetViewList(),
dates.ToSosDateViewList(),
sosPost.Media.ToMediaViewList(),
sosPost.Conditions.ToConditionViewList(),
sosPost.Pets.ToPetViewList(),
sosPost.Dates.ToSosDateViewList(),
), nil
}

Expand Down
4 changes: 3 additions & 1 deletion internal/service/tests/sos_post_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ func TestSosPostService(t *testing.T) {
sosPostService := service.NewSosPostService(db)

conditionIDs := []int{1, 2}
//krLocation, _ := time.LoadLocation("Asia/Seoul")

sosPosts := make([]sos_post.WriteSosPostView, 0)
for i := 1; i < 4; i++ {
Expand Down Expand Up @@ -758,6 +757,9 @@ func TestSosPostService(t *testing.T) {
URL: "https://test3.com",
})

sosPostImage.CreatedAt = sosPostImage.CreatedAt[:len(sosPostImage.CreatedAt)-1]
sosPostImage2.CreatedAt = sosPostImage2.CreatedAt[:len(sosPostImage2.CreatedAt)-1]

userService := service.NewUserService(db, mediaService)

owner, err := userService.RegisterUser(ctx, &user.RegisterUserRequest{
Expand Down

0 comments on commit be0aefd

Please sign in to comment.