fmt
This commit is contained in:
parent
3be98d0e20
commit
0d2e8dfe36
|
@ -1,4 +1,4 @@
|
||||||
version: '3'
|
version: "3"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
mongodb:
|
mongodb:
|
||||||
|
|
|
@ -3,36 +3,40 @@ import imageService from "../services/ImageService";
|
||||||
import mongoose, { mongo } from "mongoose";
|
import mongoose, { mongo } from "mongoose";
|
||||||
|
|
||||||
class ImageController {
|
class ImageController {
|
||||||
async getAllImages(req: Request, res: Response): Promise<void> {
|
async getAllImages(req: Request, res: Response): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const images = await imageService.findAll();
|
const images = await imageService.findAll();
|
||||||
res.json({ images });
|
res.json({ images });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
res.status(500).json({ error: "Internal Server Error" });
|
res.status(500).json({ error: "Internal Server Error" });
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async addImage(req: Request, res: Response): Promise<void> {
|
async addImage(req: Request, res: Response): Promise<void> {
|
||||||
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 imageService.add(req.body);
|
const image = await imageService.add(req.body);
|
||||||
res.status(201).json({ image });
|
res.status(201).json({ image });
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error instanceof mongo.MongoServerError && error.code === 11000) {
|
if (error instanceof mongo.MongoServerError && error.code === 11000) {
|
||||||
// Should return 409 Conflict for existing urls
|
// Should return 409 Conflict for existing urls
|
||||||
res.status(409).json({ error: `the image with URL ${error.keyValue.url} already exists` });
|
res
|
||||||
} else if (error instanceof mongoose.Error.ValidationError) {
|
.status(409)
|
||||||
// Should return 400 Bad request for invalid requests
|
.json({
|
||||||
res.status(400).json({ error: error.message });
|
error: `the image with URL ${error.keyValue.url} already exists`,
|
||||||
} else {
|
});
|
||||||
// Return 500 in other case
|
} else if (error instanceof mongoose.Error.ValidationError) {
|
||||||
res.status(500).json({ error: error });
|
// 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();
|
export default new ImageController();
|
|
@ -9,14 +9,13 @@ export const app = express();
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.get("/", (_, res) => {
|
app.get("/", (_, res) => {
|
||||||
const endpoints = listEndpoints(app);
|
const endpoints = listEndpoints(app);
|
||||||
res.json({ endpoints });
|
res.json({ endpoints });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/images", imageController.getAllImages);
|
app.get("/images", imageController.getAllImages);
|
||||||
app.post("/images", authControler.authorize, imageController.addImage);
|
app.post("/images", authControler.authorize, imageController.addImage);
|
||||||
app.post("/login", authControler.login)
|
app.post("/login", authControler.login);
|
||||||
|
|
||||||
|
|
||||||
const start = async () => {
|
const start = async () => {
|
||||||
// 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,8 +1,8 @@
|
||||||
import mongoose, { Document } from "mongoose";
|
import mongoose, { Document } from "mongoose";
|
||||||
|
|
||||||
export interface Auth extends Document {
|
export interface Auth extends Document {
|
||||||
app: String,
|
app: String;
|
||||||
secret: String
|
secret: String;
|
||||||
}
|
}
|
||||||
|
|
||||||
const AuthSchema = new mongoose.Schema({
|
const AuthSchema = new mongoose.Schema({
|
||||||
|
|
|
@ -1,26 +1,26 @@
|
||||||
import mongoose, { Document } from "mongoose";
|
import mongoose, { Document } from "mongoose";
|
||||||
|
|
||||||
export interface Image extends Document {
|
export interface Image extends Document {
|
||||||
url: String;
|
url: String;
|
||||||
enum: "consumed" | "unavailable" | "available";
|
enum: "consumed" | "unavailable" | "available";
|
||||||
tags?: String[];
|
tags?: String[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const ImageSchema = new mongoose.Schema({
|
const ImageSchema = new mongoose.Schema({
|
||||||
url: {
|
url: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true
|
required: true,
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
type: String,
|
||||||
|
enum: {
|
||||||
|
values: ["consumed", "unavailable", "available"],
|
||||||
},
|
},
|
||||||
status: {
|
required: true,
|
||||||
type: String,
|
},
|
||||||
enum: {
|
tags: {
|
||||||
values: ["consumed", "unavailable", "available"],
|
type: [String],
|
||||||
},
|
},
|
||||||
required: true
|
|
||||||
},
|
|
||||||
tags: {
|
|
||||||
type: [String]
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default mongoose.model<Image>('images', ImageSchema);
|
export default mongoose.model<Image>("images", ImageSchema);
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
import imageModel, { Image } from "../models/ImageModel";
|
import imageModel, { Image } from "../models/ImageModel";
|
||||||
|
|
||||||
class ImageService {
|
class ImageService {
|
||||||
async findAll(): Promise<Image[]> {
|
async findAll(): Promise<Image[]> {
|
||||||
const allImages = await imageModel.find();
|
const allImages = await imageModel.find();
|
||||||
return allImages;
|
return allImages;
|
||||||
}
|
}
|
||||||
async add(image: Image): Promise<Image> {
|
async add(image: Image): Promise<Image> {
|
||||||
const newImage = await imageModel.create(image);
|
const newImage = await imageModel.create(image);
|
||||||
return newImage;
|
return newImage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new ImageService();
|
export default new ImageService();
|
|
@ -41,29 +41,28 @@ describe("GET /images works properly", async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("POST /images works properly", () => {
|
describe("POST /images works properly", () => {
|
||||||
|
it("should return 401 for unauthenticated requests", async () => {
|
||||||
it("should return 401 for unauthenticated requests", async () => {
|
const res = await request(app)
|
||||||
const res = await request(app)
|
.post("/images")
|
||||||
.post("/images")
|
.send({
|
||||||
.send({
|
url: "https://test.url.com/0",
|
||||||
url: "https://test.url.com/0",
|
status: "available",
|
||||||
status: "available",
|
tags: ["2girls", "touhou"],
|
||||||
tags: ["2girls", "touhou"],
|
|
||||||
});
|
|
||||||
expect(res.status).toBe(401);
|
|
||||||
});
|
});
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
it("should return 403 for invalid tokens", async () => {
|
it("should return 403 for invalid tokens", async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.post("/images")
|
.post("/images")
|
||||||
.set("authorization", `Bearer token`)
|
.set("authorization", `Bearer token`)
|
||||||
.send({
|
.send({
|
||||||
url: "https://test.url.com/0",
|
url: "https://test.url.com/0",
|
||||||
status: "available",
|
status: "available",
|
||||||
tags: ["2girls", "touhou"],
|
tags: ["2girls", "touhou"],
|
||||||
});
|
|
||||||
expect(res.status).toBe(403);
|
|
||||||
});
|
});
|
||||||
|
expect(res.status).toBe(403);
|
||||||
|
});
|
||||||
|
|
||||||
it("should return 201 for new image", async () => {
|
it("should return 201 for new image", async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
|
|
|
@ -19,4 +19,3 @@ describe("/login", async () => {
|
||||||
expect(res.status).toBe(403);
|
expect(res.status).toBe(403);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue