Skip to content

Commit

Permalink
implemented delete server logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Jan 3, 2024
1 parent 919b6cf commit 0177a99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions controllers/DeleteServerController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package controllers

import (
"ch/kirari04/videocms/helpers"
"ch/kirari04/videocms/inits"
"ch/kirari04/videocms/models"
"fmt"
"log"

"github.com/gofiber/fiber/v2"
)

func DeleteServer(c *fiber.Ctx) error {
// parse & validate request
var validatus models.ServerDeleteValidation
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.Server{}, validatus.ServerID)
if res.Error != nil {
log.Println("Failed to delete server", validatus.ServerID, res.Error)
return c.Status(fiber.StatusInternalServerError).SendString("Failed to delete server")
}
if res.RowsAffected == 0 {
return c.Status(fiber.StatusBadRequest).SendString("Server not found")
}
return c.Status(fiber.StatusOK).SendString("ok")
}
1 change: 1 addition & 0 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Api() {
protectedApi.Put("/account/settings", controllers.UpdateUserSettingsController)

protectedApi.Post("/server", middlewares.IsAdmin, controllers.CreateServer)
protectedApi.Delete("/server", middlewares.IsAdmin, controllers.DeleteServer)
protectedApi.Get("/servers", middlewares.IsAdmin, controllers.ListServers)

protectedApi.Post("/webhook", controllers.CreateWebhook)
Expand Down

0 comments on commit 0177a99

Please sign in to comment.