Skip to content

Commit 3ae000b

Browse files
committed
chore():manage better controller
1 parent d668a11 commit 3ae000b

File tree

5 files changed

+60
-41
lines changed

5 files changed

+60
-41
lines changed

src/controllers/links.ts

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Express } from "express";
22
import { Request, Response } from "express";
3-
import { Cache, CacheContainer } from "node-ts-cache";
3+
import { CacheContainer } from "node-ts-cache";
44
import { MemoryStorage } from "node-ts-cache-storage-memory";
55

66
import { putLink, getLink, deleteLink } from "../services/links";
@@ -17,38 +17,53 @@ export default class LinksController {
1717
app.use("/docs/links", swaggerUi.serve);
1818
app.get("/docs/links", swaggerUi.setup(linksDocs));
1919

20-
app.get("/:_slug", async (req: Request, res: Response) => {
21-
const { _slug } = req.params;
22-
const cachedLink = await linksCache.getItem<ILink>(_slug);
23-
24-
if (cachedLink) {
25-
return res.redirect(cachedLink.redirect);
26-
} else {
27-
try {
28-
const link: ILink = (await getLink(_slug)) as ILink;
29-
const { redirect } = link;
30-
31-
if (redirect) {
32-
await linksCache.setItem(_slug, link, { ttl: 60 });
33-
res.redirect(redirect);
34-
} else {
35-
res.status(404).json({ message: "redirect not found" });
36-
}
37-
} catch (error) {
38-
res.status(404).json({ message: "slug not found" });
39-
}
40-
}
41-
});
20+
app.get("/:_slug", async (req: Request, res: Response) =>
21+
this.getLinks(req, res)
22+
);
4223

43-
app.post("/api/links", async (req: any, res: any) => {
44-
const { body: link } = req;
45-
res.json(await putLink(link));
24+
app.post("/api/links", async (req: Request, res: Response) => {
25+
this.insertLink(req, res);
4626
});
4727

48-
app.delete("/api/links/:_id", async (req: any, res: any) => {
49-
const { _id } = req.params;
50-
res.json(await deleteLink(_id));
51-
res.json("result");
28+
app.delete("/api/links/:_id", async (req: Request, res: Response) => {
29+
this.deleteLink(req, res);
5230
});
5331
}
32+
33+
public deleteLink = async (req: Request, res: Response) => {
34+
const { _id } = req.params;
35+
res.json(await deleteLink(_id));
36+
res.json("result");
37+
};
38+
39+
public insertLink = async (req: Request, res: Response) => {
40+
const { body: link } = req;
41+
const createdLink = (await putLink(link)) as ILink;
42+
await linksCache.setItem(createdLink.slug, null, { ttl: 60 });
43+
res.json(createdLink);
44+
};
45+
46+
public getLinks = async (req: Request, res: Response) => {
47+
const { _slug } = req.params;
48+
const cachedLink = await linksCache.getItem<ILink>(_slug);
49+
50+
if (cachedLink) {
51+
return res.redirect(cachedLink.redirect);
52+
} else {
53+
try {
54+
const link: ILink = (await getLink(_slug)) as ILink;
55+
const { redirect } = link;
56+
57+
if (redirect) {
58+
await linksCache.setItem(_slug, link, { ttl: 60 });
59+
return res.redirect(redirect);
60+
} else {
61+
return res.status(404).json({ message: "redirect not found" });
62+
}
63+
} catch (error) {
64+
return res.status(404).json({ message: "slug not found" });
65+
}
66+
}
67+
};
68+
5469
}

src/libs/ddbClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ if (!process.env.AWS_REGION) {
44
throw new Error("AWS_REGION is not set");
55
}
66

7-
export const ddbClient = new DynamoDBClient({ region: process.env.AWS_REGION });
7+
export const ddbClient = new DynamoDBClient({ region: process.env.AWS_REGION });

src/models/ILink.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export interface ILinkDTO {
22
redirect: string;
33
slug?: string;
4-
link: string;
4+
short_link?: string;
55
}
66

77
export interface ILink {
88
redirect: string;
99
slug: string;
10-
link: string;
10+
short_link: string;
1111
}

src/services/links.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import { ILink, ILinkDTO } from "../models/ILink";
44

55
const { nanoid } = require("nanoid");
66
const TABLE_NAME = "links";
7+
const BASE_PATH = process.env.BASE_URL;
78

8-
export const putLink = async (link: ILinkDTO) => {
9-
const newLink = {
10-
...link,
11-
slug: link.slug ? link.slug : nanoid(6),
9+
export const putLink = async ({ redirect, slug }: ILinkDTO) => {
10+
const insertSlug: string = slug ? slug : nanoid(6);
11+
12+
const newLink: ILink = {
13+
redirect,
14+
short_link: `${BASE_PATH}/${insertSlug}`,
15+
slug: insertSlug,
1216
};
17+
1318
const params = {
1419
TableName: TABLE_NAME,
1520
Item: newLink,
@@ -20,7 +25,6 @@ export const putLink = async (link: ILinkDTO) => {
2025
return newLink;
2126
} catch (err) {
2227
console.error(err);
23-
return { message: err, status: "error" };
2428
}
2529
};
2630

@@ -52,6 +56,5 @@ export const deleteLink = async (slug: string) => {
5256
return { message: "Link deleted", status: "success" };
5357
} catch (err) {
5458
console.error(err);
55-
return { message: err, status: "error" };
5659
}
5760
};

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"pretty": true,
1313
"outDir": "build",
1414
"resolveJsonModule": true,
15-
16-
},
15+
"emitDecoratorMetadata": true,
16+
"experimentalDecorators": true
17+
}
1718
}

0 commit comments

Comments
 (0)