diff --git a/controllers/DeleteServerController.go b/controllers/DeleteServerController.go new file mode 100755 index 0000000..b7df875 --- /dev/null +++ b/controllers/DeleteServerController.go @@ -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") +} diff --git a/routes/api.go b/routes/api.go index 68480ea..2a55d4d 100755 --- a/routes/api.go +++ b/routes/api.go @@ -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)