bsuiness
This commit is contained in:
@@ -1,34 +1,55 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
||||
import { CatalogueService } from './catalogue.service';
|
||||
import { CreateCatalogueDto } from './dto/create-catalogue.dto';
|
||||
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
|
||||
import { FindCataloguesDto } from './dto/find-catalogues.dto';
|
||||
import { AdminAuthGuard } from '../auth/guards/adminAuth.guard';
|
||||
import { BusinessId } from 'src/common/decorators';
|
||||
|
||||
@Controller('catalogue')
|
||||
@Controller()
|
||||
export class CatalogueController {
|
||||
constructor(private readonly catalogueService: CatalogueService) { }
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: CreateCatalogueDto) {
|
||||
return this.catalogueService.create(dto);
|
||||
// ============= Public Routes ==============
|
||||
|
||||
@Get('public/catalogue')
|
||||
findAllAsPublic(@Query() queryDto: FindCataloguesDto) {
|
||||
return this.catalogueService.findAll(queryDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@Get('admin/catalogue/:id')
|
||||
findOneAsPublic(@Param('id') id: string) {
|
||||
return this.catalogueService.findOne(id);
|
||||
}
|
||||
|
||||
// ============= Admin Routes ==============
|
||||
|
||||
@Post('admin/catalogue')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
create(@Body() dto: CreateCatalogueDto, @BusinessId() busId: string) {
|
||||
return this.catalogueService.create(dto, busId);
|
||||
}
|
||||
|
||||
@Get('admin/catalogue')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
findAll(@Query() queryDto: FindCataloguesDto) {
|
||||
return this.catalogueService.findAll(queryDto);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@Get('admin/catalogue/:id')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.catalogueService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@Patch('admin/catalogue/:id')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
update(@Param('id') id: string, @Body() dto: UpdateCatalogueDto) {
|
||||
return this.catalogueService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@Delete('admin/catalogue/:id')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
remove(@Param('id') id: string) {
|
||||
return this.catalogueService.remove(id);
|
||||
}
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateCatalogueDto } from './dto/create-catalogue.dto';
|
||||
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { Catalogue } from './entities/catalogue.entity';
|
||||
import { Business } from '../business/entities/business.entity';
|
||||
import { CatalogueStatus } from './enums/company-status.enum';
|
||||
import { CatalogueRepository } from './repositories/catalogue.repository';
|
||||
import { FindCataloguesDto } from './dto/find-catalogues.dto';
|
||||
import { BusinessService } from '../business/business.service';
|
||||
|
||||
@Injectable()
|
||||
export class CatalogueService {
|
||||
constructor(
|
||||
private readonly em: EntityManager,
|
||||
private readonly catalogueRepository: CatalogueRepository
|
||||
private readonly catalogueRepository: CatalogueRepository,
|
||||
private readonly businessService: BusinessService,
|
||||
) { }
|
||||
|
||||
async create(dto: CreateCatalogueDto) {
|
||||
const business = await this.em.findOne(Business, {})
|
||||
if (!business) {
|
||||
throw new BadRequestException()
|
||||
}
|
||||
async create(dto: CreateCatalogueDto, businessId: string) {
|
||||
const business = await this.businessService.findByIdOrFail(businessId)
|
||||
|
||||
const catalogue = this.em.create(Catalogue,
|
||||
{ ...dto, business, status: CatalogueStatus.PENDING })
|
||||
|
||||
await this.em.persistAndFlush(catalogue)
|
||||
|
||||
return catalogue
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user