Skip to content

Commit

Permalink
refactor: rename response to view
Browse files Browse the repository at this point in the history
  • Loading branch information
litsynp committed Feb 3, 2024
1 parent 958e0de commit c8a30fb
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 109 deletions.
4 changes: 2 additions & 2 deletions cmd/server/handler/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (h *authHandler) KakaoLogin(w http.ResponseWriter, r *http.Request) {
// @Summary Kakao 회원가입 콜백 API
// @Description Kakao 로그인 콜백을 처리하고, 사용자 기본 정보와 함께 Firebase Custom Token을 발급합니다.
// @Tags auth
// @Success 200 {object} auth.KakaoCallbackResponse
// @Success 200 {object} auth.KakaoCallbackView
// @Router /auth/callback/kakao [get]
func (h *authHandler) KakaoCallback(w http.ResponseWriter, r *http.Request) {
code := utils.ParseOptionalStringQuery(r, "code")
Expand All @@ -68,7 +68,7 @@ func (h *authHandler) KakaoCallback(w http.ResponseWriter, r *http.Request) {
return
}

render.JSON(w, r, auth.KakaoCallbackResponse{
render.JSON(w, r, auth.KakaoCallbackView{
AuthToken: *customToken,
FirebaseProviderType: user.FirebaseProviderTypeKakao,
FirebaseUID: fmt.Sprintf("%d", userProfile.ID),
Expand Down
8 changes: 4 additions & 4 deletions cmd/server/handler/sos_post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewSosPostHandler(sosPostService sos_post.SosPostService, authService auth.
// @Produce json
// @Param request body sos_post.WriteSosPostRequest true "돌봄급구 게시글 업로드 요청"
// @Security FirebaseAuth
// @Success 201 {object} sos_post.WriteSosPostResponse
// @Success 201 {object} sos_post.WriteSosPostView
// @Router /posts/sos [post]
func (h *SosPostHandler) WriteSosPost(w http.ResponseWriter, r *http.Request) {
foundUser, err := h.authService.VerifyAuthAndGetUser(r.Context(), r)
Expand Down Expand Up @@ -66,7 +66,7 @@ func (h *SosPostHandler) WriteSosPost(w http.ResponseWriter, r *http.Request) {
// @Param page query int false "페이지 번호" default(1)
// @Param size query int false "페이지 사이즈" default(20)
// @Param sort_by query string false "정렬 기준" Enums(newest, deadline)
// @Success 200 {object} pnd.PaginatedView[sos_post.FindSosPostResponse]
// @Success 200 {object} pnd.PaginatedView[sos_post.FindSosPostView]
// @Router /posts/sos [get]
func (h *SosPostHandler) FindSosPosts(w http.ResponseWriter, r *http.Request) {
authorID, err := utils.ParseOptionalIntQuery(r, "author_id")
Expand All @@ -86,7 +86,7 @@ func (h *SosPostHandler) FindSosPosts(w http.ResponseWriter, r *http.Request) {
return
}

var res []sos_post.FindSosPostResponse
var res []sos_post.FindSosPostView

if authorID != nil {
res, err = h.sosPostService.FindSosPostsByAuthorID(*authorID, page, size)
Expand All @@ -112,7 +112,7 @@ func (h *SosPostHandler) FindSosPosts(w http.ResponseWriter, r *http.Request) {
// @Accept json
// @Produce json
// @Param id path string true "게시글 ID"
// @Success 200 {object} sos_post.FindSosPostResponse
// @Success 200 {object} sos_post.FindSosPostView
// @Router /posts/sos/{id} [get]
func (h *SosPostHandler) FindSosPostByID(w http.ResponseWriter, r *http.Request) {
SosPostID, err := utils.ParseIdFromPath(r, "id")
Expand Down
10 changes: 5 additions & 5 deletions cmd/server/handler/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewUserHandler(userService user.UserServicer, authService auth.AuthService)
// @Accept json
// @Produce json
// @Param request body user.RegisterUserRequest true "사용자 회원가입 요청"
// @Success 201 {object} user.RegisterUserResponse
// @Success 201 {object} user.RegisterUserView
// @Router /users [post]
func (h *UserHandler) RegisterUser(w http.ResponseWriter, r *http.Request) {
var registerUserRequest user.RegisterUserRequest
Expand Down Expand Up @@ -149,7 +149,7 @@ func (h *UserHandler) FindUsers(w http.ResponseWriter, r *http.Request) {
// @Tags users
// @Produce json
// @Security FirebaseAuth
// @Success 200 {object} user.MyProfileResponse
// @Success 200 {object} user.MyProfileView
// @Router /users/me [get]
func (h *UserHandler) FindMyProfile(w http.ResponseWriter, r *http.Request) {
res, err := h.authService.VerifyAuthAndGetUser(r.Context(), r)
Expand All @@ -158,7 +158,7 @@ func (h *UserHandler) FindMyProfile(w http.ResponseWriter, r *http.Request) {
return
}

render.JSON(w, r, res.ToMyProfileResponse())
render.JSON(w, r, res.ToMyProfileView())
}

// UpdateMyProfile godoc
Expand All @@ -169,7 +169,7 @@ func (h *UserHandler) FindMyProfile(w http.ResponseWriter, r *http.Request) {
// @Produce json
// @Security FirebaseAuth
// @Param request body user.UpdateUserRequest true "사용자 프로필 수정 요청"
// @Success 200 {object} user.UpdateUserResponse
// @Success 200 {object} user.UpdateUserView
// @Router /users/me [put]
func (h *UserHandler) UpdateMyProfile(w http.ResponseWriter, r *http.Request) {
foundUser, err := h.authService.VerifyAuthAndGetUser(r.Context(), r)
Expand All @@ -192,7 +192,7 @@ func (h *UserHandler) UpdateMyProfile(w http.ResponseWriter, r *http.Request) {
return
}

render.JSON(w, r, user.UpdateUserResponse{
render.JSON(w, r, user.UpdateUserView{
ID: userModel.ID,
Email: userModel.Email,
Nickname: userModel.Nickname,
Expand Down
5 changes: 2 additions & 3 deletions cmd/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"encoding/json"
"log"
"net/http"

Expand All @@ -11,6 +10,7 @@ import (

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/render"
"github.com/pet-sitter/pets-next-door-api/cmd/server/handler"
"github.com/pet-sitter/pets-next-door-api/internal/configs"
"github.com/pet-sitter/pets-next-door-api/internal/domain/auth"
Expand Down Expand Up @@ -86,8 +86,7 @@ func NewRouter(app *firebaseinfra.FirebaseApp) *chi.Mux {

// Register routes
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
render.JSON(w, r, map[string]string{"status": "ok"})
})

r.Get("/swagger/*", httpSwagger.Handler(
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type AuthService interface {
VerifyAuthAndGetUser(ctx context.Context, r *http.Request) (*user.FindUserResponse, *pnd.AppError)
VerifyAuthAndGetUser(ctx context.Context, r *http.Request) (*user.FindUserView, *pnd.AppError)
CustomToken(ctx context.Context, uid string) (*string, *pnd.AppError)
}

Expand All @@ -39,7 +39,7 @@ func (s *FirebaseBearerAuthService) verifyAuth(ctx context.Context, authHeader s
return authToken, err
}

func (s *FirebaseBearerAuthService) VerifyAuthAndGetUser(ctx context.Context, r *http.Request) (*user.FindUserResponse, *pnd.AppError) {
func (s *FirebaseBearerAuthService) VerifyAuthAndGetUser(ctx context.Context, r *http.Request) (*user.FindUserView, *pnd.AppError) {
authToken, err := s.verifyAuth(ctx, r.Header.Get("Authorization"))
if err != nil {
return nil, pnd.ErrInvalidFBToken(fmt.Errorf("유효하지 않은 인증 토큰입니다"))
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/auth/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package auth

import "github.com/pet-sitter/pets-next-door-api/internal/domain/user"

type KakaoCallbackResponse struct {
type KakaoCallbackView struct {
AuthToken string `json:"authToken"`
FirebaseProviderType user.FirebaseProviderType `json:"fbProviderType"`
FirebaseUID string `json:"fbUid"`
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/media/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func generateRandomFileName(originalFileName string) string {
return uuid.New().String() + extension
}

type UploadFileResponse struct {
type UploadFileView struct {
FileEndpoint string
}

Expand Down
32 changes: 16 additions & 16 deletions internal/domain/sos_post/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewSosPostService(sosPostStore SosPostStore, resourceMediaStore media.Resou
}
}

func (service *SosPostService) WriteSosPost(fbUid string, request *WriteSosPostRequest) (*WriteSosPostResponse, *pnd.AppError) {
func (service *SosPostService) WriteSosPost(fbUid string, request *WriteSosPostRequest) (*WriteSosPostView, *pnd.AppError) {
userID, err := service.userStore.FindUserIDByFbUID(fbUid)
if err != nil {
return nil, err
Expand Down Expand Up @@ -89,7 +89,7 @@ func (service *SosPostService) WriteSosPost(fbUid string, request *WriteSosPostR
petsView = append(petsView, p)
}

return &WriteSosPostResponse{
return &WriteSosPostView{
ID: sosPost.ID,
AuthorID: sosPost.AuthorID,
Title: sosPost.Title,
Expand All @@ -111,13 +111,13 @@ func (service *SosPostService) WriteSosPost(fbUid string, request *WriteSosPostR
}, nil
}

func (service *SosPostService) FindSosPosts(page int, size int, sortBy string) ([]FindSosPostResponse, *pnd.AppError) {
func (service *SosPostService) FindSosPosts(page int, size int, sortBy string) ([]FindSosPostView, *pnd.AppError) {
sosPosts, err := service.sosPostStore.FindSosPosts(page, size, sortBy)
if err != nil {
return nil, err
}

var FindSosPostResponseList []FindSosPostResponse
var findSosPostViews []FindSosPostView

for _, sosPost := range sosPosts {
mediaData, err := service.resourceMediaStore.FindResourceMediaByResourceID(sosPost.ID, string(media.SosResourceType))
Expand Down Expand Up @@ -172,7 +172,7 @@ func (service *SosPostService) FindSosPosts(page int, size int, sortBy string) (
petsView = append(petsView, p)
}

findByAuthorSosPostResponse := &FindSosPostResponse{
findByAuthorSosPostView := &FindSosPostView{
ID: sosPost.ID,
AuthorID: sosPost.AuthorID,
Title: sosPost.Title,
Expand All @@ -193,19 +193,19 @@ func (service *SosPostService) FindSosPosts(page int, size int, sortBy string) (
UpdatedAt: sosPost.UpdatedAt,
}

FindSosPostResponseList = append(FindSosPostResponseList, *findByAuthorSosPostResponse)
findSosPostViews = append(findSosPostViews, *findByAuthorSosPostView)
}

return FindSosPostResponseList, nil
return findSosPostViews, nil
}

func (service *SosPostService) FindSosPostsByAuthorID(authorID int, page int, size int) ([]FindSosPostResponse, *pnd.AppError) {
func (service *SosPostService) FindSosPostsByAuthorID(authorID int, page int, size int) ([]FindSosPostView, *pnd.AppError) {
sosPosts, err := service.sosPostStore.FindSosPostsByAuthorID(authorID, page, size)
if err != nil {
return nil, err
}

var FindSosPostResponseList []FindSosPostResponse
var findSosPostViews []FindSosPostView

for _, sosPost := range sosPosts {
mediaData, err := service.resourceMediaStore.FindResourceMediaByResourceID(sosPost.ID, string(media.SosResourceType))
Expand Down Expand Up @@ -260,7 +260,7 @@ func (service *SosPostService) FindSosPostsByAuthorID(authorID int, page int, si
petsView = append(petsView, p)
}

findByAuthorSosPostResponse := &FindSosPostResponse{
findByAuthorSosPostView := &FindSosPostView{
ID: sosPost.ID,
AuthorID: sosPost.AuthorID,
Title: sosPost.Title,
Expand All @@ -281,13 +281,13 @@ func (service *SosPostService) FindSosPostsByAuthorID(authorID int, page int, si
UpdatedAt: sosPost.UpdatedAt,
}

FindSosPostResponseList = append(FindSosPostResponseList, *findByAuthorSosPostResponse)
findSosPostViews = append(findSosPostViews, *findByAuthorSosPostView)
}

return FindSosPostResponseList, nil
return findSosPostViews, nil
}

func (service *SosPostService) FindSosPostByID(id int) (*FindSosPostResponse, *pnd.AppError) {
func (service *SosPostService) FindSosPostByID(id int) (*FindSosPostView, *pnd.AppError) {
sosPost, err := service.sosPostStore.FindSosPostByID(id)
if err != nil {
return nil, err
Expand Down Expand Up @@ -345,7 +345,7 @@ func (service *SosPostService) FindSosPostByID(id int) (*FindSosPostResponse, *p
petsView = append(petsView, p)
}

return &FindSosPostResponse{
return &FindSosPostView{
ID: sosPost.ID,
AuthorID: sosPost.AuthorID,
Title: sosPost.Title,
Expand All @@ -367,7 +367,7 @@ func (service *SosPostService) FindSosPostByID(id int) (*FindSosPostResponse, *p
}, nil
}

func (service *SosPostService) UpdateSosPost(request *UpdateSosPostRequest) (*UpdateSosPostResponse, *pnd.AppError) {
func (service *SosPostService) UpdateSosPost(request *UpdateSosPostRequest) (*UpdateSosPostView, *pnd.AppError) {
updateSosPost, err := service.sosPostStore.UpdateSosPost(request)
if err != nil {
return nil, err
Expand Down Expand Up @@ -425,7 +425,7 @@ func (service *SosPostService) UpdateSosPost(request *UpdateSosPostRequest) (*Up
petsView = append(petsView, p)
}

return &UpdateSosPostResponse{
return &UpdateSosPostView{
ID: updateSosPost.ID,
AuthorID: updateSosPost.AuthorID,
Title: updateSosPost.Title,
Expand Down
6 changes: 3 additions & 3 deletions internal/domain/sos_post/tests/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestSosPostService(t *testing.T) {
conditionIDs := []int{1, 2}
krLocation, _ := time.LoadLocation("Asia/Seoul")

var sosPosts []sos_post.WriteSosPostResponse
var sosPosts []sos_post.WriteSosPostView

for i := 1; i < 4; i++ {
sosPost, err := sosPostService.WriteSosPost(uid, &sos_post.WriteSosPostRequest{
Expand Down Expand Up @@ -389,7 +389,7 @@ func TestSosPostService(t *testing.T) {
conditionIDs := []int{1, 2}
krLocation, _ := time.LoadLocation("Asia/Seoul")

sosPosts := make([]sos_post.WriteSosPostResponse, 0)
sosPosts := make([]sos_post.WriteSosPostView, 0)
for i := 1; i < 4; i++ {
sosPost, err := sosPostService.WriteSosPost(uid, &sos_post.WriteSosPostRequest{
Title: fmt.Sprintf("Title%d", i),
Expand Down Expand Up @@ -538,7 +538,7 @@ func TestSosPostService(t *testing.T) {
conditionIDs := []int{1, 2}
krLocation, _ := time.LoadLocation("Asia/Seoul")

sosPosts := make([]sos_post.WriteSosPostResponse, 0)
sosPosts := make([]sos_post.WriteSosPostView, 0)
for i := 1; i < 4; i++ {
sosPost, err := sosPostService.WriteSosPost(uid, &sos_post.WriteSosPostRequest{
Title: fmt.Sprintf("Title%d", i),
Expand Down
9 changes: 5 additions & 4 deletions internal/domain/sos_post/view.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package sos_post

import (
"time"

"github.com/pet-sitter/pets-next-door-api/internal/domain/media"
"github.com/pet-sitter/pets-next-door-api/internal/domain/pet"
"time"
)

type WriteSosPostRequest struct {
Expand All @@ -22,7 +23,7 @@ type WriteSosPostRequest struct {
PetIDs []int `json:"pet_ids"`
}

type WriteSosPostResponse struct {
type WriteSosPostView struct {
ID int `json:"id"`
AuthorID int `json:"author_id"`
Title string `json:"title"`
Expand All @@ -43,7 +44,7 @@ type WriteSosPostResponse struct {
UpdatedAt time.Time `json:"updated_at"`
}

type FindSosPostResponse struct {
type FindSosPostView struct {
ID int `json:"id"`
AuthorID int `json:"author_id"`
Title string `json:"title"`
Expand Down Expand Up @@ -81,7 +82,7 @@ type UpdateSosPostRequest struct {
PetIDs []int `json:"pet_ids"`
}

type UpdateSosPostResponse struct {
type UpdateSosPostView struct {
ID int `json:"id"`
AuthorID int `json:"author_id"`
Title string `json:"title"`
Expand Down
12 changes: 6 additions & 6 deletions internal/domain/user/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ func NewUserService(userStore UserStore, petStore pet.PetStore, mediaService med
}

type UserServicer interface {
RegisterUser(registerUserRequest *RegisterUserRequest) (*RegisterUserResponse, *pnd.AppError)
RegisterUser(registerUserRequest *RegisterUserRequest) (*RegisterUserView, *pnd.AppError)
FindUsers(page int, size int, nickname *string) ([]*UserWithoutPrivateInfo, *pnd.AppError)
FindUserByEmail(email string) (*UserWithProfileImage, *pnd.AppError)
FindUserByUID(uid string) (*FindUserResponse, *pnd.AppError)
FindUserByUID(uid string) (*FindUserView, *pnd.AppError)
ExistsByNickname(nickname string) (bool, *pnd.AppError)
FindUserStatusByEmail(email string) (*UserStatus, *pnd.AppError)
UpdateUserByUID(uid string, nickname string, profileImageID *int) (*UserWithProfileImage, *pnd.AppError)
AddPetsToOwner(uid string, addPetsRequest pet.AddPetsToOwnerRequest) ([]pet.PetView, *pnd.AppError)
FindPetsByOwnerUID(uid string) (*pet.FindMyPetsView, *pnd.AppError)
}

func (service *UserService) RegisterUser(registerUserRequest *RegisterUserRequest) (*RegisterUserResponse, *pnd.AppError) {
func (service *UserService) RegisterUser(registerUserRequest *RegisterUserRequest) (*RegisterUserView, *pnd.AppError) {
var media *media.Media
var err *pnd.AppError
if registerUserRequest.ProfileImageID != nil {
Expand All @@ -56,7 +56,7 @@ func (service *UserService) RegisterUser(registerUserRequest *RegisterUserReques
profileImageURL = &media.URL
}

return &RegisterUserResponse{
return &RegisterUserView{
ID: created.ID,
Email: created.Email,
Nickname: created.Nickname,
Expand Down Expand Up @@ -85,13 +85,13 @@ func (service *UserService) FindUserByEmail(email string) (*UserWithProfileImage
return user, nil
}

func (service *UserService) FindUserByUID(uid string) (*FindUserResponse, *pnd.AppError) {
func (service *UserService) FindUserByUID(uid string) (*FindUserView, *pnd.AppError) {
user, err := service.userStore.FindUserByUID(uid)
if err != nil {
return nil, err
}

return &FindUserResponse{
return &FindUserView{
ID: user.ID,
Email: user.Email,
Nickname: user.Nickname,
Expand Down
Loading

0 comments on commit c8a30fb

Please sign in to comment.