clean foldering

This commit is contained in:
2025-11-24 20:28:49 +03:30
parent cfc7e1ffb9
commit 26b5d2b561
13 changed files with 76 additions and 118 deletions
@@ -1,24 +0,0 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiOkResponse, ApiBearerAuth } from '@nestjs/swagger';
import { RestaurantShipmentMethodService } from '../providers/restaurant-shipment-method.service';
import { RestaurantShipmentMethod } from '../entities/restaurant-shipment-method.entity';
import { RestId } from 'src/common/decorators/rest-id.decorator';
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
@UseGuards(AuthGuard)
@ApiBearerAuth()
@ApiTags('restaurant-shipment-methods')
@Controller('restaurant-shipment-methods')
export class PublicRestaurantShipmentMethodController {
constructor(private readonly restaurantShipmentMethodService: RestaurantShipmentMethodService) {}
@Get()
@ApiOperation({ summary: 'Get restaurant shipment methods ' })
@ApiOkResponse({
description: 'List of restaurant shipment methods',
type: [RestaurantShipmentMethod],
})
findAll(@RestId() restId: string) {
return this.restaurantShipmentMethodService.findAllForRestaurantId(restId);
}
}
@@ -3,8 +3,8 @@ import { RestaurantsService } from '../providers/restaurants.service';
import { RestaurantSpecificationDto } from '../dto/restaurant-specification.dto';
import { ApiTags, ApiOperation, ApiOkResponse, ApiParam, ApiNotFoundResponse } from '@nestjs/swagger';
@ApiTags('restaurants')
@Controller('restaurants')
@ApiTags('public/restaurants')
@Controller('public/restaurants')
export class PublicRestaurantController {
constructor(private readonly restaurantsService: RestaurantsService) {}
@@ -3,8 +3,8 @@ import { ScheduleService } from '../providers/schedule.service';
import { Schedule } from '../entities/schedule.entity';
import { ApiTags, ApiOperation, ApiOkResponse, ApiParam } from '@nestjs/swagger';
@ApiTags('schedules')
@Controller('schedules')
@ApiTags('public/schedules')
@Controller('public/schedules')
export class PublicScheduleController {
constructor(private readonly scheduleService: ScheduleService) {}
@@ -1,84 +0,0 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards } from '@nestjs/common';
import {
ApiTags,
ApiOperation,
ApiCreatedResponse,
ApiOkResponse,
ApiNotFoundResponse,
ApiParam,
ApiBody,
ApiBearerAuth,
} from '@nestjs/swagger';
import { RestaurantShipmentMethodService } from '../providers/restaurant-shipment-method.service';
import { CreateRestaurantShipmentMethodDto } from '../dto/create-restaurant-shipment-method.dto';
import { UpdateRestaurantShipmentMethodDto } from '../dto/update-restaurant-shipment-method.dto';
import { RestaurantShipmentMethod } from '../entities/restaurant-shipment-method.entity';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { RestId } from 'src/common/decorators/rest-id.decorator';
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@ApiTags('admin/restaurant-shipment-methods')
@Controller('admin/restaurant-shipment-methods')
export class RestaurantShipmentMethodController {
constructor(private readonly restaurantShipmentMethodService: RestaurantShipmentMethodService) {}
@Post()
@ApiOperation({ summary: 'Create a new restaurant shipment method' })
@ApiBody({ type: CreateRestaurantShipmentMethodDto })
@ApiCreatedResponse({
description: 'Restaurant shipment method created successfully',
type: RestaurantShipmentMethod,
})
create(@Body() createDto: CreateRestaurantShipmentMethodDto, @RestId() restId: string) {
return this.restaurantShipmentMethodService.create(restId, createDto);
}
@Get()
@ApiOperation({ summary: 'Get the restaurant shipment methods' })
@ApiOkResponse({
description: 'List of restaurant shipment methods',
type: [RestaurantShipmentMethod],
})
findAll(@RestId() restId: string) {
return this.restaurantShipmentMethodService.findAllForRestaurantId(restId);
}
@Get(':RestaurantShipmentMethodId')
@ApiOperation({ summary: 'Get a restaurant shipment method by shipment method ID' })
@ApiParam({ name: 'RestaurantShipmentMethodId', description: 'RestaurantRestaurantShipmentMethodId method ID' })
@ApiOkResponse({
description: 'Restaurant shipment method details',
type: RestaurantShipmentMethod,
})
@ApiNotFoundResponse({ description: 'Restaurant shipment method not found' })
findOne(@Param('RestaurantShipmentMethodId') RestaurantShipmentMethodId: string, @RestId() restId: string) {
return this.restaurantShipmentMethodService.findOne(restId, RestaurantShipmentMethodId);
}
@Patch(':RestaurantShipmentMethodId')
@ApiOperation({ summary: 'Update a restaurant shipment method' })
@ApiParam({ name: 'RestaurantShipmentMethodId', description: 'Shipment method ID' })
@ApiBody({ type: UpdateRestaurantShipmentMethodDto })
@ApiOkResponse({
description: 'Restaurant shipment method updated successfully',
type: RestaurantShipmentMethod,
})
@ApiNotFoundResponse({ description: 'Restaurant shipment method not found' })
update(
@Param('RestaurantShipmentMethodId') RestaurantShipmentMethodId: string,
@Body() updateDto: UpdateRestaurantShipmentMethodDto,
@RestId() restId: string,
) {
return this.restaurantShipmentMethodService.update(restId, RestaurantShipmentMethodId, updateDto);
}
@Delete(':RestaurantShipmentMethodId')
@ApiOperation({ summary: 'Delete a restaurant shipment method' })
@ApiParam({ name: 'RestaurantShipmentMethodId', description: 'Shipment method ID' })
@ApiOkResponse({ description: 'Restaurant shipment method deleted successfully' })
@ApiNotFoundResponse({ description: 'Restaurant shipment method not found' })
remove(@Param('RestaurantShipmentMethodId') RestaurantShipmentMethodId: string, @RestId() restId: string) {
return this.restaurantShipmentMethodService.remove(restId, RestaurantShipmentMethodId);
}
}
@@ -1,28 +0,0 @@
import { IsNumber, IsBoolean, IsOptional, IsString, Min } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
export class CreateRestaurantShipmentMethodDto {
@ApiProperty({ example: 'shipment-method-id', description: 'Shipment method ID' })
@IsString()
shipmentMethodId!: string;
@ApiProperty({ example: 5000, description: 'Shipping price' })
@IsNumber()
@Min(0)
@Type(() => Number)
price!: number;
@ApiProperty({ example: 100000, description: 'Minimum order price for free shipping' })
@IsNumber()
@Min(0)
@Type(() => Number)
minOrderPrice!: number;
@ApiPropertyOptional({ example: true, description: 'Is this shipment method active?' })
@IsOptional()
@IsBoolean()
@Type(() => Boolean)
isActive?: boolean;
}
@@ -1,5 +0,0 @@
import { PartialType } from '@nestjs/swagger';
import { CreateRestaurantShipmentMethodDto } from './create-restaurant-shipment-method.dto';
export class UpdateRestaurantShipmentMethodDto extends PartialType(CreateRestaurantShipmentMethodDto) {}
@@ -1,23 +0,0 @@
import { Entity, ManyToOne, Property, Unique } from '@mikro-orm/core';
import { Restaurant } from './restaurant.entity';
import { ShipmentMethod } from '../../shipments/entities/shipment-method.entity';
import { BaseEntity } from 'src/common/entities/base.entity';
@Entity({ tableName: 'restaurant_shipment_methods' })
@Unique({ properties: ['restaurant', 'shipmentMethod'] })
export class RestaurantShipmentMethod extends BaseEntity {
@ManyToOne(() => Restaurant, { primary: true })
restaurant!: Restaurant;
@ManyToOne(() => ShipmentMethod, { primary: true })
shipmentMethod!: ShipmentMethod;
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
price: number = 0;
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
minOrderPrice: number = 0;
@Property({ default: true })
isActive: boolean = true;
}
@@ -1,7 +1,7 @@
import { Collection, Entity, ManyToMany, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { ShipmentMethod } from '../../shipments/entities/shipment-method.entity';
import { RestaurantShipmentMethod } from './restaurant-shipment-method.entity';
import { RestaurantShipmentMethod } from '../../shipments/entities/restaurant-shipment-method.entity';
@Entity({ tableName: 'restaurants' })
export class Restaurant extends BaseEntity {
@@ -1,152 +0,0 @@
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
import { EntityManager, RequiredEntityData } from '@mikro-orm/postgresql';
import { RestaurantShipmentMethod } from '../entities/restaurant-shipment-method.entity';
import { RestaurantShipmentMethodRepository } from '../repositories/restaurant-shipment-method.repository';
import { CreateRestaurantShipmentMethodDto } from '../dto/create-restaurant-shipment-method.dto';
import { UpdateRestaurantShipmentMethodDto } from '../dto/update-restaurant-shipment-method.dto';
import { RestRepository } from '../repositories/rest.repository';
import { ShipmentMethod } from '../../shipments/entities/shipment-method.entity';
@Injectable()
export class RestaurantShipmentMethodService {
constructor(
private readonly restaurantShipmentMethodRepository: RestaurantShipmentMethodRepository,
private readonly restRepository: RestRepository,
private readonly em: EntityManager,
) {}
async create(restId: string, createDto: CreateRestaurantShipmentMethodDto): Promise<RestaurantShipmentMethod> {
const restaurant = await this.restRepository.findOne({ id: restId });
if (!restaurant) {
throw new NotFoundException(`رستوران با شناسه ${restId} یافت نشد.`);
}
const shipmentMethod = await this.em.findOne(ShipmentMethod, { id: createDto.shipmentMethodId });
if (!shipmentMethod) {
throw new NotFoundException(`روش ارسال با شناسه ${createDto.shipmentMethodId} یافت نشد.`);
}
// Check if the relationship already exists
const existing = await this.restaurantShipmentMethodRepository.findOne({
restaurant: { id: restId },
shipmentMethod: { id: createDto.shipmentMethodId },
});
if (existing) {
throw new ConflictException('این روش ارسال قبلاً برای این رستوران ثبت شده است.');
}
const data: RequiredEntityData<RestaurantShipmentMethod> = {
restaurant,
shipmentMethod,
price: createDto.price,
minOrderPrice: createDto.minOrderPrice,
isActive: createDto.isActive ?? true,
};
const restaurantShipmentMethod = this.restaurantShipmentMethodRepository.create(data);
await this.em.persistAndFlush(restaurantShipmentMethod);
return restaurantShipmentMethod;
}
async findAllForRestaurantId(restId: string): Promise<RestaurantShipmentMethod[]> {
return this.restaurantShipmentMethodRepository.find(
{ restaurant: { id: restId } },
{ populate: ['shipmentMethod'] },
);
}
async findOne(restId: string, restaurantShipmentMethodId: string): Promise<RestaurantShipmentMethod> {
const restaurantShipmentMethod = await this.restaurantShipmentMethodRepository.findOne(
{
id: restaurantShipmentMethodId,
restaurant: { id: restId },
},
{ populate: ['shipmentMethod'] },
);
if (!restaurantShipmentMethod) {
throw new NotFoundException(
`Restaurant shipment method with ID ${restaurantShipmentMethodId} not found for restaurant with ID ${restId}`,
);
}
return restaurantShipmentMethod;
}
async update(
restId: string,
restaurantShipmentMethodId: string,
updateDto: UpdateRestaurantShipmentMethodDto,
): Promise<RestaurantShipmentMethod> {
const restaurantShipmentMethod = await this.restaurantShipmentMethodRepository.findOne({
restaurant: { id: restId },
id: restaurantShipmentMethodId,
});
if (!restaurantShipmentMethod) {
throw new NotFoundException(
`روش ارسال با شناسه ${restaurantShipmentMethodId} برای رستوران با شناسه ${restId} یافت نشد.`,
);
}
const updateData: Partial<RestaurantShipmentMethod> = {};
if (updateDto.price !== undefined) {
updateData.price = updateDto.price;
}
if (updateDto.minOrderPrice !== undefined) {
updateData.minOrderPrice = updateDto.minOrderPrice;
}
if (updateDto.isActive !== undefined) {
updateData.isActive = updateDto.isActive;
}
// If shipmentMethodId is being updated, we need to check for conflicts
if (
updateDto.shipmentMethodId !== undefined &&
updateDto.shipmentMethodId !== restaurantShipmentMethod.shipmentMethod.id
) {
const newShipmentMethod = await this.em.findOne(ShipmentMethod, { id: updateDto.shipmentMethodId });
if (!newShipmentMethod) {
throw new NotFoundException(`روش ارسال با شناسه ${updateDto.shipmentMethodId} یافت نشد.`);
}
// Check if the new relationship already exists
const existing = await this.restaurantShipmentMethodRepository.findOne({
restaurant: { id: restId },
shipmentMethod: { id: updateDto.shipmentMethodId },
id: { $ne: restaurantShipmentMethodId },
});
if (existing) {
throw new ConflictException('این روش ارسال قبلاً برای این رستوران ثبت شده است.');
}
updateData.shipmentMethod = newShipmentMethod;
}
this.em.assign(restaurantShipmentMethod, updateData);
await this.em.persistAndFlush(restaurantShipmentMethod);
// Reload with populated relations
await this.em.populate(restaurantShipmentMethod, ['shipmentMethod']);
return restaurantShipmentMethod;
}
async remove(restId: string, restaurantShipmentMethodId: string): Promise<void> {
const restaurantShipmentMethod = await this.restaurantShipmentMethodRepository.findOne({
restaurant: { id: restId },
id: restaurantShipmentMethodId,
});
if (!restaurantShipmentMethod) {
throw new NotFoundException(
`روش ارسال با شناسه ${restaurantShipmentMethodId} برای رستوران با شناسه ${restId} یافت نشد.`,
);
}
await this.em.removeAndFlush(restaurantShipmentMethod);
}
}
@@ -1,10 +0,0 @@
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
import { Injectable } from '@nestjs/common';
import { RestaurantShipmentMethod } from '../entities/restaurant-shipment-method.entity';
@Injectable()
export class RestaurantShipmentMethodRepository extends EntityRepository<RestaurantShipmentMethod> {
constructor(readonly em: EntityManager) {
super(em, RestaurantShipmentMethod);
}
}
+3 -27
View File
@@ -7,40 +7,16 @@ import { Restaurant } from './entities/restaurant.entity';
import { RestRepository } from './repositories/rest.repository';
import { ScheduleRepository } from './repositories/schedule.repository';
import { Schedule } from './entities/schedule.entity';
import { ShipmentMethod } from '../shipments/entities/shipment-method.entity';
import { RestaurantShipmentMethod } from './entities/restaurant-shipment-method.entity';
import { ScheduleService } from './providers/schedule.service';
import { ScheduleController } from './controllers/schedule.controller';
import { PublicScheduleController } from './controllers/public-schedule.controller';
import { RestaurantShipmentMethodController } from './controllers/restaurant-shipment-method.controller';
import { RestaurantShipmentMethodService } from './providers/restaurant-shipment-method.service';
import { RestaurantShipmentMethodRepository } from './repositories/restaurant-shipment-method.repository';
import { JwtModule } from '@nestjs/jwt';
import { AuthModule } from '../auth/auth.module';
import { PublicRestaurantShipmentMethodController } from './controllers/public-restaurant-shipment-method.controller';
@Module({
controllers: [
RestaurantsController,
PublicRestaurantController,
ScheduleController,
PublicScheduleController,
RestaurantShipmentMethodController,
PublicRestaurantShipmentMethodController,
],
providers: [
RestaurantsService,
RestRepository,
ScheduleRepository,
ScheduleService,
RestaurantShipmentMethodService,
RestaurantShipmentMethodRepository,
],
imports: [
MikroOrmModule.forFeature([Restaurant, Schedule, ShipmentMethod, RestaurantShipmentMethod]),
JwtModule,
forwardRef(() => AuthModule),
],
controllers: [RestaurantsController, PublicRestaurantController, ScheduleController, PublicScheduleController],
providers: [RestaurantsService, RestRepository, ScheduleRepository, ScheduleService],
imports: [MikroOrmModule.forFeature([Restaurant, Schedule]), JwtModule, forwardRef(() => AuthModule)],
exports: [RestRepository, ScheduleRepository, ScheduleService],
})
export class RestaurantsModule {}