Skip to content

Commit

Permalink
🌱 Sync with docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dumindu committed Oct 27, 2023
1 parent 395c160 commit 6413f86
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
44 changes: 16 additions & 28 deletions api/resource/book/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (a *API) List(w http.ResponseWriter, r *http.Request) {
books, err := a.repository.List()
if err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.DataAccessFailure)
e.ServerError(w, e.RespDBDataAccessFailure)
return
}

Expand All @@ -54,7 +54,7 @@ func (a *API) List(w http.ResponseWriter, r *http.Request) {

if err := json.NewEncoder(w).Encode(books.ToDto()); err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.JsonEncodingFailure)
e.ServerError(w, e.RespJSONEncodeFailure)
return
}
}
Expand All @@ -76,21 +76,15 @@ func (a *API) Create(w http.ResponseWriter, r *http.Request) {
form := &Form{}
if err := json.NewDecoder(r.Body).Decode(form); err != nil {
a.logger.Error().Err(err).Msg("")
e.BadRequest(w, e.JsonDecodingFailure)
e.BadRequest(w, e.RespJSONDecodeFailure)
return
}

if err := a.validator.Struct(form); err != nil {
resp := validatorUtil.ToErrResponse(err)
if resp == nil {
e.ServerError(w, e.FormErrResponseFailure)
return
}

respBody, err := json.Marshal(resp)
respBody, err := json.Marshal(validatorUtil.ToErrResponse(err))
if err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.JsonEncodingFailure)
e.ServerError(w, e.RespJSONEncodeFailure)
return
}

Expand All @@ -104,7 +98,7 @@ func (a *API) Create(w http.ResponseWriter, r *http.Request) {
book, err := a.repository.Create(newBook)
if err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.DataCreationFailure)
e.ServerError(w, e.RespDBDataInsertFailure)
return
}

Expand All @@ -128,7 +122,7 @@ func (a *API) Create(w http.ResponseWriter, r *http.Request) {
func (a *API) Read(w http.ResponseWriter, r *http.Request) {
id, err := uuid.Parse(chi.URLParam(r, "id"))
if err != nil {
e.BadRequest(w, e.InvalidIdInUrlParam)
e.BadRequest(w, e.RespInvalidURLParamID)
return
}

Expand All @@ -140,14 +134,14 @@ func (a *API) Read(w http.ResponseWriter, r *http.Request) {
}

a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.DataAccessFailure)
e.ServerError(w, e.RespDBDataAccessFailure)
return
}

dto := book.ToDto()
if err := json.NewEncoder(w).Encode(dto); err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.JsonEncodingFailure)
e.ServerError(w, e.RespJSONEncodeFailure)
return
}
}
Expand All @@ -170,28 +164,22 @@ func (a *API) Read(w http.ResponseWriter, r *http.Request) {
func (a *API) Update(w http.ResponseWriter, r *http.Request) {
id, err := uuid.Parse(chi.URLParam(r, "id"))
if err != nil {
e.BadRequest(w, e.InvalidIdInUrlParam)
e.BadRequest(w, e.RespInvalidURLParamID)
return
}

form := &Form{}
if err := json.NewDecoder(r.Body).Decode(form); err != nil {
a.logger.Error().Err(err).Msg("")
e.BadRequest(w, e.JsonDecodingFailure)
e.BadRequest(w, e.RespJSONDecodeFailure)
return
}

if err := a.validator.Struct(form); err != nil {
resp := validatorUtil.ToErrResponse(err)
if resp == nil {
e.ServerError(w, e.FormErrResponseFailure)
return
}

respBody, err := json.Marshal(resp)
respBody, err := json.Marshal(validatorUtil.ToErrResponse(err))
if err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.JsonEncodingFailure)
e.ServerError(w, e.RespJSONEncodeFailure)
return
}

Expand All @@ -205,7 +193,7 @@ func (a *API) Update(w http.ResponseWriter, r *http.Request) {
rows, err := a.repository.Update(book)
if err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.DataUpdateFailure)
e.ServerError(w, e.RespDBDataUpdateFailure)
return
}
if rows == 0 {
Expand All @@ -232,14 +220,14 @@ func (a *API) Update(w http.ResponseWriter, r *http.Request) {
func (a *API) Delete(w http.ResponseWriter, r *http.Request) {
id, err := uuid.Parse(chi.URLParam(r, "id"))
if err != nil {
e.BadRequest(w, e.InvalidIdInUrlParam)
e.BadRequest(w, e.RespInvalidURLParamID)
return
}

rows, err := a.repository.Delete(id)
if err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.DataDeletionFailure)
e.ServerError(w, e.RespDBDataRemoveFailure)
return
}
if rows == 0 {
Expand Down
27 changes: 12 additions & 15 deletions api/resource/common/err/err.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package err

import (
"fmt"
"net/http"
)

const (
DataCreationFailure = "data creation failure"
DataAccessFailure = "data access failure"
DataUpdateFailure = "data update failure"
DataDeletionFailure = "data deletion failure"
var (
RespDBDataInsertFailure = []byte(`{"error": "db data insert failure"}`)
RespDBDataAccessFailure = []byte(`{"error": "db data access failure"}`)
RespDBDataUpdateFailure = []byte(`{"error": "db data update failure"}`)
RespDBDataRemoveFailure = []byte(`{"error": "db data remove failure"}`)

JsonEncodingFailure = "json encoding failure"
JsonDecodingFailure = "json decoding failure"
RespJSONEncodeFailure = []byte(`{"error": "json encode failure"}`)
RespJSONDecodeFailure = []byte(`{"error": "json decode failure"}`)

FormErrResponseFailure = "form error response failure"

InvalidIdInUrlParam = "invalid id in url param"
RespInvalidURLParamID = []byte(`{"error": "invalid url param-id"}`)
)

type Error struct {
Expand All @@ -27,14 +24,14 @@ type Errors struct {
Errors []string `json:"errors"`
}

func ServerError(w http.ResponseWriter, msg string) {
func ServerError(w http.ResponseWriter, error []byte) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{"error": "%v"}`, msg)
w.Write(error)
}

func BadRequest(w http.ResponseWriter, msg string) {
func BadRequest(w http.ResponseWriter, error []byte) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"error": "%v"}`, msg)
w.Write(error)
}

func ValidationErrors(w http.ResponseWriter, reps []byte) {
Expand Down

0 comments on commit 6413f86

Please sign in to comment.