From 70ae3598567beedc3e6351cb73bc977e30c6fe07 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 6 Mar 2024 10:14:54 +0100 Subject: [PATCH] Example of how to extend the API --- backend/cmd/main.go | 1 + backend/internal/handlers/global_state.go | 40 +++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/backend/cmd/main.go b/backend/cmd/main.go index b0cee26..2593019 100644 --- a/backend/cmd/main.go +++ b/backend/cmd/main.go @@ -35,6 +35,7 @@ func main() { // Register our handlers server.Post("/api/register", gs.Register) + server.Delete("/api/userdelete", gs.UserDelete) // Perhaps just use POST to avoid headaches // Register handlers for example button count server.Get("/api/button", gs.GetButtonCount) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index 6c8a9dc..2d3e9f6 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -9,9 +9,28 @@ import ( // The actual interface that we will use type GlobalState interface { - Register(c *fiber.Ctx) error - GetButtonCount(c *fiber.Ctx) error - IncrementButtonCount(c *fiber.Ctx) error + Register(c *fiber.Ctx) error // To register a new user + UserDelete(c *fiber.Ctx) error // To delete a user + // LoginPost(c *fiber.Ctx) error // To get the token + // LoginUpdate(c *fiber.Ctx) error // To renew the token + // CreateProject(c *fiber.Ctx) error // To create a new project + // GetProjects(c *fiber.Ctx) error // To get all projects + // GetProject(c *fiber.Ctx) error // To get a specific project + // UpdateProject(c *fiber.Ctx) error // To update a project + // DeleteProject(c *fiber.Ctx) error // To delete a project + // CreateTask(c *fiber.Ctx) error // To create a new task + // GetTasks(c *fiber.Ctx) error // To get all tasks + // GetTask(c *fiber.Ctx) error // To get a specific task + // UpdateTask(c *fiber.Ctx) error // To update a task + // DeleteTask(c *fiber.Ctx) error // To delete a task + // CreateCollection(c *fiber.Ctx) error // To create a new collection + // GetCollections(c *fiber.Ctx) error // To get all collections + // GetCollection(c *fiber.Ctx) error // To get a specific collection + // UpdateCollection(c *fiber.Ctx) error // To update a collection + // DeleteCollection(c *fiber.Ctx) error // To delete a collection + // SignCollection(c *fiber.Ctx) error // To sign a collection + GetButtonCount(c *fiber.Ctx) error // For demonstration purposes + IncrementButtonCount(c *fiber.Ctx) error // For demonstration purposes } // "Constructor" @@ -38,6 +57,21 @@ func (gs *GState) Register(c *fiber.Ctx) error { return c.Status(200).SendString("User added") } +// This path should obviously be protected in the future +// UserDelete deletes a user from the database +func (gs *GState) UserDelete(c *fiber.Ctx) error { + u := new(types.User) + if err := c.BodyParser(u); err != nil { + return c.Status(400).SendString(err.Error()) + } + + if err := gs.Db.RemoveUser(u.Username); err != nil { + return c.Status(500).SendString(err.Error()) + } + + return c.Status(200).SendString("User deleted") +} + func (gs *GState) GetButtonCount(c *fiber.Ctx) error { return c.Status(200).JSON(fiber.Map{"pressCount": gs.ButtonCount}) }