diff --git a/.gitignore b/.gitignore index c2b8d7c..1822831 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules/ -bun.lockb \ No newline at end of file +bun.lockb diff --git a/compose.yaml b/compose.yaml index ecec3f3..964794b 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,4 +1,4 @@ -version: '3' +version: "3" services: mongodb: diff --git a/package.json b/package.json index 7a8021d..2080221 100644 --- a/package.json +++ b/package.json @@ -29,4 +29,4 @@ "jsonwebtoken": "^9.0.2", "mongoose": "^8.0.3" } -} \ No newline at end of file +} diff --git a/src/controllers/ImageController.ts b/src/controllers/ImageController.ts index 5e1fcac..1b86aea 100644 --- a/src/controllers/ImageController.ts +++ b/src/controllers/ImageController.ts @@ -3,36 +3,40 @@ import imageService from "../services/ImageService"; import mongoose, { mongo } from "mongoose"; class ImageController { - async getAllImages(req: Request, res: Response): Promise { - try { - const images = await imageService.findAll(); - res.json({ images }); - } catch (error) { - console.error(error); - res.status(500).json({ error: "Internal Server Error" }); - } + async getAllImages(req: Request, res: Response): Promise { + try { + const images = await imageService.findAll(); + res.json({ images }); + } catch (error) { + console.error(error); + res.status(500).json({ error: "Internal Server Error" }); } + } - async addImage(req: Request, res: Response): Promise { - try { - // Should add auth here before doing stuff - // Thowing a 401 if not auth provided - // Throwing a 403 for incorrect auth - const image = await imageService.add(req.body); - res.status(201).json({ image }); - } catch (error: any) { - if (error instanceof mongo.MongoServerError && error.code === 11000) { - // Should return 409 Conflict for existing urls - res.status(409).json({ error: `the image with URL ${error.keyValue.url} already exists` }); - } else if (error instanceof mongoose.Error.ValidationError) { - // Should return 400 Bad request for invalid requests - res.status(400).json({ error: error.message }); - } else { - // Return 500 in other case - res.status(500).json({ error: error }); - } - } + async addImage(req: Request, res: Response): Promise { + try { + // Should add auth here before doing stuff + // Thowing a 401 if not auth provided + // Throwing a 403 for incorrect auth + const image = await imageService.add(req.body); + res.status(201).json({ image }); + } catch (error: any) { + if (error instanceof mongo.MongoServerError && error.code === 11000) { + // Should return 409 Conflict for existing urls + res + .status(409) + .json({ + error: `the image with URL ${error.keyValue.url} already exists`, + }); + } else if (error instanceof mongoose.Error.ValidationError) { + // Should return 400 Bad request for invalid requests + res.status(400).json({ error: error.message }); + } else { + // Return 500 in other case + res.status(500).json({ error: error }); + } } + } } -export default new ImageController(); \ No newline at end of file +export default new ImageController(); diff --git a/src/index.ts b/src/index.ts index 006cb3d..d74231b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,14 +9,13 @@ export const app = express(); app.use(express.json()); app.get("/", (_, res) => { - const endpoints = listEndpoints(app); - res.json({ endpoints }); + const endpoints = listEndpoints(app); + res.json({ endpoints }); }); app.get("/images", imageController.getAllImages); app.post("/images", authControler.authorize, imageController.addImage); -app.post("/login", authControler.login) - +app.post("/login", authControler.login); const start = async () => { // Set the default port to 8080, or use the PORT environment variable diff --git a/src/models/AuthModel.ts b/src/models/AuthModel.ts index 394a64b..df71afe 100644 --- a/src/models/AuthModel.ts +++ b/src/models/AuthModel.ts @@ -1,8 +1,8 @@ import mongoose, { Document } from "mongoose"; export interface Auth extends Document { - app: String, - secret: String + app: String; + secret: String; } const AuthSchema = new mongoose.Schema({ diff --git a/src/models/ImageModel.ts b/src/models/ImageModel.ts index 5ef3b70..d91ab18 100644 --- a/src/models/ImageModel.ts +++ b/src/models/ImageModel.ts @@ -1,26 +1,26 @@ import mongoose, { Document } from "mongoose"; export interface Image extends Document { - url: String; - enum: "consumed" | "unavailable" | "available"; - tags?: String[]; + url: String; + enum: "consumed" | "unavailable" | "available"; + tags?: String[]; } const ImageSchema = new mongoose.Schema({ - url: { - type: String, - required: true + url: { + type: String, + required: true, + }, + status: { + type: String, + enum: { + values: ["consumed", "unavailable", "available"], }, - status: { - type: String, - enum: { - values: ["consumed", "unavailable", "available"], - }, - required: true - }, - tags: { - type: [String] - } + required: true, + }, + tags: { + type: [String], + }, }); -export default mongoose.model('images', ImageSchema); +export default mongoose.model("images", ImageSchema); diff --git a/src/services/ImageService.ts b/src/services/ImageService.ts index bfaab2f..8691f7d 100644 --- a/src/services/ImageService.ts +++ b/src/services/ImageService.ts @@ -1,14 +1,14 @@ import imageModel, { Image } from "../models/ImageModel"; class ImageService { - async findAll(): Promise { - const allImages = await imageModel.find(); - return allImages; - } - async add(image: Image): Promise { - const newImage = await imageModel.create(image); - return newImage; - } + async findAll(): Promise { + const allImages = await imageModel.find(); + return allImages; + } + async add(image: Image): Promise { + const newImage = await imageModel.create(image); + return newImage; + } } -export default new ImageService(); \ No newline at end of file +export default new ImageService(); diff --git a/tests/app.test.ts b/tests/app.test.ts index 3d249b9..ac40ace 100644 --- a/tests/app.test.ts +++ b/tests/app.test.ts @@ -41,29 +41,28 @@ describe("GET /images works properly", async () => { }); describe("POST /images works properly", () => { - - it("should return 401 for unauthenticated requests", async () => { - const res = await request(app) - .post("/images") - .send({ - url: "https://test.url.com/0", - status: "available", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(401); + it("should return 401 for unauthenticated requests", async () => { + const res = await request(app) + .post("/images") + .send({ + url: "https://test.url.com/0", + status: "available", + tags: ["2girls", "touhou"], }); + expect(res.status).toBe(401); + }); - it("should return 403 for invalid tokens", async () => { - const res = await request(app) - .post("/images") - .set("authorization", `Bearer token`) - .send({ - url: "https://test.url.com/0", - status: "available", - tags: ["2girls", "touhou"], - }); - expect(res.status).toBe(403); + it("should return 403 for invalid tokens", async () => { + const res = await request(app) + .post("/images") + .set("authorization", `Bearer token`) + .send({ + url: "https://test.url.com/0", + status: "available", + tags: ["2girls", "touhou"], }); + expect(res.status).toBe(403); + }); it("should return 201 for new image", async () => { const res = await request(app) diff --git a/tests/auth.test.ts b/tests/auth.test.ts index 6951875..bd20cd2 100644 --- a/tests/auth.test.ts +++ b/tests/auth.test.ts @@ -19,4 +19,3 @@ describe("/login", async () => { expect(res.status).toBe(403); }); }); -