put-images #18
|
@ -15,6 +15,7 @@ app.get("/", (_, res) => {
|
||||||
|
|
||||||
app.get("/images", imageController.getAllImages);
|
app.get("/images", imageController.getAllImages);
|
||||||
app.get("/images/:id", imageController.getImageById);
|
app.get("/images/:id", imageController.getImageById);
|
||||||
|
app.put("/images/:id", imageController.editImage)
|
||||||
app.post("/images", authControler.authorize, imageController.addImage);
|
app.post("/images", authControler.authorize, imageController.addImage);
|
||||||
app.post("/login", authControler.login);
|
app.post("/login", authControler.login);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,37 @@
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import imageService from "../services/ImageService";
|
import imageService from "../services/ImageService";
|
||||||
import mongoose, { mongo } from "mongoose";
|
import mongoose, { mongo } from "mongoose";
|
||||||
|
import { Image } from "../models/ImageModel";
|
||||||
|
|
||||||
class ImageController {
|
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) {
|
async getImageById(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const image = await imageService.findById(req.params.id);
|
const image = await imageService.findById(req.params.id);
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import imageModel, { Image } from "../models/ImageModel";
|
import imageModel, { Image } from "../models/ImageModel";
|
||||||
|
|
||||||
class ImageService {
|
class ImageService {
|
||||||
|
async replaceOne(id: string, newimage: Image) {
|
||||||
|
const image = await imageModel.updateOne({ _id: id }, newimage);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
async findById(id: string) {
|
async findById(id: string) {
|
||||||
const image = await imageModel.findOne({ _id: id });
|
const image = await imageModel.findOne({ _id: id });
|
||||||
return image;
|
return image;
|
||||||
|
|
Loading…
Reference in New Issue