Skip to content

Commit

Permalink
move all render to err handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Raajheer1 committed Mar 11, 2024
1 parent 04d4fd7 commit baa6d97
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 204 deletions.
30 changes: 15 additions & 15 deletions internal/v1/action-log/action_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ func NewActionLogEntryListResponse(ale []models.ActionLogEntry) []render.Rendere
func CreateActionLogEntry(w http.ResponseWriter, r *http.Request) {
data := &Request{}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

if err := data.Validate(); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

user := utils.GetUserCtx(r)

if !models.IsValidUser(user.CID) {
render.Render(w, r, utils.ErrInvalidCID)
utils.Render(w, r, utils.ErrInvalidCID)
return
}

Expand All @@ -81,12 +81,12 @@ func CreateActionLogEntry(w http.ResponseWriter, r *http.Request) {
}

if err := ale.Create(); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

render.Status(r, http.StatusCreated)
render.Render(w, r, NewActionLogEntryResponse(ale))
utils.Render(w, r, NewActionLogEntryResponse(ale))
}

// ListActionLog godoc
Expand All @@ -103,12 +103,12 @@ func CreateActionLogEntry(w http.ResponseWriter, r *http.Request) {
func ListActionLog(w http.ResponseWriter, r *http.Request) {
ale, err := models.GetAllActionLogEntriesByCID(utils.GetUserCtx(r).CID)
if err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

if err := render.RenderList(w, r, NewActionLogEntryListResponse(ale)); err != nil {
render.Render(w, r, utils.ErrRender(err))
utils.Render(w, r, utils.ErrRender(err))
return
}
}
Expand All @@ -131,24 +131,24 @@ func UpdateActionLog(w http.ResponseWriter, r *http.Request) {

data := &Request{}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

if err := data.Validate(); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

ale.Entry = data.Entry
ale.UpdatedBy = "System"

if err := ale.Update(); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

render.Render(w, r, NewActionLogEntryResponse(ale))
utils.Render(w, r, NewActionLogEntryResponse(ale))
}

// PatchActionLog godoc
Expand All @@ -170,7 +170,7 @@ func PatchActionLog(w http.ResponseWriter, r *http.Request) {

data := &Request{}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

Expand All @@ -180,11 +180,11 @@ func PatchActionLog(w http.ResponseWriter, r *http.Request) {
ale.UpdatedBy = "System"

if err := ale.Update(); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

render.Render(w, r, NewActionLogEntryResponse(ale))
utils.Render(w, r, NewActionLogEntryResponse(ale))
}

// DeleteActionLog godoc
Expand All @@ -203,7 +203,7 @@ func DeleteActionLog(w http.ResponseWriter, r *http.Request) {
ale := utils.GetActionLogCtx(r)

if err := ale.Delete(); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

Expand Down
32 changes: 16 additions & 16 deletions internal/v1/disciplinary-log/disciplinary_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ func NewDisciplinaryLogEntryListResponse(dle []models.DisciplinaryLogEntry) []re
func CreateDisciplinaryLogEntry(w http.ResponseWriter, r *http.Request) {
data := &Request{}
if err := data.Bind(r); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

if err := data.Validate(); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

user := utils.GetUserCtx(r)

if !models.IsValidUser(user.CID) {
render.Render(w, r, utils.ErrInvalidCID)
utils.Render(w, r, utils.ErrInvalidCID)
return
}

Expand All @@ -89,12 +89,12 @@ func CreateDisciplinaryLogEntry(w http.ResponseWriter, r *http.Request) {
}

if err := dle.Create(); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

render.Status(r, http.StatusCreated)
render.Render(w, r, NewDisciplinaryLogEntryResponse(dle))
utils.Render(w, r, NewDisciplinaryLogEntryResponse(dle))
}

// ListDisciplinaryLog godoc
Expand All @@ -118,18 +118,18 @@ func ListDisciplinaryLog(w http.ResponseWriter, r *http.Request) {

user := utils.GetUserCtx(r)
if !models.IsValidUser(user.CID) {
render.Render(w, r, utils.ErrInvalidCID)
utils.Render(w, r, utils.ErrInvalidCID)
return
}

dle, err := models.GetAllDisciplinaryLogEntriesByCID(user.CID, vatUSA)
if err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

if err := render.RenderList(w, r, NewDisciplinaryLogEntryListResponse(dle)); err != nil {
render.Render(w, r, utils.ErrRender(err))
utils.Render(w, r, utils.ErrRender(err))
return
}
}
Expand All @@ -150,12 +150,12 @@ func UpdateDisciplinaryLog(w http.ResponseWriter, r *http.Request) {
dle := utils.GetDisciplinaryLogCtx(r)
data := &Request{}
if err := data.Bind(r); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

if err := data.Validate(); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

Expand All @@ -166,11 +166,11 @@ func UpdateDisciplinaryLog(w http.ResponseWriter, r *http.Request) {
}

if err := dle.Update(); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

render.Render(w, r, NewDisciplinaryLogEntryResponse(dle))
utils.Render(w, r, NewDisciplinaryLogEntryResponse(dle))
}

// PatchDisciplinaryLog godoc
Expand All @@ -190,7 +190,7 @@ func PatchDisciplinaryLog(w http.ResponseWriter, r *http.Request) {
dle := utils.GetDisciplinaryLogCtx(r)
data := &Request{}
if err := data.Bind(r); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

Expand All @@ -203,11 +203,11 @@ func PatchDisciplinaryLog(w http.ResponseWriter, r *http.Request) {
}

if err := dle.Update(); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

render.Render(w, r, NewDisciplinaryLogEntryResponse(dle))
utils.Render(w, r, NewDisciplinaryLogEntryResponse(dle))
}

// DeleteDisciplinaryLog godoc
Expand All @@ -224,7 +224,7 @@ func PatchDisciplinaryLog(w http.ResponseWriter, r *http.Request) {
func DeleteDisciplinaryLog(w http.ResponseWriter, r *http.Request) {
dle := utils.GetDisciplinaryLogCtx(r)
if err := dle.Delete(); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

Expand Down
28 changes: 14 additions & 14 deletions internal/v1/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,24 @@ func NewDocumentListResponse(d []models.Document) []render.Renderer {
func CreateDocument(w http.ResponseWriter, r *http.Request, endpoint string) {
data := &Request{}
if err := data.Bind(r); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

if err := data.Validate(); err != nil {
render.Render(w, r, utils.ErrInvalidRequest(err))
utils.Render(w, r, utils.ErrInvalidRequest(err))
return
}

currentDocs, err := models.GetAllDocumentsByFacilityAndCategory(data.Facility, types.DocumentCategory(data.Category))
if err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

for _, doc := range currentDocs {
if doc.Name == data.Name {
render.Render(w, r, utils.ErrInvalidRequest(fmt.Errorf("document with name %s already exists", data.Name)))
utils.Render(w, r, utils.ErrInvalidRequest(fmt.Errorf("document with name %s already exists", data.Name)))
return
}
}
Expand All @@ -103,7 +103,7 @@ func CreateDocument(w http.ResponseWriter, r *http.Request, endpoint string) {

// Put the file in the S3 bucket
if err := storage.PublicBucket.Upload(directory, filename, file); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

Expand All @@ -116,7 +116,7 @@ func CreateDocument(w http.ResponseWriter, r *http.Request, endpoint string) {
}

if err := document.Create(); err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)

err := storage.PublicBucket.Delete(directory, filename)
if err != nil {
Expand All @@ -126,7 +126,7 @@ func CreateDocument(w http.ResponseWriter, r *http.Request, endpoint string) {
}

render.Status(r, http.StatusCreated)
render.Render(w, r, NewDocumentResponse(document))
utils.Render(w, r, NewDocumentResponse(document))
}

// GetDocument godoc
Expand All @@ -143,7 +143,7 @@ func CreateDocument(w http.ResponseWriter, r *http.Request, endpoint string) {
// @Router /documents/{id} [get]
func GetDocument(w http.ResponseWriter, r *http.Request) {
doc := GetDocumentCtx(r)
render.Render(w, r, NewDocumentResponse(doc))
utils.Render(w, r, NewDocumentResponse(doc))
}

// ListDocuments godoc
Expand All @@ -159,12 +159,12 @@ func GetDocument(w http.ResponseWriter, r *http.Request) {
func ListDocuments(w http.ResponseWriter, r *http.Request) {
docs, err := models.GetAllDocuments()
if err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

if err := render.RenderList(w, r, NewDocumentListResponse(docs)); err != nil {
render.Render(w, r, utils.ErrRender(err))
utils.Render(w, r, utils.ErrRender(err))
return
}
}
Expand All @@ -190,12 +190,12 @@ func ListDocumentsByFac(w http.ResponseWriter, r *http.Request) {

docs, err := models.GetAllDocumentsByFacility(facId)
if err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

if err := render.RenderList(w, r, NewDocumentListResponse(docs)); err != nil {
render.Render(w, r, utils.ErrRender(err))
utils.Render(w, r, utils.ErrRender(err))
return
}
}
Expand Down Expand Up @@ -223,12 +223,12 @@ func ListDocumentsByFacByCat(w http.ResponseWriter, r *http.Request) {

docs, err := models.GetAllDocumentsByFacilityAndCategory(facId, types.DocumentCategory(cat))
if err != nil {
render.Render(w, r, utils.ErrInternalServer)
utils.Render(w, r, utils.ErrInternalServer)
return
}

if err := render.RenderList(w, r, NewDocumentListResponse(docs)); err != nil {
render.Render(w, r, utils.ErrRender(err))
utils.Render(w, r, utils.ErrRender(err))
return
}
}
Expand Down
Loading

0 comments on commit baa6d97

Please sign in to comment.