-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#update: thuanvv update registration api
- Loading branch information
1 parent
35b197a
commit a969d25
Showing
11 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface RegistrationsRepository { | ||
create(parameters: { full_name: string, phone_number: string, email: string, is_learn: number, type_class: number }): Promise<{ id: number } | null>; | ||
} |
Empty file.
20 changes: 20 additions & 0 deletions
20
src/application/registration/queires/create/create-registration-query-validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ValidationException } from '@application/common/exceptions'; | ||
import { ZodError, z } from 'zod'; | ||
|
||
import { CreateRegistrationQuery } from './create-registration-query'; | ||
|
||
export async function validate(query: CreateRegistrationQuery) { | ||
try { | ||
const schema: z.ZodType<CreateRegistrationQuery> = z.object({ | ||
full_name: z.string(), | ||
phone_number: z.string(), | ||
email: z.string(), | ||
is_learn: z.number(), | ||
type_class: z.number() | ||
}); | ||
|
||
await schema.parseAsync(query); | ||
} catch (error) { | ||
throw new ValidationException(error as ZodError); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/application/registration/queires/create/create-registration-query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { validate } from "./create-registration-query-validator"; | ||
|
||
export type CreateRegistrationQuery = Readonly<{ | ||
full_name: string, | ||
phone_number: string, | ||
email: string, | ||
is_learn: number, | ||
type_class: number | ||
}>; | ||
|
||
export function makeCreateRegistrationQuery({ registrationRepository }: Pick<Dependencies, 'registrationRepository'>) { | ||
return async function listPagesQuery(query: CreateRegistrationQuery) { | ||
await validate(query); | ||
|
||
const { full_name, phone_number, email, is_learn, type_class } = query; | ||
|
||
const registration = await registrationRepository.create({ full_name, phone_number, email, is_learn, type_class }); | ||
|
||
return registration; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { makeCreateRegistrationQuery } from "./create-registration-query"; | ||
|
||
export function makeRegistrationsUseCases(dependencies: Dependencies) { | ||
return { | ||
queries: { | ||
create: makeCreateRegistrationQuery(dependencies), | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export class RegistrationEntity { | ||
public id?: bigint; | ||
public full_name: string; | ||
public phone_number: string; | ||
public is_learn?: number | null; | ||
public type_class?: number | null; | ||
public created_at?: Date | null; | ||
public updated_at?: Date | null; | ||
|
||
constructor(registrationEntity: { | ||
id?: bigint; | ||
full_name: string; | ||
phone_number: string; | ||
is_learn?: number | null; | ||
type_class?: number | null; | ||
created_at?: Date | null; | ||
updated_at?: Date | null; | ||
created_by?: string | null; | ||
updated_by?: string | null; | ||
}) { | ||
this.id = registrationEntity.id; | ||
this.full_name = registrationEntity.full_name; | ||
this.phone_number = registrationEntity.phone_number; | ||
this.is_learn = registrationEntity.is_learn; | ||
this.type_class = registrationEntity.type_class; | ||
this.created_at = registrationEntity.created_at; | ||
this.updated_at = registrationEntity.updated_at; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/infrastructure/repositories/registration-repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { RegistrationsRepository } from '@application/common/interfaces'; | ||
|
||
export function makeRegistrationsRepository({ db }: Dependencies): RegistrationsRepository { | ||
return { | ||
async create(params: { | ||
full_name: string, | ||
phone_number: string, | ||
email: string, | ||
is_learn: number, | ||
type_class: number, | ||
}) { | ||
const newRegistration = await db.registration.create({ | ||
data: { | ||
full_name: params.full_name, | ||
phone_number: params.phone_number, | ||
email: params.email, | ||
is_learn: params.is_learn, | ||
type_class: params.type_class, | ||
}, | ||
}); | ||
|
||
return { | ||
...newRegistration, | ||
id: Number(newRegistration.id), | ||
}; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { FastifyRequest } from 'fastify'; | ||
|
||
import { makeRegistrationsUseCases } from '@application/registration/queires/create'; | ||
import { CreateRegistrationQuery } from '@application/registration/queires/create/create-registration-query'; | ||
import ResponseBase from '@application/common/response-base'; | ||
|
||
export default async function registrationRoutes(fastify: FastifyRouteInstance) { | ||
const registrations = makeRegistrationsUseCases(fastify.diContainer.cradle); | ||
|
||
fastify.route({ | ||
method: 'POST', | ||
url: '/api/registration', | ||
schema: { | ||
body: { | ||
type: 'object', | ||
properties: { | ||
full_name: { type: 'string', description: 'Full name of student' }, | ||
phone_number: { type: 'string', description: 'Phone number of student' }, | ||
email: { type: 'string', description: 'Email of student or their parents' }, | ||
is_learn: { type: 'number', description: 'Are you learning IELTS before?' }, | ||
type_class: { type: 'number', description: 'Do you want the type of classes?' }, | ||
}, | ||
required: ['full_name', 'phone_number', 'email', 'is_learn', 'type_class'], | ||
}, | ||
response: { | ||
200: { | ||
type: 'object', | ||
properties: { | ||
status_code: { type: 'integer', example: 200 }, | ||
data: { | ||
type: 'object', | ||
properties: { | ||
id: { type: 'integer' }, | ||
}, | ||
}, | ||
message: { type: 'string', example: 'Class fetched successfully' }, | ||
}, | ||
}, | ||
400: { $ref: 'ExceptionResponse#' }, | ||
}, | ||
tags: ['registrations'], | ||
}, | ||
async handler( | ||
req: FastifyRequest<{ Body: CreateRegistrationQuery }>, | ||
res | ||
) { | ||
try { | ||
const componentsList = await registrations.queries.create({ | ||
full_name: req.body.full_name, | ||
phone_number: req.body.phone_number, | ||
email: req.body.email, | ||
is_learn: req.body.is_learn, | ||
type_class: req.body.type_class, | ||
}); | ||
|
||
const response = ResponseBase.formatBaseResponse( | ||
200, | ||
componentsList, | ||
'Register successfully', | ||
); | ||
|
||
res.status(200).send(response); | ||
} catch (error) { | ||
fastify.log.error(error); | ||
|
||
const errorResponse = ResponseBase.formatBaseResponse( | ||
400, | ||
null, | ||
'Register failed', | ||
); | ||
|
||
res.status(400).send(errorResponse); | ||
} | ||
}, | ||
}); | ||
} |