From b8006c57df3e164240db0998fa2df6a9408621ab Mon Sep 17 00:00:00 2001 From: ajikamaludin Date: Wed, 20 Jul 2022 14:47:28 +0700 Subject: [PATCH] make response more consitant --- app/handlers/note.handler.go | 35 +++++++++++++++++++++++++++----- pkg/utils/constants/constants.go | 2 ++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/handlers/note.handler.go b/app/handlers/note.handler.go index 0695119..5f7d857 100644 --- a/app/handlers/note.handler.go +++ b/app/handlers/note.handler.go @@ -3,6 +3,7 @@ package handlers import ( "github.com/ajikamaludin/go-fiber-rest/app/models" gormdb "github.com/ajikamaludin/go-fiber-rest/pkg/gorm.db" + "github.com/ajikamaludin/go-fiber-rest/pkg/utils/constants" "github.com/ajikamaludin/go-fiber-rest/pkg/utils/validator" "github.com/gofiber/fiber/v2" ) @@ -17,7 +18,11 @@ func GetAllNotes(c *fiber.Ctx) error { db.Find(¬es) - return c.Status(fiber.StatusOK).JSON(notes) + return c.Status(fiber.StatusOK).JSON(fiber.Map{ + "status": constants.STATUS_SUCCESS, + "message": "Ok", + "data": notes, + }) } func GetNoteById(c *fiber.Ctx) error { @@ -34,11 +39,16 @@ func GetNoteById(c *fiber.Ctx) error { if err != nil { return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ + "status": constants.STATUS_FAIL, "message": "note not found", }) } - return c.Status(fiber.StatusOK).JSON(note) + return c.Status(fiber.StatusOK).JSON(fiber.Map{ + "status": constants.STATUS_OK, + "message": "note found", + "data": note, + }) } func CreateNote(c *fiber.Ctx) error { @@ -68,7 +78,11 @@ func CreateNote(c *fiber.Ctx) error { db.Create(¬e) - return c.Status(fiber.StatusCreated).JSON(note) + return c.Status(fiber.StatusCreated).JSON(fiber.Map{ + "status": constants.STATUS_OK, + "message": "note created", + "data": note, + }) } func UpdateNote(c *fiber.Ctx) error { @@ -77,13 +91,18 @@ func UpdateNote(c *fiber.Ctx) error { if err := c.BodyParser(¬eRequest); err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "status": constants.STATUS_FAIL, "message": err.Error(), }) } errors := validator.ValidateRequest(noteRequest) if errors != nil { - return c.Status(fiber.StatusUnprocessableEntity).JSON(errors) + return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{ + "status": constants.STATUS_FAIL, + "message": "error", + "errors": errors, + }) } // find records @@ -99,6 +118,7 @@ func UpdateNote(c *fiber.Ctx) error { if err != nil { return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ + "status": constants.STATUS_FAIL, "message": "note not found", }) } @@ -106,7 +126,11 @@ func UpdateNote(c *fiber.Ctx) error { // Update db.Model(¬e).Updates(noteRequest) - return c.Status(fiber.StatusCreated).JSON(note) + return c.Status(fiber.StatusCreated).JSON(fiber.Map{ + "status": constants.STATUS_OK, + "message": "note updated", + "data": note, + }) } func DeleteNote(c *fiber.Ctx) error { @@ -123,6 +147,7 @@ func DeleteNote(c *fiber.Ctx) error { if err != nil { return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ + "status": constants.STATUS_FAIL, "message": "note not found", }) } diff --git a/pkg/utils/constants/constants.go b/pkg/utils/constants/constants.go index 9c2fe94..ce2e7a3 100644 --- a/pkg/utils/constants/constants.go +++ b/pkg/utils/constants/constants.go @@ -1,3 +1,5 @@ package constants +const STATUS_SUCCESS = true +const STATUS_FAIL = false const STATUS_OK = "Ok"