From 9fd568ddc30d37f0d123981d3f460a08c18e3f8f Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Wed, 12 Jan 2022 21:50:37 -0800 Subject: [PATCH] update/delete-contact --- controllers/authController.go | 45 +++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/controllers/authController.go b/controllers/authController.go index 7e98b59..2612ad6 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -734,8 +734,49 @@ func CreateContact(c *fiber.Ctx) error { } func RemoveContact(c *fiber.Ctx) error { + var data map[string]string + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + + if err := c.BodyParser(&data); err != nil { + cancel() + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "success": false, + "message": "Failed to parse body", + "error": err, + }) + } + + // Ensure Authenticated admin sent request + if !AuthAdmin(c) { + cancel() + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + "success": false, + "message": "Unauthorized: only an admin can perform this action", + }) + } + + // Check required fields are included + if data["id"] == "" { + cancel() + return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ + "success": false, + "message": "missing required fields", + }) + } + + _, err := contactCollection.DeleteOne(ctx, bson.M{"_id": data["id"]}) + if err != nil { + cancel() + return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ + "success": false, + "message": "Failed to delete object", + "error": err, + }) + } + defer cancel() + return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ - "success": nil, - "message": "not implimented", + "success": true, + "message": "Successfully deleted contact", }) }