added the logic for the endpoint and made a fix in an index that was wrong
This commit is contained in:
parent
aa1dc44eae
commit
c363947870
|
@ -16,7 +16,7 @@ db.createUser({
|
||||||
db = new Mongo().getDB("bot");
|
db = new Mongo().getDB("bot");
|
||||||
|
|
||||||
db.images.createIndex({ "status": 1 });
|
db.images.createIndex({ "status": 1 });
|
||||||
db.images.createIndex({ "image": 1 }, { "unique": true });
|
db.images.createIndex({ "url": 1 }, { "unique": true });
|
||||||
db.images.insert({
|
db.images.insert({
|
||||||
url: "https://example.com",
|
url: "https://example.com",
|
||||||
status: "consumed",
|
status: "consumed",
|
||||||
|
|
15
src/index.ts
15
src/index.ts
|
@ -23,13 +23,16 @@ app.post("/images", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// Should add auth here before doing stuff
|
// Should add auth here before doing stuff
|
||||||
// Thowing a 401 if not auth provided
|
// 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);
|
const image = await ImageModel.create(req.body);
|
||||||
res.json(image);
|
res.status(201).json(image);
|
||||||
} catch (error) {
|
} catch (error: any) {
|
||||||
// Should return 409 Conflict for existing urls
|
if (error.code == 11000){
|
||||||
// Should return 500 For other server errors
|
// Should return 409 Conflict for existing urls
|
||||||
res.status(500).json({ message: error });
|
res.status(409).json({ message: "Existing URL" });
|
||||||
|
}
|
||||||
|
// Should return 400 Bad request for invalid requests
|
||||||
|
res.status(400).json({ message: error });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue