-
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.
- Loading branch information
1 parent
e92ca5e
commit d36cca5
Showing
6 changed files
with
189 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,37 @@ | ||
import { Controller, Get, Query } from "@nestjs/common"; | ||
import { Controller, Get } from "@nestjs/common"; | ||
import { Validate } from "nestjs-typebox"; | ||
|
||
import { | ||
AllCategoriesResponse, | ||
allCategoriesSchema, | ||
} from "../schemas/categoty.schema"; | ||
import { BaseResponse, baseResponse, QueryParamsSchema } from "src/common"; | ||
import { | ||
basePaginatedResponse, | ||
BasePaginatedResponse, | ||
BasePagination, | ||
PaginationValidation, | ||
} from "src/common"; | ||
import { CategorieService } from "../categories.service"; | ||
import { CategoriesQuery } from "./types"; | ||
|
||
@Controller("categories") | ||
export class CategorieController { | ||
constructor(private readonly categoriesService: CategorieService) {} | ||
|
||
@Get() | ||
@Validate({ | ||
// request: [{ type: "query", name: "categories", schema: QueryParamsSchema }], | ||
response: baseResponse(allCategoriesSchema), | ||
request: PaginationValidation, | ||
response: basePaginatedResponse(allCategoriesSchema), | ||
}) | ||
async getAllCategories( | ||
@Query() query: CategoriesQuery, | ||
): Promise<BaseResponse<AllCategoriesResponse>> { | ||
const categories = await this.categoriesService.getCategories(query); | ||
filter?: string, | ||
limit?: number, | ||
offset?: number, | ||
sort?: string, | ||
): Promise<BasePaginatedResponse<AllCategoriesResponse, BasePagination>> { | ||
const query = { filter, limit, offset, sort }; | ||
|
||
const data = await this.categoriesService.getCategories(query); | ||
|
||
return new BaseResponse(categories) as any; | ||
return new BasePaginatedResponse(data.categories, data.pagination); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,47 +1,55 @@ | ||
// import { asc, count, desc, like, sql } from "drizzle-orm"; | ||
import { count, like, sql } from "drizzle-orm"; | ||
import { Inject, Injectable } from "@nestjs/common"; | ||
|
||
import { DatabasePg } from "src/common"; | ||
import { CategoriesQuery } from "./api/types"; | ||
import { categories, users } from "src/storage/schema"; | ||
import { getDataPagination } from "src/utils/pagination"; | ||
import { categories } from "src/storage/schema"; | ||
import { | ||
addPagination, | ||
DEFAULT_PAGINATION_LIMIT, | ||
getPageAndPageSize, | ||
getSortOptions, | ||
} from "src/utils/pagination"; | ||
|
||
@Injectable() | ||
export class CategorieService { | ||
constructor(@Inject("DB") private readonly db: DatabasePg) {} | ||
|
||
// private createLikeFilter(filter: string) { | ||
// return like(categories.title, `%${filter}%`); | ||
// } | ||
private createLikeFilter(filter: string) { | ||
return like(categories.title, `%${filter}%`); | ||
} | ||
|
||
public async getCategories(query: CategoriesQuery) { | ||
// const { sort = "", limit = 10, offset = 0, filter = "" } = query; | ||
|
||
// const sortOrder = sort.startsWith("-") ? desc : asc; | ||
// const filterCondition = this.createLikeFilter(query.filter as string); | ||
|
||
// const queryDB = this.db | ||
// .select() | ||
// .from(categories) | ||
// .where(like(categories.title, `%${filter}%`)) | ||
// .orderBy(sortOrder(sql`LOWER(${categories.title})`)) | ||
// .limit(limit) | ||
// .offset(offset); | ||
|
||
// const data = await queryDB; | ||
|
||
// const [totalItems] = await this.db | ||
// .select({ count: count() }) | ||
// .from(categories) | ||
// .where(filterCondition); | ||
|
||
//---- external function ------ | ||
const data = await getDataPagination({ | ||
model: categories, | ||
query, | ||
db: this.db, | ||
}); | ||
//----------------------- | ||
return { categories: data.categories, totalItems: data.totalItems }; | ||
const { | ||
sort = "", | ||
limit = DEFAULT_PAGINATION_LIMIT, | ||
offset = 0, | ||
filter = "", | ||
} = query; | ||
|
||
const { sortOrder } = getSortOptions(sort); | ||
const filterCondition = this.createLikeFilter(filter); | ||
|
||
const queryDB = this.db | ||
.select() | ||
.from(categories) | ||
.where(filterCondition) | ||
.orderBy(sortOrder(sql`LOWER(${categories.title})`)); | ||
|
||
const { pageSize, page } = getPageAndPageSize({ limit, offset }); | ||
|
||
const paginatedData = addPagination({ queryDB, page, pageSize }); | ||
|
||
const data = await paginatedData; | ||
|
||
const [totalItems] = await this.db | ||
.select({ count: count() }) | ||
.from(categories) | ||
.where(filterCondition); | ||
|
||
return { | ||
categories: data, | ||
pagination: { totalItems: totalItems.count, page, pageSize }, | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { Type, Static } from "@sinclair/typebox"; | ||
import { commonCategorySchema } from "src/categories/schemas/common-category.schema"; | ||
|
||
export const allCategoriesSchema = Type.Object({ | ||
categories: Type.Array(Type.Pick(commonCategorySchema, ["id", "title"])), | ||
totalItems: Type.Number(), | ||
}); | ||
export const allCategoriesSchema = Type.Array( | ||
Type.Pick(commonCategorySchema, ["id", "title"]), | ||
); | ||
|
||
export type AllCategoriesResponse = Static<typeof allCategoriesSchema>; |
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
Oops, something went wrong.