add user role components
This commit is contained in:
@@ -16,6 +16,7 @@ import { Event } from './entity/event.entity';
|
||||
import { EventsModule } from './event/events.module';
|
||||
import { User } from './entity/user';
|
||||
import { UserGroupsModule } from './user-group/user-group.module';
|
||||
import { UserRolesModule } from './user-role/user-role.module';
|
||||
|
||||
const moduleTypeOrm = TypeOrmModule.forRootAsync({
|
||||
imports: [ConfigModule],
|
||||
@@ -46,6 +47,7 @@ const moduleTypeOrm = TypeOrmModule.forRootAsync({
|
||||
ProductsModule,
|
||||
EventsModule,
|
||||
UserGroupsModule,
|
||||
UserRolesModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
|
||||
4
server/src/user-role/dto/create-user-role.dto.ts
Normal file
4
server/src/user-role/dto/create-user-role.dto.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { OmitType } from '@nestjs/mapped-types';
|
||||
import { UserRole } from '../../entity/user-role';
|
||||
|
||||
export class CreateUserRoleDto extends OmitType(UserRole, ['id']) {}
|
||||
14
server/src/user-role/dto/query-user-role.dto.ts
Normal file
14
server/src/user-role/dto/query-user-role.dto.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import {
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsNumber,
|
||||
IsIn,
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class QueryUserRoleDto {
|
||||
@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-role/dto/update-user-role.dto.ts
Normal file
4
server/src/user-role/dto/update-user-role.dto.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateUserRoleDto } from './create-user-role.dto';
|
||||
|
||||
export class UpdateUserRoleDto extends PartialType(CreateUserRoleDto) {}
|
||||
63
server/src/user-role/user-role.controller.ts
Normal file
63
server/src/user-role/user-role.controller.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Body,
|
||||
Patch,
|
||||
Param,
|
||||
Delete,
|
||||
Query,
|
||||
ParseIntPipe,
|
||||
DefaultValuePipe,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { UserRolesService } from './user-role.service';
|
||||
import { CreateUserRoleDto } from './dto/create-user-role.dto';
|
||||
import { UpdateUserRoleDto } from './dto/update-user-role.dto';
|
||||
import { QueryUserRoleDto } from './dto/query-user-role.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-role')
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles(Role.Admin)
|
||||
export class UserRolesController {
|
||||
constructor(private readonly userRolesService: UserRolesService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createUserRoleDto: CreateUserRoleDto) {
|
||||
return this.userRolesService.create(createUserRoleDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Query() queryParams: QueryUserRoleDto) {
|
||||
return this.userRolesService.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.userRolesService.search(term, { page, limit });
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.userRolesService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id', ParseIntPipe) id: number, @Body() updateUserRoleDto: UpdateUserRoleDto) {
|
||||
return this.userRolesService.update(id, updateUserRoleDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.userRolesService.remove(id);
|
||||
}
|
||||
}
|
||||
12
server/src/user-role/user-role.module.ts
Normal file
12
server/src/user-role/user-role.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserRolesService } from './user-role.service';
|
||||
import { UserRolesController } from './user-role.controller';
|
||||
import { UserRole } from '../entity/user-role';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([UserRole])],
|
||||
controllers: [UserRolesController],
|
||||
providers: [UserRolesService],
|
||||
})
|
||||
export class UserRolesModule {}
|
||||
125
server/src/user-role/user-role.service.ts
Normal file
125
server/src/user-role/user-role.service.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, FindManyOptions, FindOptionsWhere, ILike } from 'typeorm';
|
||||
import { CreateUserRoleDto } from './dto/create-user-role.dto';
|
||||
import { UpdateUserRoleDto } from './dto/update-user-role.dto';
|
||||
import { QueryUserRoleDto } from './dto/query-user-role.dto';
|
||||
import { UserRole } from '../entity/user-role';
|
||||
|
||||
type QueryConfigItem = {
|
||||
param: keyof Omit<QueryUserRoleDto, 'page' | 'limit' | 'sortBy' | 'order'>;
|
||||
dbField: keyof UserRole;
|
||||
operator: 'equals' | 'like';
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class UserRolesService {
|
||||
constructor(
|
||||
@InjectRepository(UserRole)
|
||||
private readonly userRoleRepository: Repository<UserRole>,
|
||||
) {}
|
||||
|
||||
private readonly searchableFields: (keyof UserRole)[] = ['name'];
|
||||
|
||||
create(createUserRoleDto: CreateUserRoleDto) {
|
||||
const newRecord = this.userRoleRepository.create(createUserRoleDto);
|
||||
return this.userRoleRepository.save(newRecord);
|
||||
}
|
||||
|
||||
async findAll(queryParams: QueryUserRoleDto) {
|
||||
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<UserRole> = {
|
||||
where: whereClause as FindOptionsWhere<UserRole>,
|
||||
};
|
||||
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.userRoleRepository.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.userRoleRepository.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.userRoleRepository.findOneBy({ id: id });
|
||||
if (!record) {
|
||||
throw new NotFoundException(`UserRole with ID ${id} not found`);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
async update(id: number, updateUserRoleDto: UpdateUserRoleDto) {
|
||||
const record = await this.findOne(id);
|
||||
Object.assign(record, updateUserRoleDto);
|
||||
return this.userRoleRepository.save(record);
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
const result = await this.userRoleRepository.delete(id);
|
||||
if (result.affected === 0) {
|
||||
throw new NotFoundException(`UserRole with ID ${id} not found`);
|
||||
}
|
||||
return { deleted: true, id };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user