From 3c9cbdbad3b98f443d13dd6cc2abfceb35267cda Mon Sep 17 00:00:00 2001 From: ivanab Date: Fri, 7 Jun 2024 15:22:46 +0200 Subject: [PATCH 1/6] Retuned result --- src/service/hackerspace.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service/hackerspace.service.ts b/src/service/hackerspace.service.ts index 507004d..9040647 100644 --- a/src/service/hackerspace.service.ts +++ b/src/service/hackerspace.service.ts @@ -46,5 +46,5 @@ export async function deleteHackerspace(hackerspaceId: number) { if (!hspace) { throw new Error("Hackerspace is not found"); } - await hspace.destroy(); + return await hspace.destroy(); } From 923c2d1d1d2f7928fa9cd9a76887ef9b7539d792 Mon Sep 17 00:00:00 2001 From: ivanab Date: Fri, 7 Jun 2024 15:29:46 +0200 Subject: [PATCH 2/6] Refactoring hackerspace.controller --- src/controllers/hackerspace.controller.ts | 214 ++++++++++++---------- 1 file changed, 116 insertions(+), 98 deletions(-) diff --git a/src/controllers/hackerspace.controller.ts b/src/controllers/hackerspace.controller.ts index 05b5ae4..fcf1a03 100644 --- a/src/controllers/hackerspace.controller.ts +++ b/src/controllers/hackerspace.controller.ts @@ -1,101 +1,119 @@ import { Request, Response } from "express"; -import { Hackerspace } from "../models/hackerspace.model"; -import { HackerspaceRepo } from "../repositories/hackerspace.repo"; - -class HackerspaceController { - async create(req: Request, res: Response) { - try { - const hspace = new Hackerspace(); - hspace.name = req.body.name; - hspace.city = req.body.city; - - await new HackerspaceRepo().save(hspace); - - res.status(201).json({ - status: "Created!", - message: "Successfully created hackerspace!", - }); - } catch (err) { - res.status(500).json({ - status: "Internal Server Error!", - message: "Internal Server Error!", - }); - } - } - - async delete(req: Request, res: Response) { - try { - let id = parseInt(req.params["id"]); - await new HackerspaceRepo().delete(id); - - res.status(200).json({ - status: "Ok!", - message: "Successfully deleted hackerspace!", - }); - } catch (err) { - res.status(500).json({ - status: "Internal Server Error!", - message: "Internal Server Error!", - }); - } - } - - async findById(req: Request, res: Response) { - try { - let id = parseInt(req.params["id"]); - const hspace = await new HackerspaceRepo().retrieveById(id); - - res.status(200).json({ - status: "Ok!", - message: "Successfully fetched hackerspace by id!", - data: hspace, - }); - } catch (err) { - res.status(500).json({ - status: "Internal Server Error!", - message: "Internal Server Error!", - }); - } - } - - async findAll(req: Request, res: Response) { - try { - const hspace = await new HackerspaceRepo().retrieveAll(); - - res.status(200).json({ - status: "Ok!", - message: "Successfully fetched all hackerspaces!", - data: hspace, - }); - } catch (err) { - res.status(500).json({ - status: "Internal Server Error!", - message: "Internal Server Error!", - }); - } - } - - async update(req: Request, res: Response) { - try { - let id = parseInt(req.params["id"]); - const hspace = new Hackerspace(); - hspace.id = id; - hspace.name = req.body.name; - hspace.city = req.body.city; - - await new HackerspaceRepo().update(hspace); - - res.status(200).json({ - status: "Ok!", - message: "Successfully updated hackerspace!", - }); - } catch (err) { - res.status(500).json({ - status: "Internal Server Error!", - message: "Internal Server Error!", - }); - } - } + +export async function createHackerspaceHandler(req: Request, res: Response) { + +} + +export async function updateHackerspaceHandler(req: Request, res: Response) { + +} + +export async function getHackerspaceHandler(req: Request, res: Response) { + } -export default new HackerspaceController(); +export async function deleteHackerspaceHandler(req: Request, res: Response) { + +} + +// import { Request, Response } from "express"; +// import { Hackerspace } from "../models/hackerspace.model"; +// import { HackerspaceRepo } from "../repositories/hackerspace.repo"; + +// class HackerspaceController { +// async create(req: Request, res: Response) { +// try { +// const hspace = new Hackerspace(); +// hspace.name = req.body.name; +// hspace.city = req.body.city; + +// await new HackerspaceRepo().save(hspace); + +// res.status(201).json({ +// status: "Created!", +// message: "Successfully created hackerspace!", +// }); +// } catch (err) { +// res.status(500).json({ +// status: "Internal Server Error!", +// message: "Internal Server Error!", +// }); +// } +// } + +// async delete(req: Request, res: Response) { +// try { +// let id = parseInt(req.params["id"]); +// await new HackerspaceRepo().delete(id); + +// res.status(200).json({ +// status: "Ok!", +// message: "Successfully deleted hackerspace!", +// }); +// } catch (err) { +// res.status(500).json({ +// status: "Internal Server Error!", +// message: "Internal Server Error!", +// }); +// } +// } + +// async findById(req: Request, res: Response) { +// try { +// let id = parseInt(req.params["id"]); +// const hspace = await new HackerspaceRepo().retrieveById(id); + +// res.status(200).json({ +// status: "Ok!", +// message: "Successfully fetched hackerspace by id!", +// data: hspace, +// }); +// } catch (err) { +// res.status(500).json({ +// status: "Internal Server Error!", +// message: "Internal Server Error!", +// }); +// } +// } + +// async findAll(req: Request, res: Response) { +// try { +// const hspace = await new HackerspaceRepo().retrieveAll(); + +// res.status(200).json({ +// status: "Ok!", +// message: "Successfully fetched all hackerspaces!", +// data: hspace, +// }); +// } catch (err) { +// res.status(500).json({ +// status: "Internal Server Error!", +// message: "Internal Server Error!", +// }); +// } +// } + +// async update(req: Request, res: Response) { +// try { +// let id = parseInt(req.params["id"]); +// const hspace = new Hackerspace(); +// hspace.id = id; +// hspace.name = req.body.name; +// hspace.city = req.body.city; + +// await new HackerspaceRepo().update(hspace); + +// res.status(200).json({ +// status: "Ok!", +// message: "Successfully updated hackerspace!", +// }); +// } catch (err) { +// res.status(500).json({ +// status: "Internal Server Error!", +// message: "Internal Server Error!", +// }); +// } +// } +// } + +// export default new HackerspaceController(); From 18dd05ca7664fdffce5b8ae47776950ae3250f3e Mon Sep 17 00:00:00 2001 From: ivanab Date: Fri, 7 Jun 2024 15:38:31 +0200 Subject: [PATCH 3/6] Added fields required rules --- src/schema/hackerspace.schema.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/schema/hackerspace.schema.ts b/src/schema/hackerspace.schema.ts index 16a42d7..4020a4e 100644 --- a/src/schema/hackerspace.schema.ts +++ b/src/schema/hackerspace.schema.ts @@ -3,10 +3,14 @@ import { z } from "zod"; export const createHackerspaceSchema = z.object({ body: z.object({ name: z - .string() + .string({ + required_error: "Name is required", + }) .min(0, { message: "Name must contain at least one character!" }), city: z - .string() + .string({ + required_error: "City is required", + }) .min(0, { message: "City must contain at least one character!" }), }), }); @@ -16,10 +20,14 @@ export const updateHackerspaceSchema = z.object({ body: z .object({ name: z - .string() + .string({ + required_error: "Name is required", + }) .min(0, { message: "Name must contain at least one character!" }), city: z - .string() + .string({ + required_error: "City is required", + }) .min(0, { message: "City must contain at least one character!" }), }) .partial(), From a18a67a941a37673223afcdb0ffe9c4d6e3691d3 Mon Sep 17 00:00:00 2001 From: ivanab Date: Fri, 7 Jun 2024 15:57:58 +0200 Subject: [PATCH 4/6] Refactored hackerspace.schema --- src/schema/hackerspace.schema.ts | 66 ++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/src/schema/hackerspace.schema.ts b/src/schema/hackerspace.schema.ts index 4020a4e..4637f8d 100644 --- a/src/schema/hackerspace.schema.ts +++ b/src/schema/hackerspace.schema.ts @@ -1,34 +1,42 @@ -import { z } from "zod"; +import { object, number, string, TypeOf } from "zod"; -export const createHackerspaceSchema = z.object({ - body: z.object({ - name: z - .string({ - required_error: "Name is required", - }) - .min(0, { message: "Name must contain at least one character!" }), - city: z - .string({ - required_error: "City is required", - }) - .min(0, { message: "City must contain at least one character!" }), +const payload = { + body: object({ + name: string({ + required_error: "Name is required", + }).min(0, { message: "Name must contain at least one character" }), + city: string({ + required_error: "City is required", + }).min(0, { message: "City must contain at least one character" }), }), +}; + +const params = { + params: object({ + id: string({ + required_error: "Hackerspace id is required", + }), + }), +}; + +export const getHackerspaceSchema = object({ + ...params, +}); + +export const createHackerspaceSchema = object({ + ...payload, +}); + +export const updateHackerspaceSchema = object({ + ...payload, + ...params, }); -export const updateHackerspaceSchema = z.object({ - params: z.object({ id: z.string() }), - body: z - .object({ - name: z - .string({ - required_error: "Name is required", - }) - .min(0, { message: "Name must contain at least one character!" }), - city: z - .string({ - required_error: "City is required", - }) - .min(0, { message: "City must contain at least one character!" }), - }) - .partial(), +export const deleteHackerspaceSchema = object({ + ...params, }); + +export type GetHackerspaceSchema = TypeOf; +export type CreateHackerspaceSchema = TypeOf; +export type UpdateHackerspaceSchema = TypeOf; +export type DeleteHackerspaceSchema = TypeOf; From bea04c6676ab474d86ec73d7f1a3ebebad2fcaf5 Mon Sep 17 00:00:00 2001 From: ivanab Date: Fri, 7 Jun 2024 16:42:12 +0200 Subject: [PATCH 5/6] Added Request parameters --- src/controllers/hackerspace.controller.ts | 42 ++++++++++++++--------- src/schema/hackerspace.schema.ts | 8 ++--- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/controllers/hackerspace.controller.ts b/src/controllers/hackerspace.controller.ts index fcf1a03..dad4ec2 100644 --- a/src/controllers/hackerspace.controller.ts +++ b/src/controllers/hackerspace.controller.ts @@ -1,20 +1,30 @@ import { Request, Response } from "express"; - -export async function createHackerspaceHandler(req: Request, res: Response) { - -} - -export async function updateHackerspaceHandler(req: Request, res: Response) { - -} - -export async function getHackerspaceHandler(req: Request, res: Response) { - -} - -export async function deleteHackerspaceHandler(req: Request, res: Response) { - -} +import { + CreateHackerspaceInput, + DeleteHackerspaceInput, + GetHackerspaceInput, + UpdateHackerspaceInput, +} from "../schema/hackerspace.schema"; + +export async function getHackerspaceHandler( + req: Request, + res: Response +) {} + +export async function createHackerspaceHandler( + req: Request<{}, {}, CreateHackerspaceInput["body"]>, + res: Response +) {} + +export async function updateHackerspaceHandler( + req: Request, + res: Response +) {} + +export async function deleteHackerspaceHandler( + req: Request, + res: Response +) {} // import { Request, Response } from "express"; // import { Hackerspace } from "../models/hackerspace.model"; diff --git a/src/schema/hackerspace.schema.ts b/src/schema/hackerspace.schema.ts index 4637f8d..e1cdaae 100644 --- a/src/schema/hackerspace.schema.ts +++ b/src/schema/hackerspace.schema.ts @@ -36,7 +36,7 @@ export const deleteHackerspaceSchema = object({ ...params, }); -export type GetHackerspaceSchema = TypeOf; -export type CreateHackerspaceSchema = TypeOf; -export type UpdateHackerspaceSchema = TypeOf; -export type DeleteHackerspaceSchema = TypeOf; +export type GetHackerspaceInput = TypeOf; +export type CreateHackerspaceInput = TypeOf; +export type UpdateHackerspaceInput = TypeOf; +export type DeleteHackerspaceInput = TypeOf; From 66baa4294bb7e20c94e18d83e44f5dbe250463d8 Mon Sep 17 00:00:00 2001 From: ivanab Date: Fri, 7 Jun 2024 20:01:51 +0200 Subject: [PATCH 6/6] Implemented create hackerspace handler --- src/controllers/hackerspace.controller.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/controllers/hackerspace.controller.ts b/src/controllers/hackerspace.controller.ts index dad4ec2..377b965 100644 --- a/src/controllers/hackerspace.controller.ts +++ b/src/controllers/hackerspace.controller.ts @@ -14,7 +14,15 @@ export async function getHackerspaceHandler( export async function createHackerspaceHandler( req: Request<{}, {}, CreateHackerspaceInput["body"]>, res: Response -) {} +) { + const userId = res.locals.user.id; + + const body = req.body; + + const hspace = await createHackerspace({ ...body, user: userId }); + + return res.send(hspace); +} export async function updateHackerspaceHandler( req: Request,