Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/modules/shorts/cases/editShort/EditShortController.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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 });
});
}
}
28 changes: 28 additions & 0 deletions src/modules/shorts/cases/editShort/EditShortUseCase.ts
Original file line number Diff line number Diff line change
@@ -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<Short> {
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);
}
}
7 changes: 7 additions & 0 deletions src/modules/shorts/cases/editShort/index.ts
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion src/modules/shorts/cases/findShort/FindShortController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class FindShortController {
constructor(private findShortUseCase: FindShortUseCase) {}

async handle(request: Request, response: Response): Promise<void> {
const { short } = request.params;
const short = request.params.short as string;

await this.findShortUseCase.execute(short)
.then(instance => response.redirect(instance.url))
Expand Down
5 changes: 5 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
});
Expand Down