Files
dmenu-api/src/modules/restaurants/providers/restaurants.service.ts
T

68 lines
1.8 KiB
TypeScript

import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateRestaurantDto } from '../dto/create-restaurant.dto';
import { UpdateRestaurantDto } from '../dto/update-restaurant.dto';
import { Restaurant } from '../entities/restaurant.entity';
import { EntityManager } from '@mikro-orm/postgresql';
import { RestRepository } from '../repositories/rest.repository';
import { RestMessage } from 'src/common/enums/message.enum';
@Injectable()
export class RestaurantsService {
constructor(
private readonly em: EntityManager,
private readonly restRepository: RestRepository,
) {}
async create(dto: CreateRestaurantDto): Promise<Restaurant> {
const restaurant = this.em.create(Restaurant, {
...dto,
isActive: true,
});
await this.em.persistAndFlush(restaurant);
return restaurant;
}
findAll() {
return this.restRepository.findAll();
}
async findBySlug(slug: string): Promise<Restaurant> {
const restaurant = await this.em.findOne(Restaurant, { slug });
if (!restaurant) {
throw new NotFoundException(`Restaurant with slug "${slug}" not found`);
}
return restaurant;
}
async update(id: string, dto: UpdateRestaurantDto): Promise<Restaurant> {
const restaurant = await this.restRepository.findOne({ id });
if (!restaurant) {
throw new NotFoundException(RestMessage.NOT_FOUND);
}
this.restRepository.assign(restaurant, dto);
await this.em.persistAndFlush(restaurant);
return restaurant;
}
async remove(id: string) {
// Soft delete the restaurant by setting isActive to false
const restaurant = await this.restRepository.findOne({ id });
if (!restaurant) {
throw new NotFoundException(RestMessage.NOT_FOUND);
}
restaurant.isActive = false;
await this.em.persistAndFlush(restaurant);
return restaurant;
}
}