Separated things into Model-Service-Controller pattern
Gitea Actions Demo / Explore-Gitea-Actions (pull_request) Has been cancelled
Details
Gitea Actions Demo / Explore-Gitea-Actions (pull_request) Has been cancelled
Details
This commit is contained in:
parent
bf7961f1ca
commit
e89bc6d508
|
@ -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();
|
31
src/index.ts
31
src/index.ts
|
@ -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,34 +9,11 @@ app.use(express.json());
|
||||||
|
|
||||||
app.get("/", (_, res) => {
|
app.get("/", (_, res) => {
|
||||||
const endpoints = listEndpoints(app);
|
const endpoints = listEndpoints(app);
|
||||||
res.json({ endpoints });
|
res.json({ message: endpoints });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/images", async (_, res) => {
|
app.get("/images", imageController.getAllImages);
|
||||||
try {
|
app.post("/images", imageController.addImage);
|
||||||
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
|
||||||
|
|
||||||
|
|
|
@ -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({
|
const ImageSchema = new mongoose.Schema({
|
||||||
url: {
|
url: {
|
||||||
|
@ -17,4 +23,4 @@ const ImageSchema = new mongoose.Schema({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export default mongoose.model('images', ImageSchema);
|
export default mongoose.model<Image>('images', ImageSchema);
|
|
@ -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();
|
|
@ -2,18 +2,22 @@ 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 what it should",async () => {
|
describe("GET / shows all of the endpoints", 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", "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 () => {
|
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", async () => {
|
it("should be an array", () => {
|
||||||
expect(Array.isArray(res.body.message)).toBeTrue();
|
expect(Array.isArray(res.body.message)).toBeTrue();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue