Skip to content

Commit

Permalink
Merge pull request #22 from go-park-mail-ru/TP-6ec_easyjson
Browse files Browse the repository at this point in the history
TP-6ec_easyjson: generated easyjson, changed serialization for user, …
  • Loading branch information
Gvidow authored Dec 19, 2023
2 parents ea2a4d0 + 24e85bd commit 5fcf9c3
Show file tree
Hide file tree
Showing 23 changed files with 1,913 additions and 364 deletions.
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b h1:slYM766cy2nI3BwyRiyQj/Ud48djTMtMebDqepE95rw=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
Expand Down Expand Up @@ -51,6 +53,7 @@ github.com/go-fonts/liberation v0.3.1 h1:9RPT2NhUpxQ7ukUvz3jeUckmN42T9D9TpjtQcqK
github.com/go-gorp/gorp v2.0.0+incompatible h1:dIQPsBtl6/H1MjVseWuWPXa7ET4p6Dve4j3Hg+UjqYw=
github.com/go-gorp/gorp v2.0.0+incompatible/go.mod h1:7IfkAQnO7jfT/9IQ3R9wL1dFhukN6aQxzKTHnkxzA/E=
github.com/go-latex/latex v0.0.0-20230307184459-12ec69307ad9 h1:NxXI5pTAtpEaU49bpLpQoDsu1zrteW/vxzTz8Cd2UAs=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
Expand Down Expand Up @@ -119,6 +122,9 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
Expand All @@ -145,6 +151,9 @@ github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvls
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pashagolub/pgxmock/v2 v2.12.0 h1:IVRmQtVFNCoq7NOZ+PdfvB6fwnLJmEuWDhnc3yrDxBs=
github.com/pashagolub/pgxmock/v2 v2.12.0/go.mod h1:D3YslkN/nJ4+umVqWmbwfSXugJIjPMChkGBG47OJpNw=
Expand Down
140 changes: 43 additions & 97 deletions internal/pkg/delivery/http/v1/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,23 @@ package v1

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

"github.com/go-chi/chi/v5"
entity "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/entity/board"
"github.com/mailru/easyjson"

errHTTP "github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/delivery/http/v1/errors"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/delivery/http/v1/structs"
"github.com/go-park-mail-ru/2023_2_OND_team/internal/pkg/middleware/auth"
log "github.com/go-park-mail-ru/2023_2_OND_team/pkg/logger"
)

var TimeFormat = "2006-01-02"

// data for board creation/update
type BoardData struct {
Title *string `json:"title" example:"new board"`
Description *string `json:"description" example:"long desc"`
Public *bool `json:"public" example:"true"`
Tags []string `json:"tags" example:"['blue', 'car']"`
}

// board view for delivery layer
type CertainBoard struct {
ID int `json:"board_id" example:"22"`
AuthorID int `json:"author_id" example:"22"`
Title string `json:"title" example:"new board"`
Description string `json:"description" example:"long desc"`
CreatedAt string `json:"created_at" example:"07-11-2023"`
PinsNumber int `json:"pins_number" example:"12"`
Pins []string `json:"pins" example:"['/pic1', '/pic2']"`
Tags []string `json:"tags" example:"['love', 'green']"`
}

type CertainBoardWithUsername struct {
ID int `json:"board_id" example:"22"`
AuthorID int `json:"author_id" example:"22"`
AuthorUsername string `json:"author_username" example:"Bob"`
Title string `json:"title" example:"new board"`
Description string `json:"description" example:"long desc"`
CreatedAt string `json:"created_at" example:"07-11-2023"`
PinsNumber int `json:"pins_number" example:"12"`
Pins []string `json:"pins" example:"['/pic1', '/pic2']"`
Tags []string `json:"tags" example:"['love', 'green']"`
}

type DeletePinFromBoard struct {
PinID int `json:"pin_id" example:"22"`
}

func ToCertainBoardFromService(board entity.BoardWithContent) CertainBoard {
return CertainBoard{
func ToCertainBoardFromService(board entity.BoardWithContent) structs.CertainBoard {
return structs.CertainBoard{
ID: board.BoardInfo.ID,
AuthorID: board.BoardInfo.AuthorID,
Title: board.BoardInfo.Title,
Expand All @@ -64,8 +30,8 @@ func ToCertainBoardFromService(board entity.BoardWithContent) CertainBoard {
}
}

func ToCertainBoardUsernameFromService(board entity.BoardWithContent, username string) CertainBoardWithUsername {
return CertainBoardWithUsername{
func ToCertainBoardUsernameFromService(board entity.BoardWithContent, username string) structs.CertainBoardWithUsername {
return structs.CertainBoardWithUsername{
ID: board.BoardInfo.ID,
AuthorID: board.BoardInfo.AuthorID,
AuthorUsername: username,
Expand All @@ -78,48 +44,28 @@ func ToCertainBoardUsernameFromService(board entity.BoardWithContent, username s
}
}

func (data *BoardData) Validate() error {
if data.Title == nil || *data.Title == "" {
return ErrInvalidBoardTitle
}
if data.Description == nil {
data.Description = new(string)
*data.Description = ""
}
if data.Public == nil {
return ErrEmptyPubOpt
}
if !isValidBoardTitle(*data.Title) {
return ErrInvalidBoardTitle
}
if err := checkIsValidTagTitles(data.Tags); err != nil {
return fmt.Errorf("%s: %w", err.Error(), ErrInvalidTagTitles)
}
return nil
}

func (h *HandlerHTTP) CreateNewBoard(w http.ResponseWriter, r *http.Request) {
logger := h.getRequestLogger(r)
if contentType := r.Header.Get("Content-Type"); contentType != ApplicationJson {
code, message := getErrCodeMessage(ErrBadContentType)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadContentType)
responseError(w, code, message)
return
}

var newBoard BoardData
err := json.NewDecoder(r.Body).Decode(&newBoard)
var newBoard structs.BoardData
err := easyjson.UnmarshalFromReader(r.Body, &newBoard)
defer r.Body.Close()
if err != nil {
logger.Info("create board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(ErrBadBody)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadBody)
responseError(w, code, message)
return
}

err = newBoard.Validate()
if err != nil {
logger.Info("create board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(err)
code, message := errHTTP.GetErrCodeMessage(err)
responseError(w, code, message)
return
}
Expand All @@ -140,7 +86,7 @@ func (h *HandlerHTTP) CreateNewBoard(w http.ResponseWriter, r *http.Request) {

if err != nil {
logger.Info("create board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(err)
code, message := errHTTP.GetErrCodeMessage(err)
responseError(w, code, message)
return
}
Expand All @@ -149,7 +95,7 @@ func (h *HandlerHTTP) CreateNewBoard(w http.ResponseWriter, r *http.Request) {
if err != nil {
logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(ErrInternalError.Error()))
w.Write([]byte(errHTTP.ErrInternalError.Error()))
}
}

Expand All @@ -158,29 +104,29 @@ func (h *HandlerHTTP) GetUserBoards(w http.ResponseWriter, r *http.Request) {

username := chi.URLParam(r, "username")
if !isValidUsername(username) {
logger.Info("update board", log.F{"message", ErrInvalidUsername.Error()})
code, message := getErrCodeMessage(ErrInvalidUsername)
logger.Info("update board", log.F{"message", errHTTP.ErrInvalidUsername.Error()})
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrInvalidUsername)
responseError(w, code, message)
return
}

boards, err := h.boardCase.GetBoardsByUsername(r.Context(), username)
if err != nil {
logger.Info("get user boards", log.F{"message", err.Error()})
code, message := getErrCodeMessage(err)
code, message := errHTTP.GetErrCodeMessage(err)
responseError(w, code, message)
return
}

userBoards := make([]CertainBoard, 0, len(boards))
userBoards := make([]structs.CertainBoard, 0, len(boards))
for _, board := range boards {
userBoards = append(userBoards, ToCertainBoardFromService(board))
}
err = responseOk(http.StatusOK, w, "got user boards successfully", userBoards)
if err != nil {
logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(ErrInternalError.Error()))
w.Write([]byte(errHTTP.ErrInternalError.Error()))
}
}

Expand All @@ -190,15 +136,15 @@ func (h *HandlerHTTP) GetCertainBoard(w http.ResponseWriter, r *http.Request) {
boardID, err := strconv.ParseInt(chi.URLParam(r, "boardID"), 10, 64)
if err != nil {
logger.Info("get certain board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(ErrBadUrlParam)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadUrlParam)
responseError(w, code, message)
return
}

board, username, err := h.boardCase.GetCertainBoard(r.Context(), int(boardID))
if err != nil {
logger.Info("get certain board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(err)
code, message := errHTTP.GetErrCodeMessage(err)
responseError(w, code, message)
return
}
Expand All @@ -207,7 +153,7 @@ func (h *HandlerHTTP) GetCertainBoard(w http.ResponseWriter, r *http.Request) {
if err != nil {
logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(ErrInternalError.Error()))
w.Write([]byte(errHTTP.ErrInternalError.Error()))
}
}

Expand All @@ -217,15 +163,15 @@ func (h *HandlerHTTP) GetBoardInfoForUpdate(w http.ResponseWriter, r *http.Reque
boardID, err := strconv.ParseInt(chi.URLParam(r, "boardID"), 10, 64)
if err != nil {
logger.Info("get certain board info for update", log.F{"message", err.Error()})
code, message := getErrCodeMessage(ErrBadUrlParam)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadUrlParam)
responseError(w, code, message)
return
}

board, tagTitles, err := h.boardCase.GetBoardInfoForUpdate(r.Context(), int(boardID))
if err != nil {
logger.Info("get certain board info for update", log.F{"message", err.Error()})
code, message := getErrCodeMessage(err)
code, message := errHTTP.GetErrCodeMessage(err)
responseError(w, code, message)
return
}
Expand All @@ -234,40 +180,40 @@ func (h *HandlerHTTP) GetBoardInfoForUpdate(w http.ResponseWriter, r *http.Reque
if err != nil {
logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(ErrInternalError.Error()))
w.Write([]byte(errHTTP.ErrInternalError.Error()))
}
}

func (h *HandlerHTTP) UpdateBoardInfo(w http.ResponseWriter, r *http.Request) {
logger := h.getRequestLogger(r)
if contentType := r.Header.Get("Content-Type"); contentType != ApplicationJson {
code, message := getErrCodeMessage(ErrBadContentType)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadContentType)
responseError(w, code, message)
return
}

boardID, err := strconv.ParseInt(chi.URLParam(r, "boardID"), 10, 64)
if err != nil {
logger.Info("update certain board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(ErrBadUrlParam)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadUrlParam)
responseError(w, code, message)
return
}

var updatedData BoardData
err = json.NewDecoder(r.Body).Decode(&updatedData)
var updatedData structs.BoardData
err = easyjson.UnmarshalFromReader(r.Body, &updatedData)
defer r.Body.Close()
if err != nil {
logger.Info("update certain board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(ErrBadBody)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadBody)
responseError(w, code, message)
return
}

err = updatedData.Validate()
if err != nil {
logger.Info("update certain board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(err)
code, message := errHTTP.GetErrCodeMessage(err)
responseError(w, code, message)
return
}
Expand All @@ -286,7 +232,7 @@ func (h *HandlerHTTP) UpdateBoardInfo(w http.ResponseWriter, r *http.Request) {
err = h.boardCase.UpdateBoardInfo(r.Context(), updatedBoard, tagTitles)
if err != nil {
logger.Info("update certain board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(err)
code, message := errHTTP.GetErrCodeMessage(err)
responseError(w, code, message)
return
}
Expand All @@ -295,7 +241,7 @@ func (h *HandlerHTTP) UpdateBoardInfo(w http.ResponseWriter, r *http.Request) {
if err != nil {
logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(ErrInternalError.Error()))
w.Write([]byte(errHTTP.ErrInternalError.Error()))
}
}

Expand All @@ -305,15 +251,15 @@ func (h *HandlerHTTP) DeleteBoard(w http.ResponseWriter, r *http.Request) {
boardID, err := strconv.ParseInt(chi.URLParam(r, "boardID"), 10, 64)
if err != nil {
logger.Info("update certain board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(ErrBadUrlParam)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadUrlParam)
responseError(w, code, message)
return
}

err = h.boardCase.DeleteCertainBoard(r.Context(), int(boardID))
if err != nil {
logger.Info("update certain board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(err)
code, message := errHTTP.GetErrCodeMessage(err)
responseError(w, code, message)
return
}
Expand All @@ -322,7 +268,7 @@ func (h *HandlerHTTP) DeleteBoard(w http.ResponseWriter, r *http.Request) {
if err != nil {
logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(ErrInternalError.Error()))
w.Write([]byte(errHTTP.ErrInternalError.Error()))
}
}

Expand Down Expand Up @@ -390,21 +336,21 @@ func (h *HandlerHTTP) DeletePinFromBoard(w http.ResponseWriter, r *http.Request)
boardID, err := strconv.ParseInt(chi.URLParam(r, "boardID"), 10, 64)
if err != nil {
logger.Info("delete pin from board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(ErrBadUrlParam)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadUrlParam)
responseError(w, code, message)
return
}

if contentType := r.Header.Get("Content-Type"); contentType != ApplicationJson {
code, message := getErrCodeMessage(ErrBadContentType)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadContentType)
responseError(w, code, message)
return
}

delPinFromBoard := DeletePinFromBoard{}
err = json.NewDecoder(r.Body).Decode(&delPinFromBoard)
delPinFromBoard := structs.DeletePinFromBoard{}
err = easyjson.UnmarshalFromReader(r.Body, &delPinFromBoard)
if err != nil {
code, message := getErrCodeMessage(ErrBadBody)
code, message := errHTTP.GetErrCodeMessage(errHTTP.ErrBadBody)
responseError(w, code, message)
return
}
Expand All @@ -413,7 +359,7 @@ func (h *HandlerHTTP) DeletePinFromBoard(w http.ResponseWriter, r *http.Request)
err = h.boardCase.DeletePinFromBoard(r.Context(), int(boardID), delPinFromBoard.PinID)
if err != nil {
logger.Info("delete pin from board", log.F{"message", err.Error()})
code, message := getErrCodeMessage(err)
code, message := errHTTP.GetErrCodeMessage(err)
responseError(w, code, message)
return
}
Expand All @@ -422,6 +368,6 @@ func (h *HandlerHTTP) DeletePinFromBoard(w http.ResponseWriter, r *http.Request)
if err != nil {
logger.Error(err.Error())
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(ErrInternalError.Error()))
w.Write([]byte(errHTTP.ErrInternalError.Error()))
}
}
Loading

0 comments on commit 5fcf9c3

Please sign in to comment.