Skip to content

Commit

Permalink
#update: thuanvv update 1-1-2025 v1
Browse files Browse the repository at this point in the history
  • Loading branch information
VanThuan76 committed Jan 1, 2025
1 parent 39809c6 commit e1eab43
Show file tree
Hide file tree
Showing 140 changed files with 3,443 additions and 489 deletions.
13 changes: 13 additions & 0 deletions src/application/classes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { makeGetClassByIdQuery } from "./queries/get-class-by-id";
import { makeListHightlightClassesQuery } from "./queries/list-highlight-classes";
import { makeListShortClassesQuery } from "./queries/list-short-classes";

export function makeClassessUseCases(dependencies: Dependencies) {
return {
queries: {
listHightlightClasses: makeListHightlightClassesQuery(dependencies),
listShortClasses: makeListShortClassesQuery(dependencies),
getClassById: makeGetClassByIdQuery(dependencies),
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ClassDTO } from "@domain/dtos/class";
import { ClassEntity } from "@domain/entities";

export function map(renamedClass: ClassEntity, language: string): ClassDTO {
return {
id: renamedClass.id?.toString() || '',
title: language === 'vi' ? renamedClass.vi_name : renamedClass.en_name,
description: language === 'vi' ? renamedClass.vi_description || '' : renamedClass.en_description || '',
content: language === 'vi' ? renamedClass.vi_content || '' : renamedClass.en_content || '',
image: renamedClass.image || null,
order: renamedClass.order || null,
status: renamedClass.status,
created_at: renamedClass.created_at?.toISOString() || null,
updated_at: renamedClass.updated_at?.toISOString() || null,
created_by: renamedClass.created_by || null,
updated_by: renamedClass.updated_by || null,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ValidationException } from '@application/common/exceptions';
import { ZodError, z } from 'zod';

import { GetClassByIdQuery } from './get-class-by-id-query';

export async function validate(query: GetClassByIdQuery) {
try {
const schema: z.ZodType<GetClassByIdQuery> = z.object({
language: z.string(),
id: z.string()
});

await schema.parseAsync(query);
} catch (error) {
throw new ValidationException(error as ZodError);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { map } from "./get-class-by-id-query-mapper";
import { validate } from "./get-class-by-id-query-validator";

export type GetClassByIdQuery = Readonly<{
language: string;
id: string
}>;

export function makeGetClassByIdQuery({ classesRepository }: Pick<Dependencies, 'classesRepository'>) {
return async function getClassByIdQuery(query: GetClassByIdQuery) {
await validate(query);

const { language, id } = query;

const renamedClass = await classesRepository.getById({ language, id });

return renamedClass ? map(renamedClass, language) : null;
};
}
1 change: 1 addition & 0 deletions src/application/classes/queries/get-class-by-id/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './get-class-by-id-query';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './list-highlight-classes-query';
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ClassDTO } from "@domain/dtos/class";
import { ClassEntity } from "@domain/entities";

export function map(renamedClass: ClassEntity, language: string): ClassDTO {
return {
id: renamedClass.id?.toString() || '',
title: language === 'vi' ? renamedClass.vi_name : renamedClass.en_name,
description: language === 'vi' ? renamedClass.vi_description || '' : renamedClass.en_description || '',
content: language === 'vi' ? renamedClass.vi_content || '' : renamedClass.en_content || '',
image: renamedClass.image || null,
order: renamedClass.order || null,
status: renamedClass.status,
created_at: renamedClass.created_at?.toISOString() || null,
updated_at: renamedClass.updated_at?.toISOString() || null,
created_by: renamedClass.created_by || null,
updated_by: renamedClass.updated_by || null,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ValidationException } from '@application/common/exceptions';
import { ZodError, z } from 'zod';

import { ListHightlightClassesQuery } from './list-highlight-classes-query';

export async function validate(query: ListHightlightClassesQuery) {
try {
const schema: z.ZodType<ListHightlightClassesQuery> = z.object({
language: z.string(),
size: z.number().optional(),
page: z.number().optional()
});

await schema.parseAsync(query);
} catch (error) {
throw new ValidationException(error as ZodError);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { map } from "./list-highlight-classes-query-mapper";
import { validate } from "./list-highlight-classes-query-validator";

export type ListHightlightClassesQuery = Readonly<{
language: string;
size?: number;
page?: number
}>;

export function makeListHightlightClassesQuery({ classesRepository }: Pick<Dependencies, 'classesRepository'>) {
return async function listHightlightClassesQuery(query: ListHightlightClassesQuery) {
await validate(query);

const { language, size, page } = query;

const { data, total } = await classesRepository.getHighlightClasses({ language, size, page });

const per_page = size;
const current_page = page;
const last_page = total && per_page ? Math.ceil(total / per_page) : 1;

return {
data: data.map((classItem) => map(classItem, language)),
total,
per_page,
current_page,
last_page,
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './list-short-classes-query';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ClassDTO } from "@domain/dtos/class";
import { ClassEntity } from "@domain/entities";

type HighlightClassDTO = Omit<ClassDTO, 'description' | 'content' | 'image' | 'status' | 'created_at' | 'updated_at' | 'created_by' | 'updated_by'>;

export function map(renamedClass: ClassEntity, language: string): HighlightClassDTO {
return {
id: renamedClass.id?.toString() || '',
title: language === 'vi' ? renamedClass.vi_name : renamedClass.en_name,
order: renamedClass.order || null,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ValidationException } from '@application/common/exceptions';
import { ZodError, z } from 'zod';

import { ListShortClassesQuery } from './list-short-classes-query';

export async function validate(query: ListShortClassesQuery) {
try {
const schema: z.ZodType<ListShortClassesQuery> = z.object({
language: z.string(),
});

await schema.parseAsync(query);
} catch (error) {
throw new ValidationException(error as ZodError);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { map } from "./list-short-classes-query-mapper";
import { validate } from "./list-short-classes-query-validator";

export type ListShortClassesQuery = Readonly<{
language: string;
}>;

export function makeListShortClassesQuery({ classesRepository }: Pick<Dependencies, 'classesRepository'>) {
return async function listShortClassesQuery(query: ListShortClassesQuery) {
await validate(query);

const { language } = query;

const classes = await classesRepository.getListShort({ language });

return classes.map((classItem) => map(classItem, language));
};
}
12 changes: 8 additions & 4 deletions src/application/common/interfaces/classes-repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Class } from '@domain/entities';
import { ClassEntity } from '@domain/entities';

export interface ClassesRepository {
highLight(): Promise<{ data: Array<Class> }>;
listShort(): Promise<{ data: Array<Class> }>;
getById(parameters: { language: string, page_id: string }): Promise<Class | null>;
getHighlightClasses(params: {
language: string;
size?: number;
page?: number
}): Promise<{ data: Array<ClassEntity>, total: number } | { data: [], total: null }>;
getListShort(params: { language: string }): Promise<Array<ClassEntity> | []>;
getById(params: { language: string, id: string }): Promise<ClassEntity | null>;
}
4 changes: 2 additions & 2 deletions src/application/common/interfaces/components-repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '@domain/entities';
import { ComponentEntity } from '@domain/entities';

export interface ComponentsRepository {
getBySection(parameters: { language: string, section_id: string }): Promise<Component | null>;
getBySection(parameters: { language: string, section_id: string }): Promise<Array<ComponentEntity> | []>;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DocumentType } from '@domain/entities';
import { DocumentTypeEntity } from '@domain/entities';

export interface DocumentTypesRepository {
list(): Promise<{ data: Array<DocumentType> }>;
list(params: { language: string }): Promise<Array<DocumentTypeEntity>>;
}
6 changes: 3 additions & 3 deletions src/application/common/interfaces/documents-repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Document } from '@domain/entities';
import { DocumentEntity } from '@domain/entities';

export interface DocumentsRepository {
getById(parameters: { language: string, id: number }): Promise<{ data: Array<Document> }>;
getByDocumentType(parameters: { language: string, document_type_id: number, size: number, page: number }): Promise<{ data: Array<Document> }>;
getById(params: { language: string, id: number }): Promise<DocumentEntity | null>;
getByDocumentType(params: { language: string, document_type_id: number, size?: number; page?: number }): Promise<{ data: Array<DocumentEntity>, total: number } | { data: [], total: null }>;
}
8 changes: 6 additions & 2 deletions src/application/common/interfaces/feedbacks-repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Feedback } from '@domain/entities';
import { FeedbackEntity } from '@domain/entities';

export interface FeedbacksRepository {
list(): Promise<{ data: Array<Feedback> }>;
list(params: {
language: string;
size?: number;
page?: number
}): Promise<{ data: Array<FeedbackEntity>, total: number } | { data: [], total: null }>;
}
8 changes: 5 additions & 3 deletions src/application/common/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ export * from './pages-repository';
export * from './sections-repository';
export * from './components-repository';
export * from './classes-repository';
export * from './lecturers-repository';
export * from './news-categories-repository';
export * from './news-repository';
export * from './documents-repository';
export * from './document-types-repository';
export * from './lecturers-repository';
export * from './lectures-repository';
export * from './lecture-types-repository';
export * from './feedbacks-repository';
export * from './news-categories-repository';
export * from './news-repository';
4 changes: 2 additions & 2 deletions src/application/common/interfaces/lecture-types-repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LectureType } from '@domain/entities';
import { LectureTypeEntity } from '@domain/entities';

export interface LectureTypesRepository {
list(): Promise<{ data: Array<LectureType> }>;
list(params: { language: string }): Promise<Array<LectureTypeEntity>>;
}
8 changes: 6 additions & 2 deletions src/application/common/interfaces/lecturers-repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Lecturer } from '@domain/entities';
import { LecturerEntity } from '@domain/entities';

export interface LecturersRepository {
list(): Promise<{ data: Array<Lecturer> }>;
list(params: {
language: string;
size?: number;
page?: number
}): Promise<{ data: Array<LecturerEntity>, total: number } | { data: [], total: null }>;
}
6 changes: 3 additions & 3 deletions src/application/common/interfaces/lectures-repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Lecture } from '@domain/entities';
import { LectureEntity } from '@domain/entities';

export interface LecturesRepository {
getByLectureType(parameters: { language: string, lecture_type_id: number, size: number, page: number }): Promise<{ data: Array<Lecture> }>;
getById(parameters: { language: string, id: number }): Promise<Lecture | null>;
getByLectureType(params: { language: string, lecture_type_id: number, size?: number, page?: number }): Promise<{ data: Array<LectureEntity>, total: number } | { data: [], total: null }>;
getById(params: { language: string, id: number }): Promise<LectureEntity | null>;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NewsCategory } from '@domain/entities';
import { NewsCategoryEntity } from '@domain/entities';

export interface NewsCategorysRepository {
list(): Promise<{ data: Array<NewsCategory> }>;
export interface NewsCategoriesRepository {
list(params: { language: string; }): Promise<Array<NewsCategoryEntity> | null>;
}
10 changes: 5 additions & 5 deletions src/application/common/interfaces/news-repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { News } from '@domain/entities';
import { NewsEntity } from '@domain/entities';

export interface NewssRepository {
getRelated(parameters: { language: string, news_category_id: string, size: number, id: number }): Promise<{ data: Array<News> }>;
getByCategory(parameters: { language: string, news_category_id: string, size: number, page: number }): Promise<{ data: Array<News> }>;
getBySlug(parameters: { language: string, slug: string }): Promise<News | null>;
export interface NewsRepository {
getRelated(params: { language: string, news_category_id: number, size?: number, page?: number, id: number }): Promise<{ data: Array<NewsEntity>, total: number } | { data: [], total: null }>;
getByCategory(params: { language: string, news_category_id: number, size?: number, page?: number }): Promise<{ data: Array<NewsEntity>, total: number } | { data: [], total: null }>;
getBySlug(params: { language: string, slug: string }): Promise<NewsEntity | null>;
}
6 changes: 3 additions & 3 deletions src/application/common/interfaces/sections-repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Section } from '@domain/entities';
import { SectionEntity } from "@domain/entities";

export interface SectionsRepository {
getByPage(parameters: { language: string, page_id: string }): Promise<{ data: Array<Section> }>;
getByPageUrl(parameters: { language: string, url: string }): Promise<{ data: Array<Section> }>;
getByPage(parameters: { language: string, page_id: string }): Promise<Array<SectionEntity> | []>;
getByPageUrl(parameters: { language: string, url: string }): Promise<Array<SectionEntity> | []>;
}
30 changes: 17 additions & 13 deletions src/application/common/response-base.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
export default class ResponseBase {
static formatCountResponse<T>(data: T, perPage: number, total: number) {
static formatPaginationResponse<T>(status_code: number, data: T, total: number, per_page: number, current_page: number, last_page: number, message: string) {
return {
content: data,
totalElements: perPage,
totalPages: total,
status_code,
data,
total,
per_page,
current_page,
last_page,
message,
};
}

static formatBaseResponse<T>(statusCode: number, data: T, message: string) {
static formatBaseResponse<T>(status_code: number, data: T, message: string) {
return {
statusCode: statusCode,
data: data,
message: message,
status_code,
data,
message,
};
}

static formatBaseResponseWithTotal<T>(statusCode: number, data: T, total: number, message: string) {
static formatBaseResponseWithTotal<T>(status_code: number, data: T, total: number, message: string) {
return {
statusCode: statusCode,
data: data,
total: total,
message: message,
status_code,
data,
total,
message,
};
}
}
9 changes: 9 additions & 0 deletions src/application/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { makeComponentGetBySection } from "./queries/get-by-section";

export function makeComponentsUseCases(dependencies: Dependencies) {
return {
queries: {
listComponentsBySection: makeComponentGetBySection(dependencies),
},
};
}
Loading

0 comments on commit e1eab43

Please sign in to comment.