Skip to content

Commit

Permalink
implemented delete webpage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Jan 11, 2024
1 parent 36dccef commit 180031e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
34 changes: 34 additions & 0 deletions controllers/DeleteWebPageController.go
Original file line number Diff line number Diff line change
@@ -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")
}
4 changes: 4 additions & 0 deletions models/WebPage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
1 change: 1 addition & 0 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 180031e

Please sign in to comment.