v1.0.0 #28

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

View File

@ -15,6 +15,7 @@ app.get("/", (_, res) => {
app.get("/images", imageController.getAllImages);
app.get("/images/:id", imageController.getImageById);
app.put("/images/:id", imageController.editImage)
app.post("/images", authControler.authorize, imageController.addImage);
app.post("/login", authControler.login);

View File

@ -1,8 +1,37 @@
import { Request, Response } from "express";
import imageService from "../services/ImageService";
import mongoose, { mongo } from "mongoose";
import { Image } from "../models/ImageModel";
class ImageController {
async editImage(req: Request, res: Response) {
try {
const change: Image = req.body;
const result = await imageService.replaceOne(req.params.id, change);
if (result.matchedCount > 0) {
res.status(204).json();
} else {
res.status(404).json({ error: "Image not found" });
}
} catch (error: any) {
if (error instanceof mongoose.Error.CastError) {
res.status(400).json({ error: "Invalid Id" });
} else 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 {
res.status(500).json({ error: "Internal Server Error" });
}
}
}
async getImageById(req: Request, res: Response) {
try {
const image = await imageService.findById(req.params.id);

View File

@ -1,6 +1,10 @@
import imageModel, { Image } from "../models/ImageModel";
class ImageService {
async replaceOne(id: string, newimage: Image) {
const image = await imageModel.updateOne({ _id: id }, newimage);
return image;
}
async findById(id: string) {
const image = await imageModel.findOne({ _id: id });
return image;