diff --git a/src/modules/shorts/cases/editShort/EditShortController.ts b/src/modules/shorts/cases/editShort/EditShortController.ts new file mode 100644 index 0000000..c87c763 --- /dev/null +++ b/src/modules/shorts/cases/editShort/EditShortController.ts @@ -0,0 +1,21 @@ +import { Request, Response } from "express"; + +import { EditShortUseCase } from "./EditShortUseCase"; + +export class EditShortController { + constructor(private editShortUseCase: EditShortUseCase) {} + + async handle(request: Request, response: Response): Promise { + const short = request.params.short as string; + const { url } = request.body; + + return await this.editShortUseCase.execute(short, url) + .then(data => response.status(200).json({ short: data.short })) + .catch(error => { + if (error.message === "Short not found") { + return response.status(404).json({ error: error.message }); + } + return response.status(400).json({ error: error.message }); + }); + } +} diff --git a/src/modules/shorts/cases/editShort/EditShortUseCase.ts b/src/modules/shorts/cases/editShort/EditShortUseCase.ts new file mode 100644 index 0000000..9ceef38 --- /dev/null +++ b/src/modules/shorts/cases/editShort/EditShortUseCase.ts @@ -0,0 +1,28 @@ +import { Short } from "../../models/Short"; +import { ShortRepository } from "../../repositories/ShortRepository"; + +export class EditShortUseCase { + constructor(private shortRepository: ShortRepository) {} + + async execute(short: string, url: string): Promise { + if (!url) { + throw new Error("Invalid URL"); + } + + try { + new URL(url); + } catch { + throw new Error("Invalid URL"); + } + + const record = await this.shortRepository.findByShort(short); + + if (!record) { + throw new Error("Short not found"); + } + + record.url = url; + + return await this.shortRepository.save(record); + } +} diff --git a/src/modules/shorts/cases/editShort/index.ts b/src/modules/shorts/cases/editShort/index.ts new file mode 100644 index 0000000..a7feef7 --- /dev/null +++ b/src/modules/shorts/cases/editShort/index.ts @@ -0,0 +1,7 @@ +import { ShortRepository } from "../../repositories/ShortRepository"; +import { EditShortController } from "./EditShortController"; +import { EditShortUseCase } from "./EditShortUseCase"; + +const shortRepository = ShortRepository.getInstance(); +const editShortUseCase = new EditShortUseCase(shortRepository); +export const editShortController = new EditShortController(editShortUseCase); diff --git a/src/modules/shorts/cases/findShort/FindShortController.ts b/src/modules/shorts/cases/findShort/FindShortController.ts index 6b7bc28..26cf836 100644 --- a/src/modules/shorts/cases/findShort/FindShortController.ts +++ b/src/modules/shorts/cases/findShort/FindShortController.ts @@ -5,7 +5,7 @@ export class FindShortController { constructor(private findShortUseCase: FindShortUseCase) {} async handle(request: Request, response: Response): Promise { - const { short } = request.params; + const short = request.params.short as string; await this.findShortUseCase.execute(short) .then(instance => response.redirect(instance.url)) diff --git a/src/routes/index.ts b/src/routes/index.ts index eda2726..2e4c98b 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,6 +1,7 @@ import { Router } from "express"; import { createShortController } from "../modules/shorts/cases/createShort"; +import { editShortController } from "../modules/shorts/cases/editShort"; import { findShortController } from "../modules/shorts/cases/findShort"; export const router = Router(); @@ -9,6 +10,10 @@ router.post("/short", async (request, response) => { await createShortController.handle(request, response); }); +router.put("/short/:short", async (request, response) => { + await editShortController.handle(request, response); +}); + router.get("/:short", async (request, response) => { await findShortController.handle(request, response); });