add user group components
This commit is contained in:
4
server/src/user-group/dto/create-user-group.dto.ts
Normal file
4
server/src/user-group/dto/create-user-group.dto.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { OmitType } from '@nestjs/mapped-types';
|
||||
import { UserGroup } from '../../entity/user-group';
|
||||
|
||||
export class CreateUserGroupDto extends OmitType(UserGroup, ['id']) {}
|
||||
14
server/src/user-group/dto/query-user-group.dto.ts
Normal file
14
server/src/user-group/dto/query-user-group.dto.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import {
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsNumber,
|
||||
IsIn,
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class QueryUserGroupDto {
|
||||
@IsOptional() @Type(() => Number) @IsNumber() page?: number;
|
||||
@IsOptional() @Type(() => Number) @IsNumber() limit?: number;
|
||||
@IsOptional() @IsString() sortBy?: string;
|
||||
@IsOptional() @IsIn(['ASC', 'DESC']) order?: 'ASC' | 'DESC';
|
||||
}
|
||||
4
server/src/user-group/dto/update-user-group.dto.ts
Normal file
4
server/src/user-group/dto/update-user-group.dto.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateUserGroupDto } from './create-user-group.dto';
|
||||
|
||||
export class UpdateUserGroupDto extends PartialType(CreateUserGroupDto) {}
|
||||
63
server/src/user-group/user-group.controller.ts
Normal file
63
server/src/user-group/user-group.controller.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
Query,
|
||||
ParseIntPipe,
|
||||
DefaultValuePipe,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { UserGroupsService } from './user-group.service';
|
||||
import { CreateUserGroupDto } from './dto/create-user-group.dto';
|
||||
import { UpdateUserGroupDto } from './dto/update-user-group.dto';
|
||||
import { QueryUserGroupDto } from './dto/query-user-group.dto';
|
||||
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { Roles } from '../auth/roles.decorator';
|
||||
import { Role } from '../auth/role.enum';
|
||||
import { RolesGuard } from '../auth/roles.guard';
|
||||
|
||||
@Controller('user-group')
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles(Role.Admin)
|
||||
export class UserGroupsController {
|
||||
constructor(private readonly userGroupsService: UserGroupsService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createUserGroupDto: CreateUserGroupDto) {
|
||||
return this.userGroupsService.create(createUserGroupDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Query() queryParams: QueryUserGroupDto) {
|
||||
return this.userGroupsService.findAll(queryParams);
|
||||
}
|
||||
|
||||
@Get('search')
|
||||
search(
|
||||
@Query('q') term: string,
|
||||
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
|
||||
@Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number,
|
||||
) {
|
||||
return this.userGroupsService.search(term, { page, limit });
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.userGroupsService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id', ParseIntPipe) id: number, @Body() updateUserGroupDto: UpdateUserGroupDto) {
|
||||
return this.userGroupsService.update(id, updateUserGroupDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.userGroupsService.remove(id);
|
||||
}
|
||||
}
|
||||
13
server/src/user-group/user-group.module.ts
Normal file
13
server/src/user-group/user-group.module.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserGroupsService } from './user-group.service';
|
||||
import { UserGroupsController } from './user-group.controller';
|
||||
import { UserGroup } from '../entity/user-group';
|
||||
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([UserGroup])],
|
||||
controllers: [UserGroupsController],
|
||||
providers: [UserGroupsService],
|
||||
})
|
||||
export class UserGroupsModule {}
|
||||
101
server/src/user-group/user-group.service.ts
Normal file
101
server/src/user-group/user-group.service.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, FindManyOptions, FindOptionsWhere, ILike } from 'typeorm';
|
||||
import { CreateUserGroupDto } from './dto/create-user-group.dto';
|
||||
import { UpdateUserGroupDto } from './dto/update-user-group.dto';
|
||||
import { QueryUserGroupDto } from './dto/query-user-group.dto';
|
||||
import { UserGroup } from '../entity/user-group';
|
||||
|
||||
type QueryConfigItem = {
|
||||
param: keyof Omit<QueryUserGroupDto, 'page' | 'limit' | 'sortBy' | 'order'>;
|
||||
dbField: keyof UserGroup;
|
||||
operator: 'equals' | 'like';
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class UserGroupsService {
|
||||
constructor(
|
||||
@InjectRepository(UserGroup)
|
||||
private readonly userGroupRepository: Repository<UserGroup>,
|
||||
) {}
|
||||
|
||||
private readonly searchableFields: (keyof UserGroup)[] = [
|
||||
'name'
|
||||
];
|
||||
|
||||
create(createUserGroupDto: CreateUserGroupDto) {
|
||||
const newRecord = this.userGroupRepository.create(createUserGroupDto);
|
||||
return this.userGroupRepository.save(newRecord);
|
||||
}
|
||||
|
||||
async findAll(queryParams: QueryUserGroupDto) {
|
||||
const { page = 1, limit = 0, sortBy, order, ...filters } = queryParams;
|
||||
const queryConfig: QueryConfigItem[] = [];
|
||||
const whereClause: { [key: string]: any } = {};
|
||||
for (const config of queryConfig) {
|
||||
if (filters[config.param] !== undefined) {
|
||||
if (config.operator === 'like') {
|
||||
whereClause[config.dbField] = ILike(`%${filters[config.param]}%`);
|
||||
} else {
|
||||
whereClause[config.dbField] = filters[config.param];
|
||||
}
|
||||
}
|
||||
}
|
||||
const findOptions: FindManyOptions<UserGroup> = { where: whereClause as FindOptionsWhere<UserGroup> };
|
||||
const paginated = limit > 0;
|
||||
if (paginated) {
|
||||
findOptions.skip = (page - 1) * limit;
|
||||
findOptions.take = limit;
|
||||
}
|
||||
if (sortBy && order) {
|
||||
findOptions.order = { [sortBy]: order };
|
||||
}
|
||||
const [data, totalItems] = await this.userGroupRepository.findAndCount(findOptions);
|
||||
if (!paginated) {
|
||||
return { data, total: data.length };
|
||||
}
|
||||
return {
|
||||
data,
|
||||
meta: { totalItems, itemCount: data.length, itemsPerPage: limit, totalPages: Math.ceil(totalItems / limit), currentPage: page },
|
||||
};
|
||||
}
|
||||
|
||||
async search(term: string, options: { page: number; limit: number }) {
|
||||
if (this.searchableFields.length === 0) {
|
||||
console.warn('Search is not configured for this entity.');
|
||||
return { data: [], meta: { totalItems: 0, itemCount: 0, itemsPerPage: options.limit, totalPages: 0, currentPage: options.page } };
|
||||
}
|
||||
const whereConditions = this.searchableFields.map(field => ({ [field]: ILike(`%${term}%`) }));
|
||||
const [data, totalItems] = await this.userGroupRepository.findAndCount({
|
||||
where: whereConditions,
|
||||
skip: (options.page - 1) * options.limit,
|
||||
take: options.limit,
|
||||
});
|
||||
return {
|
||||
data,
|
||||
meta: { totalItems, itemCount: data.length, itemsPerPage: options.limit, totalPages: Math.ceil(totalItems / options.limit), currentPage: options.page },
|
||||
};
|
||||
}
|
||||
|
||||
async findOne(id: number) {
|
||||
const record = await this.userGroupRepository.findOneBy({ id: id as any });
|
||||
if (!record) {
|
||||
throw new NotFoundException(`UserGroup with ID ${id} not found`);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
async update(id: number, updateUserGroupDto: UpdateUserGroupDto) {
|
||||
const record = await this.findOne(id);
|
||||
Object.assign(record, updateUserGroupDto);
|
||||
return this.userGroupRepository.save(record);
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
const result = await this.userGroupRepository.delete(id);
|
||||
if (result.affected === 0) {
|
||||
throw new NotFoundException(`UserGroup with ID ${id} not found`);
|
||||
}
|
||||
return { deleted: true, id };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user