v1.0.0 #28

Merged
bizcochito merged 147 commits from develop into main 2024-01-14 19:49:34 +00:00
5 changed files with 66 additions and 32 deletions
Showing only changes of commit c026657e2f - Show all commits

View File

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

View File

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

View File

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