55 lines
2.2 KiB
TypeScript
55 lines
2.2 KiB
TypeScript
import { Body, Controller, Delete, Get, Inject, Param, Post, Put, Request, UseGuards } from '@nestjs/common';
|
|
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
|
|
import { CommercialPropertyListing } from 'src/models/db.model.js';
|
|
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 { JwtUser, 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,
|
|
) {}
|
|
|
|
@UseGuards(OptionalJwtAuthGuard)
|
|
@Get(':id')
|
|
findById(@Request() req, @Param('id') id: string): any {
|
|
return this.listingsService.findCommercialPropertiesById(id, req.user as JwtUser);
|
|
}
|
|
|
|
@UseGuards(OptionalJwtAuthGuard)
|
|
@Get('user/:email')
|
|
findByEmail(@Request() req, @Param('email') email: string): Promise<CommercialPropertyListing[]> {
|
|
return this.listingsService.findCommercialPropertiesByEmail(email, req.user as JwtUser);
|
|
}
|
|
@UseGuards(OptionalJwtAuthGuard)
|
|
@Post('search')
|
|
async find(@Request() req, @Body() criteria: ListingCriteria): Promise<any> {
|
|
return await this.listingsService.findCommercialPropertyListings(criteria, req.user as JwtUser);
|
|
}
|
|
@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);
|
|
}
|
|
}
|