-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): list API now support Pagination and Search
- Loading branch information
Showing
33 changed files
with
487 additions
and
98 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
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,4 @@ | ||
export * from './pagination'; | ||
export * from './pagination-params'; | ||
export * from './crud.service'; | ||
export * from './crud.controller'; |
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,39 @@ | ||
import { ApiModelProperty, ApiModelPropertyOptional } from '@nestjs/swagger'; | ||
import { Transform } from 'class-transformer'; | ||
import { IsOptional, Max, Min } from 'class-validator'; | ||
|
||
export enum OrderType { | ||
DESC = 'DESC', | ||
ASC = 'ASC', | ||
} | ||
|
||
/** | ||
* Describes generic pagination params | ||
*/ | ||
export abstract class PaginationParams<T> { | ||
/** | ||
* Pagination limit | ||
*/ | ||
@ApiModelPropertyOptional({ type: Number, minimum: 0, maximum: 50 }) | ||
@IsOptional() | ||
@Min(0) | ||
@Max(50) | ||
@Transform((val: string) => parseInt(val, 10)) | ||
readonly take = 10; | ||
|
||
/** | ||
* Pagination offset | ||
*/ | ||
@ApiModelPropertyOptional({ type: Number, minimum: 0 }) | ||
@IsOptional() | ||
@Min(0) | ||
@Transform((val: string) => parseInt(val, 10)) | ||
readonly skip = 0; | ||
|
||
/** | ||
* OrderBy | ||
*/ | ||
@ApiModelPropertyOptional() | ||
@IsOptional() | ||
abstract readonly order?: { [P in keyof T]?: OrderType }; | ||
} |
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,14 @@ | ||
/** | ||
* Generic pagination interface | ||
*/ | ||
export interface IPagination<T> { | ||
/** | ||
* Items included in the current listing | ||
*/ | ||
readonly items: T[]; | ||
|
||
/** | ||
* Total number of available items | ||
*/ | ||
readonly total: number; | ||
} |
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,5 +1,5 @@ | ||
export * from './crud'; | ||
export * from './core.module'; | ||
export * from './services/base-remote.service'; | ||
export * from './crud/crud.service'; | ||
export * from './context/request-context'; | ||
export * from './crud/crud.controller'; | ||
|
Empty file.
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,30 @@ | ||
import { ArgumentMetadata, Injectable, NotFoundException, PipeTransform } from '@nestjs/common'; | ||
import { Validator } from 'class-validator'; | ||
|
||
/** | ||
* UUID Validation Pipe | ||
* | ||
* Validates UUID passed in request parameters. | ||
*/ | ||
@Injectable() | ||
export class UUIDValidationPipe implements PipeTransform { | ||
/** | ||
* Instance of class-validator | ||
* | ||
* Can not be easily injected, and there's no need to do so as we | ||
* only use it for uuid validation method. | ||
*/ | ||
private readonly validator: Validator = new Validator(); | ||
|
||
/** | ||
* When user requests an entity with invalid UUID we must return 404 | ||
* error before reaching into the database. | ||
*/ | ||
public transform(value: string, metadata: ArgumentMetadata): string { | ||
if (!this.validator.isUUID(value)) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return value; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
apps/api/src/app/notifications/notification/dto/find-notifications.dto.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,45 @@ | ||
import { OrderType, PaginationParams } from '../../../core'; | ||
import { Notification, TargetType } from '../notification.entity'; | ||
import { ApiModelPropertyOptional } from '@nestjs/swagger'; | ||
import { IsAscii, IsBoolean, IsEnum, IsNotEmpty, IsOptional, MaxLength, MinLength } from 'class-validator'; | ||
import { Transform } from 'class-transformer'; | ||
|
||
export class FindNotificationsDto extends PaginationParams<Notification> { | ||
@ApiModelPropertyOptional({ type: String, minLength: 3, maxLength: 50 }) | ||
@IsOptional() | ||
@IsNotEmpty() | ||
@IsAscii() | ||
@MinLength(3) | ||
@MaxLength(50) | ||
readonly target?: any; | ||
|
||
@ApiModelPropertyOptional({ type: String, /*enum: TargetType */ enum: ['all', 'user', 'topic'] }) | ||
@IsOptional() | ||
@IsNotEmpty() | ||
@IsEnum(TargetType) | ||
readonly targetType?: string; | ||
|
||
@ApiModelPropertyOptional({ type: Boolean, default: false }) | ||
@Transform((val: string) => ( val === 'true' )) | ||
@IsOptional() | ||
@IsBoolean() | ||
readonly read = false; | ||
|
||
@ApiModelPropertyOptional({ type: Boolean, default: true }) | ||
@Transform((val: string) => ( val === 'true' )) | ||
@IsOptional() | ||
@IsBoolean() | ||
readonly isActive = true; | ||
|
||
@ApiModelPropertyOptional({ type: String, enum: ['ASC', 'DESC'] }) | ||
@IsOptional() | ||
@Transform((val: string) => ({ createdAt: val === OrderType.ASC ? OrderType.ASC : OrderType.DESC })) | ||
readonly order = { | ||
createdAt: OrderType.DESC, | ||
}; | ||
|
||
constructor(values: Partial<FindNotificationsDto>) { | ||
super(); | ||
Object.assign(this, values); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
apps/api/src/app/notifications/notification/dto/find-own-notifications.dto.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,14 @@ | ||
import { OrderType, PaginationParams } from '../../../core'; | ||
import { Notification } from '../notification.entity'; | ||
import { ApiModelPropertyOptional } from '@nestjs/swagger'; | ||
import { IsOptional } from 'class-validator'; | ||
import { Transform } from 'class-transformer'; | ||
|
||
export class FindOwnNotificationsDto extends PaginationParams<Notification> { | ||
@ApiModelPropertyOptional({ type: String, enum: ['ASC', 'DESC'] }) | ||
@IsOptional() | ||
@Transform((val: string) => ({ createdAt: val === OrderType.ASC ? OrderType.ASC : OrderType.DESC })) | ||
readonly order = { | ||
createdAt: OrderType.DESC, | ||
}; | ||
} |
7 changes: 7 additions & 0 deletions
7
apps/api/src/app/notifications/notification/dto/notification-list.model.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,7 @@ | ||
import { IPagination } from '../../../core'; | ||
import { Notification } from '../notification.entity'; | ||
|
||
export class NotificationList implements IPagination<Notification> { | ||
readonly items: Notification[]; | ||
readonly total: number; | ||
} |
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.