-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding categories CRUD for admin
- Loading branch information
1 parent
edf1abf
commit 313c4ea
Showing
14 changed files
with
798 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package api_admin | ||
|
||
import ( | ||
"gcstatus/internal/adapters/api" | ||
"gcstatus/internal/domain" | ||
"gcstatus/internal/errors" | ||
ports_admin "gcstatus/internal/ports/admin" | ||
"gcstatus/internal/resources" | ||
usecases_admin "gcstatus/internal/usecases/admin" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type AdminCategoryHandler struct { | ||
categoryService *usecases_admin.AdminCategoryService | ||
} | ||
|
||
func NewAdminCategoryHandler( | ||
categoryService *usecases_admin.AdminCategoryService, | ||
) *AdminCategoryHandler { | ||
return &AdminCategoryHandler{ | ||
categoryService: categoryService, | ||
} | ||
} | ||
|
||
func (h *AdminCategoryHandler) GetAll(c *gin.Context) { | ||
categories, err := h.categoryService.GetAll() | ||
if err != nil { | ||
api.RespondWithError(c, http.StatusInternalServerError, "Failed to fetch categories: "+err.Error()) | ||
return | ||
} | ||
|
||
transformedCategories := resources.TransformCategories(categories) | ||
|
||
response := resources.Response{ | ||
Data: transformedCategories, | ||
} | ||
|
||
c.JSON(http.StatusOK, response) | ||
} | ||
|
||
func (h *AdminCategoryHandler) Create(c *gin.Context) { | ||
var request struct { | ||
Name string `json:"name" binding:"required"` | ||
} | ||
|
||
if err := c.ShouldBindJSON(&request); err != nil { | ||
api.RespondWithError(c, http.StatusUnprocessableEntity, "Please, provide a category name.") | ||
return | ||
} | ||
|
||
category := &domain.Category{ | ||
Name: request.Name, | ||
} | ||
|
||
if err := h.categoryService.Create(category); err != nil { | ||
api.RespondWithError(c, http.StatusInternalServerError, "Failed to create category: "+err.Error()) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, gin.H{"message": "The category was successfully created!"}) | ||
} | ||
|
||
func (h *AdminCategoryHandler) Update(c *gin.Context) { | ||
categoryIdStr := c.Param("id") | ||
|
||
categoryID, err := strconv.ParseUint(categoryIdStr, 10, 32) | ||
if err != nil { | ||
api.RespondWithError(c, http.StatusBadRequest, "Invalid category ID: "+err.Error()) | ||
return | ||
} | ||
|
||
var request ports_admin.UpdateCategoryInterface | ||
|
||
if err := c.ShouldBindJSON(&request); err != nil { | ||
api.RespondWithError(c, http.StatusUnprocessableEntity, "Please, provide a category name.") | ||
return | ||
} | ||
|
||
if err := h.categoryService.Update(uint(categoryID), request); err != nil { | ||
api.RespondWithError(c, http.StatusInternalServerError, "Failed to update category: "+err.Error()) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"message": "The category was successfully updated!"}) | ||
} | ||
|
||
func (h *AdminCategoryHandler) Delete(c *gin.Context) { | ||
categoryIdStr := c.Param("id") | ||
|
||
categoryID, err := strconv.ParseUint(categoryIdStr, 10, 32) | ||
if err != nil { | ||
api.RespondWithError(c, http.StatusBadRequest, "Invalid category ID: "+err.Error()) | ||
return | ||
} | ||
|
||
if err := h.categoryService.Delete(uint(categoryID)); err != nil { | ||
if httpErr, ok := err.(*errors.HttpError); ok { | ||
api.RespondWithError(c, httpErr.Code, httpErr.Error()) | ||
} else { | ||
api.RespondWithError(c, http.StatusInternalServerError, "Failed to delete category: "+err.Error()) | ||
} | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"message": "The category was successfully removed!"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package db_admin | ||
|
||
import ( | ||
"fmt" | ||
"gcstatus/internal/domain" | ||
"gcstatus/internal/errors" | ||
ports_admin "gcstatus/internal/ports/admin" | ||
"net/http" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type AdminCategoryRepositoryMySQL struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewAdminCategoryRepositoryMySQL(db *gorm.DB) ports_admin.AdminCategoryRepository { | ||
return &AdminCategoryRepositoryMySQL{ | ||
db: db, | ||
} | ||
} | ||
|
||
func (h *AdminCategoryRepositoryMySQL) GetAll() ([]domain.Category, error) { | ||
var categories []domain.Category | ||
err := h.db.Model(&domain.Category{}). | ||
Find(&categories). | ||
Error | ||
|
||
return categories, err | ||
} | ||
|
||
func (h *AdminCategoryRepositoryMySQL) Create(category *domain.Category) error { | ||
return h.db.Create(&category).Error | ||
} | ||
|
||
func (h *AdminCategoryRepositoryMySQL) Update(id uint, request ports_admin.UpdateCategoryInterface) error { | ||
updateFields := map[string]any{ | ||
"name": request.Name, | ||
"slug": request.Slug, | ||
} | ||
|
||
if err := h.db.Model(&domain.Category{}).Where("id = ?", id).Updates(updateFields).Error; err != nil { | ||
return fmt.Errorf("failed to update category: %+s", err.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *AdminCategoryRepositoryMySQL) Delete(id uint) error { | ||
if err := h.db.Delete(&domain.Category{}, id).Error; err != nil { | ||
return errors.NewHttpError(http.StatusNotFound, "category not found.") | ||
} | ||
return nil | ||
} |
Oops, something went wrong.