Skip to content

Commit

Permalink
Update categories.go
Browse files Browse the repository at this point in the history
  • Loading branch information
kervyntan committed Sep 28, 2024
1 parent 5ed4852 commit cb175f4
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions peer-prep-be/src/controllers/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -98,13 +100,16 @@ 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 {
// A new category was created
return c.JSON(http.StatusCreated, responses.StatusResponse{
Status: http.StatusCreated,
Message: "Category created successfully",
Data: &echo.Map{"data": updateResult},
})
}

Expand Down Expand Up @@ -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{
Expand All @@ -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},
})
}

Expand Down

0 comments on commit cb175f4

Please sign in to comment.