Skip to content

Commit

Permalink
refactor: use render.JSON for response
Browse files Browse the repository at this point in the history
  • Loading branch information
litsynp committed Feb 3, 2024
1 parent 8bd3d80 commit 958e0de
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 75 deletions.
36 changes: 9 additions & 27 deletions api/common.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package pnd

import (
"encoding/json"
"net/http"
)
type ItemListView[T interface{}] struct {
Items []T `json:"items"`
}

func NewItemListView[T interface{}](items []T) *ItemListView[T] {
return &ItemListView[T]{
Items: items,
}
}

type PaginatedView[T interface{}] struct {
Page int `json:"page"`
Expand All @@ -18,26 +23,3 @@ func NewPaginatedView[T interface{}](page int, size int, items []T) *PaginatedVi
Items: items,
}
}

func writePayload(w http.ResponseWriter, headers map[string]string, payload interface{}, statusCode int) error {
setHeaders(w, headers)

w.WriteHeader(statusCode)

if payload == nil {
return nil
}

return json.NewEncoder(w).Encode(payload)
}

func setHeaders(w http.ResponseWriter, headers map[string]string) {
if headers == nil {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
return
}

for key, value := range headers {
w.Header().Set(key, value)
}
}
15 changes: 0 additions & 15 deletions api/success.go

This file was deleted.

5 changes: 1 addition & 4 deletions cmd/server/handler/auth_handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handler

import (
"encoding/json"
"fmt"
"net/http"

Expand Down Expand Up @@ -69,9 +68,7 @@ func (h *authHandler) KakaoCallback(w http.ResponseWriter, r *http.Request) {
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(auth.KakaoCallbackResponse{
render.JSON(w, r, auth.KakaoCallbackResponse{
AuthToken: *customToken,
FirebaseProviderType: user.FirebaseProviderTypeKakao,
FirebaseUID: fmt.Sprintf("%d", userProfile.ID),
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/handler/breed_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ func (h *BreedHandler) FindBreeds(w http.ResponseWriter, r *http.Request) {
return
}

pnd.OK(w, nil, pnd.NewPaginatedView(page, size, res))
render.JSON(w, r, pnd.NewPaginatedView(page, size, res))
}
4 changes: 2 additions & 2 deletions cmd/server/handler/condition_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewConditionHandler(conditionService *sos_post.ConditionService) *Condition
// @Tags posts
// @Accept json
// @Produce json
// @Success 200 {object} []sos_post.ConditionView
// @Success 200 {object} pnd.ItemListView[sos_post.ConditionView]
// @Router /posts/sos/conditions [get]
func (h *ConditionHandler) FindConditions(w http.ResponseWriter, r *http.Request) {
res, err := h.conditionService.FindConditions()
Expand All @@ -31,5 +31,5 @@ func (h *ConditionHandler) FindConditions(w http.ResponseWriter, r *http.Request
return
}

pnd.OK(w, nil, res)
render.JSON(w, r, pnd.NewItemListView(res))
}
17 changes: 8 additions & 9 deletions cmd/server/handler/media_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (h *mediaHandler) FindMediaByID(w http.ResponseWriter, r *http.Request) {
return
}

pnd.OK(w, nil, media.MediaView{
render.JSON(w, r, media.MediaView{
ID: found.ID,
MediaType: found.MediaType,
URL: found.URL,
Expand Down Expand Up @@ -83,14 +83,13 @@ func (h *mediaHandler) UploadImage(w http.ResponseWriter, r *http.Request) {
return
}

pnd.Created(w,
nil,
media.MediaView{
ID: res.ID,
MediaType: res.MediaType,
URL: res.URL,
CreatedAt: res.CreatedAt,
})
render.Status(r, http.StatusCreated)
render.JSON(w, r, media.MediaView{
ID: res.ID,
MediaType: res.MediaType,
URL: res.URL,
CreatedAt: res.CreatedAt,
})
}

var supportedMimeTypes = []string{
Expand Down
9 changes: 5 additions & 4 deletions cmd/server/handler/sos_post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func (h *SosPostHandler) WriteSosPost(w http.ResponseWriter, r *http.Request) {
return
}

pnd.Created(w, nil, res)
render.Status(r, http.StatusCreated)
render.JSON(w, r, res)
}

// FindSosPosts godoc
Expand Down Expand Up @@ -101,7 +102,7 @@ func (h *SosPostHandler) FindSosPosts(w http.ResponseWriter, r *http.Request) {
}
}

pnd.OK(w, nil, pnd.NewPaginatedView(page, size, res))
render.JSON(w, r, pnd.NewPaginatedView(page, size, res))
}

// FindSosPostByID godoc
Expand All @@ -125,7 +126,7 @@ func (h *SosPostHandler) FindSosPostByID(w http.ResponseWriter, r *http.Request)
return
}

pnd.OK(w, nil, res)
render.JSON(w, r, res)
}

// UpdateSosPost godoc
Expand Down Expand Up @@ -167,5 +168,5 @@ func (h *SosPostHandler) UpdateSosPost(w http.ResponseWriter, r *http.Request) {
return
}

pnd.OK(w, nil, res)
render.JSON(w, r, res)
}
28 changes: 16 additions & 12 deletions cmd/server/handler/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func (h *UserHandler) RegisterUser(w http.ResponseWriter, r *http.Request) {
return
}

pnd.Created(w, nil, res)
render.Status(r, http.StatusCreated)
render.JSON(w, r, res)
}

// CheckUserNickname godoc
Expand All @@ -70,7 +71,7 @@ func (h *UserHandler) CheckUserNickname(w http.ResponseWriter, r *http.Request)
return
}

pnd.OK(w, nil, user.CheckNicknameView{IsAvailable: !exists})
render.JSON(w, r, user.CheckNicknameView{IsAvailable: !exists})
}

// FindUserStatusByEmail godoc
Expand All @@ -90,14 +91,17 @@ func (h *UserHandler) FindUserStatusByEmail(w http.ResponseWriter, r *http.Reque
}

userStatus, err := h.userService.FindUserStatusByEmail(providerRequest.Email)
if err != nil || userStatus == nil {
pnd.OK(w, nil, user.UserStatusView{
Status: user.UserStatusNotRegistered,
})
if err != nil {
render.Render(w, r, err)
return
}

if userStatus == nil {
render.JSON(w, r, user.UserStatusView{Status: user.UserStatusNotRegistered})
return
}

pnd.OK(w, nil, user.UserStatusView{
render.JSON(w, r, user.UserStatusView{
Status: user.UserStatusRegistered,
FirebaseProviderType: userStatus.FirebaseProviderType,
})
Expand Down Expand Up @@ -136,7 +140,7 @@ func (h *UserHandler) FindUsers(w http.ResponseWriter, r *http.Request) {
return
}

pnd.OK(w, nil, pnd.NewPaginatedView(page, size, res))
render.JSON(w, r, pnd.NewPaginatedView(page, size, res))
}

// FindMyProfile godoc
Expand All @@ -154,7 +158,7 @@ func (h *UserHandler) FindMyProfile(w http.ResponseWriter, r *http.Request) {
return
}

pnd.OK(w, nil, res.ToMyProfileResponse())
render.JSON(w, r, res.ToMyProfileResponse())
}

// UpdateMyProfile godoc
Expand Down Expand Up @@ -188,7 +192,7 @@ func (h *UserHandler) UpdateMyProfile(w http.ResponseWriter, r *http.Request) {
return
}

pnd.OK(w, nil, user.UpdateUserResponse{
render.JSON(w, r, user.UpdateUserResponse{
ID: userModel.ID,
Email: userModel.Email,
Nickname: userModel.Nickname,
Expand Down Expand Up @@ -228,7 +232,7 @@ func (h *UserHandler) AddMyPets(w http.ResponseWriter, r *http.Request) {
return
}

pnd.OK(w, nil, nil)
render.Status(r, http.StatusOK)
}

// FindMyPets godoc
Expand All @@ -254,5 +258,5 @@ func (h *UserHandler) FindMyPets(w http.ResponseWriter, r *http.Request) {
return
}

pnd.OK(w, nil, res)
render.JSON(w, r, res)
}
2 changes: 1 addition & 1 deletion internal/domain/sos_post/condition_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewConditionService(conditionStore ConditionStore) *ConditionService {
func (service *ConditionService) FindConditions() ([]ConditionView, *pnd.AppError) {
conditions, err := service.conditionStore.FindConditions()
if err != nil {
return nil, err
return []ConditionView{}, err
}

var conditionViews []ConditionView
Expand Down

0 comments on commit 958e0de

Please sign in to comment.