From 3c6d7e027ad553ddd1849d59eed022811e023baf Mon Sep 17 00:00:00 2001 From: Daniel Svitan Date: Sun, 13 Apr 2025 17:58:05 +0200 Subject: [PATCH] :zap: Adds a unique name rule --- server/main.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/server/main.go b/server/main.go index df9120b..8c47ba9 100644 --- a/server/main.go +++ b/server/main.go @@ -237,12 +237,22 @@ func main() { return c.JSON(http.StatusBadRequest, "missing field 'name'") } + var target *Client = nil + for _, client := range clients { if client.ID == id || client.Name == id.String() { - client.Name = name - client.UpdatedAt = time.Now() - return c.JSON(http.StatusOK, echo.Map{"message": "ok"}) + target = client } + + if client.Name == name { + return c.JSON(http.StatusBadRequest, echo.Map{"message": "name already used"}) + } + } + + if target != nil { + target.Name = name + target.UpdatedAt = time.Now() + return c.JSON(http.StatusOK, echo.Map{"message": "ok"}) } return c.JSON(http.StatusNotFound, echo.Map{"message": "not found"})