Compare commits

..

No commits in common. "c026657e2f11247228075b1bfc1ef91477c1f9a8" and "bf7961f1ca962fa81f5345107112270788d48639" have entirely different histories.

5 changed files with 32 additions and 66 deletions

View File

@ -1,10 +1,4 @@
import mongoose, { Document } from "mongoose"; import mongoose from "mongoose";
export interface Image extends Document {
url: String;
enum: "consumed" | "unavailable" | "available";
tags?: String[];
}
const ImageSchema = new mongoose.Schema({ const ImageSchema = new mongoose.Schema({
url: { url: {
@ -23,4 +17,4 @@ const ImageSchema = new mongoose.Schema({
} }
}); });
export default mongoose.model<Image>('images', ImageSchema); export default mongoose.model('images', ImageSchema);

View File

@ -1,33 +0,0 @@
import { Request, Response } from "express";
import imageService from "../services/ImageService";
class ImageController {
async getAllImages(req: Request, res: Response): Promise<void> {
try {
const images = await imageService.findAll();
res.json({ message: images });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal Server Error" });
}
}
async addImage(req: Request, res: Response): Promise<void> {
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({ message: 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 });
}
}
}
export default new ImageController();

View File

@ -1,7 +1,7 @@
import express from "express"; import express from "express";
import mongoose from "mongoose"; import mongoose from "mongoose";
import ImageModel from "./ImageModel";
import listEndpoints from "express-list-endpoints"; import listEndpoints from "express-list-endpoints";
import imageController from "./controllers/ImageController";
export const app = express(); export const app = express();
@ -9,11 +9,34 @@ app.use(express.json());
app.get("/", (_, res) => { app.get("/", (_, res) => {
const endpoints = listEndpoints(app); const endpoints = listEndpoints(app);
res.json({ message: endpoints }); res.json({ endpoints });
}); });
app.get("/images", imageController.getAllImages); app.get("/images", async (_, res) => {
app.post("/images", imageController.addImage); try {
const allImages = await ImageModel.find();
res.json({ message: allImages });
} catch (error) {
res.status(500).json({ message: error });
}
});
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
const image = await ImageModel.create(req.body);
res.status(201).json({ message: 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 });
}
});
// Set the default port to 8080, or use the PORT environment variable // Set the default port to 8080, or use the PORT environment variable

View File

@ -1,14 +0,0 @@
import ImageModel, { Image } from "../models/ImageModel";
class ImageService {
async findAll(): Promise<Image[]> {
const allImages = await ImageModel.find();
return allImages;
}
async add(image: Image): Promise<Image> {
const newImage = await ImageModel.create(image);
return newImage;
}
}
export default new ImageService();

View File

@ -2,22 +2,18 @@ import { describe, expect, it, mock } from "bun:test";
import request from "supertest"; import request from "supertest";
import { app } from "../src"; import { app } from "../src";
describe("GET / shows all of the endpoints", async () => { describe("GET / shows what it should",async () => {
const res = await request(app).get("/"); const res = await request(app).get("/");
it("should be", async () => { it("should be", async () => {
expect(res.body).toHaveProperty("message"); expect(res.body).toHaveProperty("message", "Blazing fast 🚀");
}); });
it("should be an array", () => {
expect(Array.isArray(res.body.message)).toBeTrue();
})
}) })
describe("GET /images works properly", async () => { describe("GET /images works properly", async () => {
const res = await request(app).get("/images"); const res = await request(app).get("/images");
it("should be an array", () => { it("should be an array", async () => {
expect(Array.isArray(res.body.message)).toBeTrue(); expect(Array.isArray(res.body.message)).toBeTrue();
}); });