import { Body, Controller, Delete, Get, Inject, Param, Post, Put, Request, UseGuards } from '@nestjs/common'; import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; import { Logger } from 'winston'; import { commercials } from '../drizzle/schema.js'; import { FileService } from '../file/file.service.js'; import { OptionalJwtAuthGuard } from '../jwt-auth/optional-jwt-auth.guard.js'; import { ListingCriteria } from '../models/main.model.js'; import { ListingsService } from './listings.service.js'; @Controller('listings/commercialProperty') export class CommercialPropertyListingsController { constructor( private readonly listingsService: ListingsService, private fileService: FileService, @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger, ) {} @Get(':id') findById(@Param('id') id: string): any { return this.listingsService.findById(id, commercials); } @UseGuards(OptionalJwtAuthGuard) @Get('user/:userid') findByUserId(@Request() req, @Param('userid') userid: string): any { console.log(req.user?.username); return this.listingsService.findByUserId(userid, commercials, req.user?.username); } @Post('search') async find(@Body() criteria: ListingCriteria): Promise { return await this.listingsService.findListingsByCriteria(criteria, commercials); } @Get('states/all') getStates(): any { return this.listingsService.getStates(commercials); } @Post() async create(@Body() listing: any) { this.logger.info(`Save Listing`); return await this.listingsService.createListing(listing, commercials); } @Put() async update(@Body() listing: any) { this.logger.info(`Save Listing`); return await this.listingsService.updateCommercialPropertyListing(listing.id, listing); } @Delete(':id/:imagePath') deleteById(@Param('id') id: string, @Param('imagePath') imagePath: string) { this.listingsService.deleteListing(id, commercials); this.fileService.deleteDirectoryIfExists(imagePath); } }