Merge branch 'master' into dev
This commit is contained in:
@@ -23,6 +23,7 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
|
||||
import { PagerModule } from './modules/pager/pager.module';
|
||||
import { ContactModule } from './modules/contact/contact.module';
|
||||
import { InventoryModule } from './modules/inventory/inventory.module';
|
||||
import { IconsModule } from './modules/icons/icons.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -54,6 +55,7 @@ import { InventoryModule } from './modules/inventory/inventory.module';
|
||||
PagerModule,
|
||||
ContactModule,
|
||||
InventoryModule,
|
||||
IconsModule,
|
||||
],
|
||||
controllers: [],
|
||||
providers: [],
|
||||
|
||||
@@ -649,3 +649,17 @@ export const enum ReviewMessage {
|
||||
COMMENT_UPDATED = 'نظر با موفقیت بهروزرسانی شد',
|
||||
COMMENT_DELETED = 'نظر با موفقیت حذف شد',
|
||||
}
|
||||
|
||||
export const enum IconMessage {
|
||||
NOT_FOUND = 'آیکون یافت نشد',
|
||||
NOT_CREATED = 'ایجاد آیکون با خطا مواجه شد',
|
||||
NOT_UPDATED = 'بهروزرسانی آیکون با خطا مواجه شد',
|
||||
NOT_DELETED = 'حذف آیکون با خطا مواجه شد',
|
||||
}
|
||||
|
||||
export const enum GroupMessage {
|
||||
NOT_FOUND = 'گروه آیکون یافت نشد',
|
||||
NOT_CREATED = 'ایجاد گروه آیکون با خطا مواجه شد',
|
||||
NOT_UPDATED = 'بهروزرسانی گروه آیکون با خطا مواجه شد',
|
||||
NOT_DELETED = 'حذف گروه آیکون با خطا مواجه شد',
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { IconService } from '../providers/icon.service';
|
||||
import { GroupService } from '../providers/group.service';
|
||||
import { CreateIconDto } from '../dto/create-icon.dto';
|
||||
import { UpdateIconDto } from '../dto/update-icon.dto';
|
||||
import { CreateGroupDto } from '../dto/create-group.dto';
|
||||
import { UpdateGroupDto } from '../dto/update-group.dto';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
ApiCreatedResponse,
|
||||
ApiOkResponse,
|
||||
ApiNotFoundResponse,
|
||||
ApiParam,
|
||||
ApiBody,
|
||||
ApiBearerAuth,
|
||||
} from '@nestjs/swagger';
|
||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
import { UseGuards } from '@nestjs/common';
|
||||
|
||||
@ApiTags('icons')
|
||||
@Controller('admin/icons')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
export class IconsController {
|
||||
constructor(
|
||||
private readonly iconService: IconService,
|
||||
private readonly groupService: GroupService,
|
||||
) {}
|
||||
|
||||
// Icon endpoints
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create icon' })
|
||||
@ApiCreatedResponse({ description: 'Icon created', type: CreateIconDto })
|
||||
@ApiBody({ type: CreateIconDto })
|
||||
createIcon(@Body() dto: CreateIconDto) {
|
||||
return this.iconService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'Get all icons' })
|
||||
@ApiOkResponse({ description: 'List of all icons' })
|
||||
findAllIcons() {
|
||||
return this.iconService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get icon by id' })
|
||||
@ApiOkResponse({ description: 'Icon detail', type: CreateIconDto })
|
||||
@ApiNotFoundResponse({ description: 'Icon not found' })
|
||||
@ApiParam({ name: 'id', required: true, type: String })
|
||||
findOneIcon(@Param('id') id: string) {
|
||||
return this.iconService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'Update icon' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiBody({ type: UpdateIconDto })
|
||||
@ApiOkResponse({ description: 'Updated icon', type: UpdateIconDto })
|
||||
updateIcon(@Param('id') id: string, @Body() dto: UpdateIconDto) {
|
||||
return this.iconService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'Delete icon' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiOkResponse({ description: 'Deleted' })
|
||||
removeIcon(@Param('id') id: string) {
|
||||
return this.iconService.remove(id);
|
||||
}
|
||||
|
||||
// Group endpoints (must come before :id routes to avoid route conflicts)
|
||||
@Post('groups')
|
||||
@ApiOperation({ summary: 'Create icon group' })
|
||||
@ApiCreatedResponse({ description: 'Group created', type: CreateGroupDto })
|
||||
@ApiBody({ type: CreateGroupDto })
|
||||
createGroup(@Body() dto: CreateGroupDto) {
|
||||
return this.groupService.create(dto);
|
||||
}
|
||||
|
||||
@Get('groups')
|
||||
@ApiOperation({ summary: 'Get all icon groups' })
|
||||
@ApiOkResponse({ description: 'List of all icon groups' })
|
||||
findAllGroups() {
|
||||
return this.groupService.findAll();
|
||||
}
|
||||
|
||||
@Get('groups/:id')
|
||||
@ApiOperation({ summary: 'Get icon group by id' })
|
||||
@ApiOkResponse({ description: 'Group detail', type: CreateGroupDto })
|
||||
@ApiNotFoundResponse({ description: 'Group not found' })
|
||||
@ApiParam({ name: 'id', required: true, type: String })
|
||||
findOneGroup(@Param('id') id: string) {
|
||||
return this.groupService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch('groups/:id')
|
||||
@ApiOperation({ summary: 'Update icon group' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiBody({ type: UpdateGroupDto })
|
||||
@ApiOkResponse({ description: 'Updated group', type: UpdateGroupDto })
|
||||
updateGroup(@Param('id') id: string, @Body() dto: UpdateGroupDto) {
|
||||
return this.groupService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete('groups/:id')
|
||||
@ApiOperation({ summary: 'Delete icon group' })
|
||||
@ApiParam({ name: 'id' })
|
||||
@ApiOkResponse({ description: 'Deleted' })
|
||||
removeGroup(@Param('id') id: string) {
|
||||
return this.groupService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { IsString, IsOptional, IsBoolean } from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class CreateGroupDto {
|
||||
@IsString()
|
||||
@ApiProperty({ example: 'Social Media' })
|
||||
name!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@Type(() => Boolean)
|
||||
@ApiPropertyOptional({ example: true })
|
||||
isActive?: boolean;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import { IsString, IsOptional, IsBoolean } from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class CreateIconDto {
|
||||
@IsString()
|
||||
@ApiProperty({ example: 'Facebook Icon' })
|
||||
name!: string;
|
||||
|
||||
@IsString()
|
||||
@ApiProperty({ example: 'https://example.com/icons/facebook.svg' })
|
||||
url!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@Type(() => Boolean)
|
||||
@ApiPropertyOptional({ example: true })
|
||||
isActive?: boolean;
|
||||
|
||||
@IsString()
|
||||
@ApiProperty({ example: 'group-id-here' })
|
||||
groupId!: string;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateGroupDto } from './create-group.dto';
|
||||
|
||||
export class UpdateGroupDto extends PartialType(CreateGroupDto) {}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger';
|
||||
import { CreateIconDto } from './create-icon.dto';
|
||||
|
||||
export class UpdateIconDto extends PartialType(CreateIconDto) {}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Entity, Index, Property, Collection, OneToMany } from '@mikro-orm/core';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Icon } from './icon.entity';
|
||||
|
||||
@Entity({ tableName: 'icon_groups' })
|
||||
@Index({ properties: ['isActive'] })
|
||||
export class Group extends BaseEntity {
|
||||
@Property()
|
||||
name!: string;
|
||||
|
||||
@Property({ default: true })
|
||||
isActive: boolean = true;
|
||||
|
||||
@OneToMany(() => Icon, icon => icon.group)
|
||||
icons = new Collection<Icon>(this);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Entity, Index, Property, ManyToOne } from '@mikro-orm/core';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Group } from './group.entity';
|
||||
|
||||
@Entity({ tableName: 'icons' })
|
||||
@Index({ properties: ['isActive'] })
|
||||
@Index({ properties: ['group', 'isActive'] })
|
||||
export class Icon extends BaseEntity {
|
||||
@Property()
|
||||
name!: string;
|
||||
|
||||
@Property()
|
||||
url!: string;
|
||||
|
||||
@Property({ default: true })
|
||||
isActive: boolean = true;
|
||||
|
||||
@ManyToOne(() => Group)
|
||||
group!: Group;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MikroOrmModule } from '@mikro-orm/nestjs';
|
||||
import { Icon } from './entities/icon.entity';
|
||||
import { Group } from './entities/group.entity';
|
||||
import { IconService } from './providers/icon.service';
|
||||
import { GroupService } from './providers/group.service';
|
||||
import { IconRepository } from './repositories/icon.repository';
|
||||
import { GroupRepository } from './repositories/group.repository';
|
||||
import { IconsController } from './controllers/icons.controller';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
|
||||
@Module({
|
||||
imports: [MikroOrmModule.forFeature([Icon, Group]), JwtModule],
|
||||
controllers: [IconsController],
|
||||
providers: [IconService, GroupService, IconRepository, GroupRepository],
|
||||
exports: [IconRepository, GroupRepository, IconService, GroupService],
|
||||
})
|
||||
export class IconsModule {}
|
||||
@@ -0,0 +1,59 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateGroupDto } from '../dto/create-group.dto';
|
||||
import { UpdateGroupDto } from '../dto/update-group.dto';
|
||||
import { GroupRepository } from '../repositories/group.repository';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
import { Group } from '../entities/group.entity';
|
||||
import { GroupMessage } from 'src/common/enums/message.enum';
|
||||
|
||||
@Injectable()
|
||||
export class GroupService {
|
||||
constructor(
|
||||
private readonly groupRepository: GroupRepository,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async create(dto: CreateGroupDto): Promise<Group> {
|
||||
const data: RequiredEntityData<Group> = {
|
||||
name: dto.name,
|
||||
isActive: dto.isActive ?? true,
|
||||
};
|
||||
|
||||
const group = this.groupRepository.create(data);
|
||||
await this.em.persistAndFlush(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
async findAll(): Promise<Group[]> {
|
||||
return this.groupRepository.find({}, { populate: ['icons'] });
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<Group> {
|
||||
const group = await this.groupRepository.findOne({ id }, { populate: ['icons'] });
|
||||
if (!group) {
|
||||
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateGroupDto): Promise<Group> {
|
||||
const group = await this.groupRepository.findOne({ id });
|
||||
if (!group) {
|
||||
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
||||
}
|
||||
this.em.assign(group, dto);
|
||||
await this.em.persistAndFlush(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
const group = await this.groupRepository.findOne({ id });
|
||||
if (!group) {
|
||||
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
||||
}
|
||||
group.deletedAt = new Date();
|
||||
await this.em.persistAndFlush(group);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateIconDto } from '../dto/create-icon.dto';
|
||||
import { UpdateIconDto } from '../dto/update-icon.dto';
|
||||
import { IconRepository } from '../repositories/icon.repository';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { RequiredEntityData } from '@mikro-orm/core';
|
||||
import { Icon } from '../entities/icon.entity';
|
||||
import { IconMessage, GroupMessage } from 'src/common/enums/message.enum';
|
||||
import { Group } from '../entities/group.entity';
|
||||
|
||||
@Injectable()
|
||||
export class IconService {
|
||||
constructor(
|
||||
private readonly iconRepository: IconRepository,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async create(dto: CreateIconDto): Promise<Icon> {
|
||||
const group = await this.em.findOne(Group, { id: dto.groupId });
|
||||
if (!group) {
|
||||
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
const data: RequiredEntityData<Icon> = {
|
||||
name: dto.name,
|
||||
url: dto.url,
|
||||
isActive: dto.isActive ?? true,
|
||||
group: group,
|
||||
};
|
||||
|
||||
const icon = this.iconRepository.create(data);
|
||||
await this.em.persistAndFlush(icon);
|
||||
return icon;
|
||||
}
|
||||
|
||||
async findAll(): Promise<Icon[]> {
|
||||
return this.iconRepository.find({}, { populate: ['group'] });
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<Icon> {
|
||||
const icon = await this.iconRepository.findOne({ id }, { populate: ['group'] });
|
||||
if (!icon) {
|
||||
throw new NotFoundException(IconMessage.NOT_FOUND);
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateIconDto): Promise<Icon> {
|
||||
const icon = await this.iconRepository.findOne({ id });
|
||||
if (!icon) {
|
||||
throw new NotFoundException(IconMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
if (dto.groupId) {
|
||||
const group = await this.em.findOne(Group, { id: dto.groupId });
|
||||
if (!group) {
|
||||
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
||||
}
|
||||
icon.group = group;
|
||||
}
|
||||
|
||||
this.em.assign(icon, {
|
||||
name: dto.name,
|
||||
url: dto.url,
|
||||
isActive: dto.isActive,
|
||||
});
|
||||
|
||||
await this.em.persistAndFlush(icon);
|
||||
return icon;
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
const icon = await this.iconRepository.findOne({ id });
|
||||
if (!icon) {
|
||||
throw new NotFoundException(IconMessage.NOT_FOUND);
|
||||
}
|
||||
icon.deletedAt = new Date();
|
||||
await this.em.persistAndFlush(icon);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
||||
import { Group } from '../entities/group.entity';
|
||||
|
||||
@Injectable()
|
||||
export class GroupRepository extends EntityRepository<Group> {
|
||||
constructor(readonly em: EntityManager) {
|
||||
super(em, Group);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
|
||||
import { Icon } from '../entities/icon.entity';
|
||||
|
||||
@Injectable()
|
||||
export class IconRepository extends EntityRepository<Icon> {
|
||||
constructor(readonly em: EntityManager) {
|
||||
super(em, Icon);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user