From c363947870ffebc9b05d8f681ad522ed8276af54 Mon Sep 17 00:00:00 2001 From: Alie Date: Mon, 25 Dec 2023 12:21:01 +0100 Subject: [PATCH] added the logic for the endpoint and made a fix in an index that was wrong --- mongo-init.js | 2 +- src/index.ts | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/mongo-init.js b/mongo-init.js index c02de9c..a7b191b 100644 --- a/mongo-init.js +++ b/mongo-init.js @@ -16,7 +16,7 @@ db.createUser({ db = new Mongo().getDB("bot"); db.images.createIndex({ "status": 1 }); -db.images.createIndex({ "image": 1 }, { "unique": true }); +db.images.createIndex({ "url": 1 }, { "unique": true }); db.images.insert({ url: "https://example.com", status: "consumed", diff --git a/src/index.ts b/src/index.ts index cc1c227..3308c10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,13 +23,16 @@ app.post("/images", async (req, res) => { try { // Should add auth here before doing stuff // Thowing a 401 if not auth provided - // throwing a 403 for incorrect auth + // Throwing a 403 for incorrect auth const image = await ImageModel.create(req.body); - res.json(image); - } catch (error) { - // Should return 409 Conflict for existing urls - // Should return 500 For other server errors - res.status(500).json({ message: error }); + res.status(201).json(image); + } catch (error: any) { + if (error.code == 11000){ + // Should return 409 Conflict for existing urls + res.status(409).json({ message: "Existing URL" }); + } + // Should return 400 Bad request for invalid requests + res.status(400).json({ message: error }); } })