π The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.
Vue dockergolangkanbanknowledge-baseknowledge-managementmarkdownnote-takingnotesnotes-appnotionnuxtnuxtjsoffline-firstoidcpwaselfhostedssovuevuejswiki
Categories routes
Smaug6739f95cc6117e22
diff --git a/backend/controllers/categories.controller.go b/backend/controllers/categories.controller.goindex 5c5e7580..cd27556c 100644--- a/backend/controllers/categories.controller.go+++ b/backend/controllers/categories.controller.go@@ -2,6 +2,7 @@ package controllersimport ("alexandrie/app"+ "alexandrie/models""alexandrie/utils""net/http"@@ -10,6 +11,9 @@ import (type CategoriesController interface {GetCategories(c *gin.Context) (int, any)+ CreateCategory(c *gin.Context) (int, any)+ UpdateCategory(c *gin.Context) (int, any)+ DeleteCategory(c *gin.Context) (int, any)}func NewCategoriesController(app *app.App) CategoriesController {@@ -37,3 +41,123 @@ func (ctr *Controller) GetCategories(c *gin.Context) (int, any) {}return http.StatusOK, categories}++// Create category+// @Summary Create a new category+// @Method POST+// @Router /categories [post]+// @Security Authenfification: Auth+// @Param category body models.Category true "Category"+// @Success 201 {object} Success(models.Category)+// @Failure 400 {object} Error+// @Failure 401 {object} Error+func (ctr *Controller) CreateCategory(c *gin.Context) (int, any) {+ var category models.Category+ if err := c.ShouldBind(&category); err != nil {+ return http.StatusBadRequest, utils.Error("Invalid request payload")+ }++ userId, err := utils.GetUserIdCtx(c)+ if err != nil {+ return http.StatusUnauthorized, err+ }+ category = models.Category{+ Id: ctr.app.Snowflake.Generate(),+ Name: category.Name,+ Icon: category.Icon,+ Order: category.Order,+ Role: category.Role,+ WorkspaceId: category.WorkspaceId,+ ParentId: category.ParentId,+ AuthorId: userId,+ }++ if err := ctr.app.Services.Category.CreateCategory(&category); err != nil {+ return http.StatusInternalServerError, err+ }+ return http.StatusCreated, category+}++// Update category+// @Summary Update a category+// @Method PUT+// @Router /categories/{id} [put]+// @Security Authenfification: Auth+// @Param id path string true "Category ID"+// @Param category body models.Category true "Category"+// @Success 200 {object} Success(models.Category)+// @Failure 400 {object} Error+// @Failure 401 {object} Error+func (ctr *Controller) UpdateCategory(c *gin.Context) (int, any) {+ categoryid, err := utils.GetIdParam(c, c.Param("id"))+ if err != nil {+ return http.StatusBadRequest, err+ }+ dbCategory, err := ctr.app.Services.Category.GetCategory(categoryid)+ if err != nil {+ return http.StatusInternalServerError, err+ }+ err = utils.RessourceAccess(c, dbCategory.AuthorId)+ if err != nil {+ return http.StatusUnauthorized, err+ }+ category := &models.Category{}+ if err := c.ShouldBind(category); err != nil {+ return http.StatusBadRequest, err+ }+ category = &models.Category{+ Id: categoryid,+ Name: category.Name,+ Icon: category.Icon,+ Order: category.Order,+ Role: category.Role,+ WorkspaceId: category.WorkspaceId,+ ParentId: category.ParentId,+ AuthorId: dbCategory.AuthorId,+ }++ err = ctr.app.Services.Category.UpdateCategory(category)+ if err != nil {+ return http.StatusInternalServerError, err+ }+ return http.StatusOK, category+}++// Delete category+// @Summary Delete a category+// @Method DELETE+// @Router /categories/{id} [delete]+// @Security Authenfification: Auth+// @Param id path string true "Category ID"+// @Success 204 {object} Success+// @Failure 400 {object} Error+// @Failure 401 {object} Error++// Delete document+// @Summary Delete document+// @Method DELETE+// @Router /documents/{id} [delete]+// @Security Authenfification: Auth+// @Param id path string true "Document ID"+// @Success 200 {object} Success(models.Document)+// @Failure 400 {object} Error+// @Failure 401 {object} Error+func (ctr *Controller) DeleteCategory(c *gin.Context) (int, any) {+ categoryId, err := utils.GetIdParam(c, c.Param("id"))+ if err != nil {+ return http.StatusBadRequest, err+ }+ db_category, err := ctr.app.Services.Category.GetCategory(categoryId)+ if err != nil {+ return http.StatusInternalServerError, err+ }+ err = utils.RessourceAccess(c, db_category.AuthorId)+ if err != nil {+ return http.StatusUnauthorized, err+ }+ err = ctr.app.Services.Category.DeleteCategory(categoryId)+ if err != nil {+ return http.StatusInternalServerError, err+ }+ return http.StatusOK, db_category+}diff --git a/backend/router/routes/categories.go b/backend/router/routes/categories.goindex 945a17d4..83004ae9 100644--- a/backend/router/routes/categories.go+++ b/backend/router/routes/categories.go@@ -14,4 +14,7 @@ func Categories(app *app.App, router *gin.RouterGroup) {docCtrl := controllers.NewCategoriesController(app)doc.GET("/:userId", middlewares.Auth(), utils.WP(docCtrl.GetCategories))+ doc.POST("/", middlewares.Auth(), utils.WP(docCtrl.CreateCategory))+ doc.PUT("/:id", middlewares.Auth(), utils.WP(docCtrl.UpdateCategory))+ doc.DELETE("/:id", middlewares.Auth(), utils.WP(docCtrl.DeleteCategory))}diff --git a/backend/services/category.service.go b/backend/services/category.service.goindex c11ad177..d6240d9a 100644--- a/backend/services/category.service.go+++ b/backend/services/category.service.go@@ -7,6 +7,10 @@ import (type CategoryService interface {GetAllCategories(userId uint64) ([]*models.Category, error)+ GetCategory(categoryId uint64) (*models.Category, error)+ CreateCategory(category *models.Category) error+ UpdateCategory(category *models.Category) error+ DeleteCategory(categoryId uint64) error}func NewCategoryService(db *sql.DB) CategoryService {@@ -30,3 +34,50 @@ func (s *Service) GetAllCategories(userId uint64) ([]*models.Category, error) {}return categories, nil}++func (s *Service) GetCategory(categoryId uint64) (*models.Category, error) {+ var category models.Category+ err := s.db.QueryRow("SELECT * FROM categories WHERE id = ?", categoryId).Scan(+ &category.Id,+ &category.Name,+ &category.Icon,+ &category.Order,+ &category.Role,+ &category.WorkspaceId,+ &category.ParentId,+ &category.AuthorId)+ if err != nil {+ return nil, err+ }+ return &category, nil+}++func (s *Service) CreateCategory(category *models.Category) error {+ _, err := s.db.Exec("INSERT INTO categories (id, name, icon, `order`, role, workspace_id, parent_id, author_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",+ category.Id,+ category.Name,+ category.Icon,+ category.Order,+ category.Role,+ category.WorkspaceId,+ category.ParentId,+ category.AuthorId)+ return err+}++func (s *Service) UpdateCategory(category *models.Category) error {+ _, err := s.db.Exec("UPDATE categories SET name = ?, icon = ?, `order` = ?, role = ?, workspace_id = ?, parent_id = ? WHERE id = ?",+ category.Name,+ category.Icon,+ category.Order,+ category.Role,+ category.WorkspaceId,+ category.ParentId,+ category.Id)+ return err+}++func (s *Service) DeleteCategory(categoryId uint64) error {+ _, err := s.db.Exec("DELETE FROM categories WHERE id = ?", categoryId)+ return err+}