From e4086227263771ccff6db477da918de29438bdb0 Mon Sep 17 00:00:00 2001 From: mhmmdriz Date: Mon, 24 Jun 2024 11:25:34 +0700 Subject: [PATCH 1/2] Update Get & Get by ID Response --- controllers/category/category.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/controllers/category/category.go b/controllers/category/category.go index 66f1439..406c239 100644 --- a/controllers/category/category.go +++ b/controllers/category/category.go @@ -28,7 +28,12 @@ func (cc *CategoryController) GetAll(c echo.Context) error { return c.JSON(utils.ConvertResponseCode(err), base.NewErrorResponse(err.Error())) } - return c.JSON(http.StatusOK, base.NewSuccessResponse("Success get all categories", categories)) + responseCategories := make([]*response.Get, len(categories)) + for i, category := range categories { + responseCategories[i] = response.GetFromEntitiesToResponse(&category) + } + + return c.JSON(http.StatusOK, base.NewSuccessResponse("Success get all categories", responseCategories)) } func (cc *CategoryController) GetByID(c echo.Context) error { @@ -42,10 +47,6 @@ func (cc *CategoryController) GetByID(c echo.Context) error { return c.JSON(http.StatusInternalServerError, base.NewErrorResponse(err.Error())) } - if err != nil { - return c.JSON(http.StatusBadRequest, base.NewErrorResponse(err.Error())) - } - category, err := cc.useCase.GetByID(id) if err != nil { if errors.Is(err, constants.ErrCategoryNotFound) { @@ -54,7 +55,9 @@ func (cc *CategoryController) GetByID(c echo.Context) error { return c.JSON(utils.ConvertResponseCode(err), base.NewErrorResponse(err.Error())) } - return c.JSON(http.StatusOK, base.NewSuccessResponse("Success get category by ID", category)) + responseCategory := response.GetFromEntitiesToResponse(&category) + + return c.JSON(http.StatusOK, base.NewSuccessResponse("Success get category by ID", responseCategory)) } func (cc *CategoryController) CreateCategory(c echo.Context) error { From 2609e25e68224863f004247256eb796578269e7d Mon Sep 17 00:00:00 2001 From: mhmmdriz Date: Mon, 24 Jun 2024 12:00:30 +0700 Subject: [PATCH 2/2] Update Response --- controllers/regency/regency.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/controllers/regency/regency.go b/controllers/regency/regency.go index 19e276b..bb6fee4 100644 --- a/controllers/regency/regency.go +++ b/controllers/regency/regency.go @@ -2,6 +2,7 @@ package regency import ( "e-complaint-api/controllers/base" + "e-complaint-api/controllers/regency/response" "e-complaint-api/entities" "net/http" @@ -22,5 +23,10 @@ func (rc *RegencyController) GetAll(c echo.Context) error { return c.JSON(http.StatusInternalServerError, err.Error()) } - return c.JSON(http.StatusOK, base.NewSuccessResponse("Success get all regencies", regencies)) + var responseRegencies []*response.Regency + for _, regency := range regencies { + responseRegencies = append(responseRegencies, response.FromEntitiesToResponse(®ency)) + } + + return c.JSON(http.StatusOK, base.NewSuccessResponse("Success get all regencies", responseRegencies)) }