diff --git a/controllers/DeleteWebPageController.go b/controllers/DeleteWebPageController.go new file mode 100755 index 0000000..d45e6ac --- /dev/null +++ b/controllers/DeleteWebPageController.go @@ -0,0 +1,34 @@ +package controllers + +import ( + "ch/kirari04/videocms/helpers" + "ch/kirari04/videocms/inits" + "ch/kirari04/videocms/models" + "fmt" + "log" + + "github.com/gofiber/fiber/v2" +) + +func DeleteWebPage(c *fiber.Ctx) error { + // parse & validate request + var validatus models.WebPageDeleteValidation + if err := c.BodyParser(&validatus); err != nil { + return c.Status(fiber.StatusBadRequest).SendString("Invalid body request format") + } + + if errors := helpers.ValidateStruct(validatus); len(errors) > 0 { + return c.Status(fiber.StatusBadRequest).SendString(fmt.Sprintf("%s [%s] : %s", errors[0].FailedField, errors[0].Tag, errors[0].Value)) + } + + res := inits.DB.Delete(&models.WebPage{}, validatus.WebPageID) + if res.Error != nil { + log.Println("Failed to delete webpage", res.Error) + return c.SendStatus(fiber.StatusInternalServerError) + } + if res.RowsAffected <= 0 { + return c.Status(fiber.StatusBadRequest).SendString("Webpage not found") + } + + return c.Status(fiber.StatusOK).SendString("ok") +} diff --git a/models/WebPage.go b/models/WebPage.go index 4f552d7..7d53141 100644 --- a/models/WebPage.go +++ b/models/WebPage.go @@ -23,6 +23,10 @@ type WebPageUpdateValidation struct { ListInFooter *bool `validate:"required,boolean"` } +type WebPageDeleteValidation struct { + WebPageID uint `validate:"required,number"` +} + type WebPageGetValidation struct { Path string `validate:"required,dirpath,min=2,max=50"` } diff --git a/routes/api.go b/routes/api.go index 051b573..5f8990a 100755 --- a/routes/api.go +++ b/routes/api.go @@ -54,6 +54,7 @@ func Api() { protectedApi.Get("/pages", middlewares.IsAdmin, controllers.ListWebPage) protectedApi.Post("/page", middlewares.IsAdmin, controllers.CreateWebPage) protectedApi.Put("/page", middlewares.IsAdmin, controllers.UpdateWebPage) + protectedApi.Delete("/page", middlewares.IsAdmin, controllers.DeleteWebPage) protectedApi.Post("/webhook", controllers.CreateWebhook) protectedApi.Put("/webhook", controllers.UpdateWebhook)