From cb175f4afb1883235e994b5a41183ba06f1764e3 Mon Sep 17 00:00:00 2001 From: Kervyn <73232587+kervyntan@users.noreply.github.com> Date: Sat, 28 Sep 2024 14:42:13 +0800 Subject: [PATCH] Update categories.go --- peer-prep-be/src/controllers/categories.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/peer-prep-be/src/controllers/categories.go b/peer-prep-be/src/controllers/categories.go index 07db0b9060..60731081f4 100644 --- a/peer-prep-be/src/controllers/categories.go +++ b/peer-prep-be/src/controllers/categories.go @@ -74,8 +74,10 @@ func CreateCategory(c echo.Context) error { // return c.JSON(http.StatusBadRequest, responses.StatusResponse{Message: "Category already exists"}) // } + newCategoryId := primitive.NewObjectID() + newCategory := models.Category{ - Category_id: primitive.NewObjectID(), + Category_id: newCategoryId, Category_name: category.Category_name, } @@ -98,6 +100,8 @@ func CreateCategory(c echo.Context) error { Message: err.Error(), }) } + + updateResult.UpsertedID = newCategoryId; // Check if a new category was created or if it was already existing if updateResult.UpsertedCount > 0 { @@ -105,6 +109,7 @@ func CreateCategory(c echo.Context) error { return c.JSON(http.StatusCreated, responses.StatusResponse{ Status: http.StatusCreated, Message: "Category created successfully", + Data: &echo.Map{"data": updateResult}, }) } @@ -143,7 +148,7 @@ func UpdateCategory(c echo.Context) error { } // Perform the Update operation - _, err = categoriesCollection.UpdateOne(ctx, bson.M{"_id": categoryId}, update) + updateResult, err := categoriesCollection.UpdateOne(ctx, bson.M{"_id": categoryId}, update) if err != nil { return c.JSON(http.StatusInternalServerError, responses.StatusResponse{ @@ -155,6 +160,7 @@ func UpdateCategory(c echo.Context) error { return c.JSON(http.StatusOK, responses.StatusResponse{ Status: http.StatusOK, Message: "Category updated successfully", + Data: &echo.Map{"data": updateResult}, }) }