This commit is contained in:
2025-12-07 10:26:09 +03:30
parent be0185e6bd
commit f762b03260
4 changed files with 14 additions and 9 deletions
@@ -1,21 +1,26 @@
import { Entity, ManyToOne, Property } from '@mikro-orm/core'; import { Entity, ManyToOne, Property, Unique } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity'; import { BaseEntity } from '../../../common/entities/base.entity';
import { Food } from '../../foods/entities/food.entity'; import { Food } from '../../foods/entities/food.entity';
import { User } from '../../users/entities/user.entity'; import { User } from '../../users/entities/user.entity';
import { Order } from 'src/modules/orders/entities/order.entity';
@Entity({ tableName: 'food_comments' }) @Entity({ tableName: 'food_comments' })
@Unique({ properties: ['order', 'food', 'user'] })
export class FoodComment extends BaseEntity { export class FoodComment extends BaseEntity {
@ManyToOne(() => Order)
order: Order;
@ManyToOne(() => Food) @ManyToOne(() => Food)
food!: Food; food: Food;
@ManyToOne(() => User) @ManyToOne(() => User)
user!: User; user: User;
@Property({ type: 'text', nullable: true }) @Property({ type: 'text', nullable: true })
comment?: string; comment?: string;
@Property({ type: 'int', nullable: false }) @Property({ type: 'int', nullable: false })
rating!: number; // 1-5 rating: number = 0;
@Property({ type: 'boolean', default: false }) @Property({ type: 'boolean', default: false })
isApproved: boolean = false; isApproved: boolean = false;
@@ -68,7 +68,7 @@ export class FoodCommentService {
} }
async findById(id: string): Promise<FoodComment> { async findById(id: string): Promise<FoodComment> {
const comment = await this.foodCommentRepository.findOne({ id }, { populate: ['food', 'user'] }); const comment = await this.foodCommentRepository.findOne({ id }, { populate: ['food', 'user', 'order'] });
if (!comment) { if (!comment) {
throw new NotFoundException(FoodCommentMessage.NOT_FOUND); throw new NotFoundException(FoodCommentMessage.NOT_FOUND);
} }
@@ -76,7 +76,7 @@ export class FoodCommentService {
} }
async update(id: string, userId: string, dto: UpdateFoodCommentDto, isAdmin: boolean = false): Promise<FoodComment> { async update(id: string, userId: string, dto: UpdateFoodCommentDto, isAdmin: boolean = false): Promise<FoodComment> {
const comment = await this.foodCommentRepository.findOne({ id }, { populate: ['food', 'user'] }); const comment = await this.foodCommentRepository.findOne({ id }, { populate: ['food', 'user', 'order'] });
if (!comment) { if (!comment) {
throw new NotFoundException(FoodCommentMessage.NOT_FOUND); throw new NotFoundException(FoodCommentMessage.NOT_FOUND);
} }
@@ -104,7 +104,7 @@ export class FoodCommentService {
} }
async remove(id: string, userId: string, isAdmin: boolean = false) { async remove(id: string, userId: string, isAdmin: boolean = false) {
const comment = await this.foodCommentRepository.findOne({ id }, { populate: ['food', 'user'] }); const comment = await this.foodCommentRepository.findOne({ id }, { populate: ['food', 'user', 'order'] });
if (!comment) { if (!comment) {
throw new NotFoundException(FoodCommentMessage.NOT_FOUND); throw new NotFoundException(FoodCommentMessage.NOT_FOUND);
} }
@@ -47,7 +47,7 @@ export class FoodCommentRepository extends EntityRepository<FoodComment> {
limit, limit,
offset, offset,
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' }, orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
populate: ['food', 'user'], populate: ['food', 'user', 'order'],
}); });
const totalPages = Math.ceil(total / limit); const totalPages = Math.ceil(total / limit);