Skip to content

Commit

Permalink
fix: refactoring the sos-post query API
Browse files Browse the repository at this point in the history
  • Loading branch information
barabobBOB committed Dec 10, 2023
1 parent 459fcfe commit 599bb7c
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 172 deletions.
76 changes: 23 additions & 53 deletions cmd/server/handler/sos_post_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,21 @@ func (h *SosPostHandler) WriteSosPost(w http.ResponseWriter, r *http.Request) {
commonviews.Created(w, nil, res)
}

// findSosPostsByAuthorID godoc
// @Summary 전체 돌봄급구 게시글을 조회합니다.
// findSosPosts godoc
// @Summary 돌봄급구 게시글을 조회합니다.
// @Description
// @Tags posts
// @Accept json
// @Produce json
// @Param authorID query int false "작성자 ID"
// @Param page query int false "페이지 번호" default(1)
// @Param size query int false "페이지 사이즈" default(20)
// @Param sort_by query string false "정렬 기준" Enums(newest, deadline)
// @Security FirebaseAuth
// @Success 200 {object} commonviews.PaginatedView[sos_post.FindSosPostResponse]
// @Router /posts/sos [get]
func (h *SosPostHandler) FindSosPosts(w http.ResponseWriter, r *http.Request) {
authorIDQuery := r.URL.Query().Get("authorID")
pageQuery := r.URL.Query().Get("page")
sizeQuery := r.URL.Query().Get("size")
sortByQuery := r.URL.Query().Get("sort_by")
Expand All @@ -100,68 +102,36 @@ func (h *SosPostHandler) FindSosPosts(w http.ResponseWriter, r *http.Request) {
}
}

var sortBy string
if sortByQuery == "" {
sortBy = "newest"
} else {
sortBy = sortByQuery
}

res, err := h.sosPostService.FindSosPosts(page, size, sortBy)
if err != nil {
commonviews.InternalServerError(w, nil, err.Error())
return
}
commonviews.OK(w, nil, commonviews.NewPaginatedView(page, size, res))
}

// findSosPostsByAuthorID godoc
// @Summary 작성자 ID로 돌봄급구 게시글을 조회합니다.
// @Description
// @Tags posts
// @Accept json
// @Produce json
// @Param page query int false "페이지 번호" default(1)
// @Param size query int false "페이지 사이즈" default(20)
// @Security FirebaseAuth
// @Success 200 {object} commonviews.PaginatedView[sos_post.FindSosPostResponse]
// @Router /posts/sos/author [get]
func (h *SosPostHandler) FindSosPostsByAuthorID(w http.ResponseWriter, r *http.Request) {
foundUser, err := h.authService.VerifyAuthAndGetUser(r.Context(), r.Header.Get("Authorization"))
if err != nil {
commonviews.Unauthorized(w, nil, "unauthorized")
return
}
var res []sos_post.FindSosPostResponse

pageQuery := r.URL.Query().Get("page")
sizeQuery := r.URL.Query().Get("size")

page := 1
size := 20

if pageQuery != "" {
page, err = strconv.Atoi(pageQuery)
if authorIDQuery != "" {
var authorID int
authorID, err = strconv.Atoi(authorIDQuery)
if err != nil {
commonviews.BadRequest(w, nil, err.Error())
return
}
}

if sizeQuery != "" {
size, err = strconv.Atoi(sizeQuery)
res, err = h.sosPostService.FindSosPostsByAuthorID(authorID, page, size)
if err != nil {
commonviews.BadRequest(w, nil, err.Error())
commonviews.InternalServerError(w, nil, err.Error())
return
}
}

uid, _ := strconv.Atoi(foundUser.FirebaseUID)
} else {
var sortBy string
if sortByQuery == "" {
sortBy = "newest"
} else {
sortBy = sortByQuery
}

res, err := h.sosPostService.FindSosPostsByAuthorID(uid, page, size)
if err != nil {
commonviews.InternalServerError(w, nil, err.Error())
return
res, err = h.sosPostService.FindSosPosts(page, size, sortBy)
if err != nil {
commonviews.InternalServerError(w, nil, err.Error())
return
}
}

commonviews.OK(w, nil, commonviews.NewPaginatedView(page, size, res))
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func NewRouter(app *firebaseinfra.FirebaseApp) *chi.Mux {
sosPostService := sos_post.NewSosPostService(
postgres.NewSosPostPostgresStore(db),
postgres.NewResourceMediaPostgresStore(db),
postgres.NewUserPostgresStore(db),
)

// Initialize handlers
Expand Down Expand Up @@ -111,7 +112,6 @@ func NewRouter(app *firebaseinfra.FirebaseApp) *chi.Mux {
})
r.Route("/posts", func(r chi.Router) {
r.Post("/sos", sosPostHandler.WriteSosPost)
r.Get("/sos/author", sosPostHandler.FindSosPostsByAuthorID)
r.Get("/sos/{id}", sosPostHandler.FindSosPostByID)
r.Get("/sos", sosPostHandler.FindSosPosts)
r.Put("/sos", sosPostHandler.UpdateSosPost)
Expand Down
12 changes: 10 additions & 2 deletions internal/domain/sos_post/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ package sos_post
import (
"github.com/pet-sitter/pets-next-door-api/internal/domain/media"
"github.com/pet-sitter/pets-next-door-api/internal/domain/pet"
"github.com/pet-sitter/pets-next-door-api/internal/domain/user"
)

type SosPostService struct {
sosPostStore SosPostStore
resourceMediaStore media.ResourceMediaStore
userStore user.UserStore
}

func NewSosPostService(sosPostStore SosPostStore, resourceMediaStore media.ResourceMediaStore) *SosPostService {
func NewSosPostService(sosPostStore SosPostStore, resourceMediaStore media.ResourceMediaStore, userStore user.UserStore) *SosPostService {
return &SosPostService{
sosPostStore: sosPostStore,
resourceMediaStore: resourceMediaStore,
userStore: userStore,
}
}

func (service *SosPostService) WriteSosPost(authorID int, request *WriteSosPostRequest) (*WriteSosPostResponse, error) {
sosPost, err := service.sosPostStore.WriteSosPost(authorID, request)
userID, err := service.userStore.FindUserIDByUID(authorID)
if err != nil {
return nil, err
}

sosPost, err := service.sosPostStore.WriteSosPost(userID, request)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions internal/domain/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type UserStore interface {
CreateUser(request *RegisterUserRequest) (*User, error)
FindUserByEmail(email string) (*UserWithProfileImage, error)
FindUserByUID(uid string) (*UserWithProfileImage, error)
FindUserIDByUID(uid int) (int, error)
ExistsByNickname(nickname string) (bool, error)
FindUserStatusByEmail(email string) (*UserStatus, error)
UpdateUserByUID(uid string, nickname string, profileImageID int) (*User, error)
Expand Down
24 changes: 24 additions & 0 deletions internal/postgres/user_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,30 @@ func (s *UserPostgresStore) FindUserByUID(uid string) (*user.UserWithProfileImag
return user, nil
}

func (s *UserPostgresStore) FindUserIDByUID(uid int) (int, error) {
var UserID int

tx, _ := s.db.Begin()
err := tx.QueryRow(`
SELECT
id
FROM
users
WHERE
fb_uid = $1 AND
deleted_at IS NULL
`,
uid,
).Scan(&UserID)
tx.Commit()

if err != nil {
return 0, err
}

return UserID, nil
}

func (s *UserPostgresStore) ExistsByNickname(nickname string) (bool, error) {
var exists bool

Expand Down
51 changes: 7 additions & 44 deletions pkg/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,14 @@ const docTemplate = `{
"tags": [
"posts"
],
"summary": "전체 돌봄급구 게시글을 조회합니다.",
"summary": "돌봄급구 게시글을 조회합니다.",
"parameters": [
{
"type": "integer",
"description": "작성자 ID",
"name": "authorID",
"in": "query"
},
{
"type": "integer",
"default": 1,
Expand Down Expand Up @@ -282,49 +288,6 @@ const docTemplate = `{
}
}
},
"/posts/sos/author": {
"get": {
"security": [
{
"FirebaseAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"posts"
],
"summary": "작성자 ID로 돌봄급구 게시글을 조회합니다.",
"parameters": [
{
"type": "integer",
"default": 1,
"description": "페이지 번호",
"name": "page",
"in": "query"
},
{
"type": "integer",
"default": 20,
"description": "페이지 사이즈",
"name": "size",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/commonviews.PaginatedView-sos_post_FindSosPostResponse"
}
}
}
}
},
"/posts/sos/{id}": {
"get": {
"security": [
Expand Down
51 changes: 7 additions & 44 deletions pkg/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,14 @@
"tags": [
"posts"
],
"summary": "전체 돌봄급구 게시글을 조회합니다.",
"summary": "돌봄급구 게시글을 조회합니다.",
"parameters": [
{
"type": "integer",
"description": "작성자 ID",
"name": "authorID",
"in": "query"
},
{
"type": "integer",
"default": 1,
Expand Down Expand Up @@ -275,49 +281,6 @@
}
}
},
"/posts/sos/author": {
"get": {
"security": [
{
"FirebaseAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"posts"
],
"summary": "작성자 ID로 돌봄급구 게시글을 조회합니다.",
"parameters": [
{
"type": "integer",
"default": 1,
"description": "페이지 번호",
"name": "page",
"in": "query"
},
{
"type": "integer",
"default": 20,
"description": "페이지 사이즈",
"name": "size",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/commonviews.PaginatedView-sos_post_FindSosPostResponse"
}
}
}
}
},
"/posts/sos/{id}": {
"get": {
"security": [
Expand Down
33 changes: 5 additions & 28 deletions pkg/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ paths:
consumes:
- application/json
parameters:
- description: 작성자 ID
in: query
name: authorID
type: integer
- default: 1
description: 페이지 번호
in: query
Expand All @@ -633,7 +637,7 @@ paths:
$ref: '#/definitions/commonviews.PaginatedView-sos_post_FindSosPostResponse'
security:
- FirebaseAuth: []
summary: 전체 돌봄급구 게시글을 조회합니다.
summary: 돌봄급구 게시글을 조회합니다.
tags:
- posts
post:
Expand Down Expand Up @@ -700,33 +704,6 @@ paths:
summary: 게시글 ID로 돌봄급구 게시글을 조회합니다.
tags:
- posts
/posts/sos/author:
get:
consumes:
- application/json
parameters:
- default: 1
description: 페이지 번호
in: query
name: page
type: integer
- default: 20
description: 페이지 사이즈
in: query
name: size
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/commonviews.PaginatedView-sos_post_FindSosPostResponse'
security:
- FirebaseAuth: []
summary: 작성자 ID로 돌봄급구 게시글을 조회합니다.
tags:
- posts
/users:
post:
consumes:
Expand Down

0 comments on commit 599bb7c

Please sign in to comment.