Fix for Issue #45

This commit is contained in:
2024-05-24 10:52:11 -05:00
parent f9d9c6ad9e
commit c9d94e973a
11 changed files with 30 additions and 74 deletions

View File

@@ -128,22 +128,6 @@ export class FileService {
let timeTaken = Date.now() - start;
this.logger.info(`Quality: ${quality} - Time: ${timeTaken} milliseconds`);
}
// getProfileImagesForUsers(userids: string) {
// const ids = userids.split(',');
// let result = {};
// for (const id of ids) {
// result = { ...result, [id]: fs.existsSync(`./pictures/profile/${id}.avif`) };
// }
// return result;
// }
// getCompanyLogosForUsers(userids: string) {
// const ids = userids.split(',');
// let result = {};
// for (const id of ids) {
// result = { ...result, [id]: fs.existsSync(`./pictures/logo/${id}.avif`) };
// }
// return result;
// }
deleteImage(path: string) {
fs.unlinkSync(path);
}

View File

@@ -1,4 +1,4 @@
import { Controller, Delete, Get, Inject, Param, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { Controller, Delete, Inject, Param, Post, UploadedFile, UseInterceptors } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
@@ -6,9 +6,6 @@ import { FileService } from '../file/file.service.js';
import { ListingsService } from '../listings/listings.service.js';
import { SelectOptionsService } from '../select-options/select-options.service.js';
import { commercials } from '../drizzle/schema.js';
import { CommercialPropertyListing } from '../models/db.model.js';
@Controller('image')
export class ImageController {
constructor(
@@ -24,20 +21,7 @@ export class ImageController {
@UseInterceptors(FileInterceptor('file'))
async uploadPropertyPicture(@UploadedFile() file: Express.Multer.File, @Param('imagePath') imagePath: string, @Param('serial') serial: string) {
const imagename = await this.fileService.storePropertyPicture(file, imagePath, serial);
await this.listingService.addImage(imagePath, imagename);
}
@Get(':email/:serial')
async getPropertyImagesById(@Param('email') adjustedEmail: string, @Param('serial') serial: string): Promise<any> {
const result = await this.listingService.findByImagePath(adjustedEmail);
const listing = result as CommercialPropertyListing;
if (listing.imageOrder) {
return listing.imageOrder;
} else {
const imageOrder = await this.fileService.getPropertyImages(adjustedEmail, serial);
listing.imageOrder = imageOrder;
this.listingService.updateListing(listing.id, listing, commercials);
return imageOrder;
}
await this.listingService.addImage(imagePath, serial, imagename);
}
@Delete('propertyPicture/:imagePath/:serial/:imagename')
async deletePropertyImagesById(@Param('imagePath') imagePath: string, @Param('serial') serial: string, @Param('imagename') imagename: string): Promise<any> {

View File

@@ -34,7 +34,7 @@ export class BusinessListingsController {
@Put()
update(@Body() listing: any) {
this.logger.info(`Save Listing`);
return this.listingsService.updateListing(listing.id, listing, businesses);
return this.listingsService.updateBusinessListing(listing.id, listing);
}
@Delete(':id')
deleteById(@Param('id') id: string) {

View File

@@ -38,16 +38,11 @@ export class CommercialPropertyListingsController {
@Put()
async update(@Body() listing: any) {
this.logger.info(`Save Listing`);
return await this.listingsService.updateListing(listing.id, listing, commercials);
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);
}
@Put('imageOrder/:id')
async changeImageOrder(@Param('id') id: string, @Body() imageOrder: string[]) {
this.listingsService.updateImageOrder(id, imageOrder);
}
}

View File

@@ -6,6 +6,7 @@ import { BusinessListing, CommercialPropertyListing } from 'src/models/db.model.
import { Logger } from 'winston';
import * as schema from '../drizzle/schema.js';
import { PG_CONNECTION, businesses, commercials } from '../drizzle/schema.js';
import { FileService } from '../file/file.service.js';
import { ListingCriteria } from '../models/main.model.js';
@Injectable()
@@ -13,6 +14,7 @@ export class ListingsService {
constructor(
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
@Inject(PG_CONNECTION) private conn: NodePgDatabase<typeof schema>,
private fileService: FileService,
) {}
private getConditions(criteria: ListingCriteria, table: typeof businesses | typeof commercials): any[] {
const conditions = [];
@@ -68,11 +70,11 @@ export class ListingsService {
.where(sql`${table.id} = ${id}`);
return result[0] as BusinessListing | CommercialPropertyListing;
}
async findByImagePath(imagePath: string): Promise<CommercialPropertyListing> {
async findByImagePath(imagePath: string, serial: string): Promise<CommercialPropertyListing> {
const result = await this.conn
.select()
.from(commercials)
.where(sql`${commercials.imagePath} = ${imagePath}`);
.where(and(sql`${commercials.imagePath} = ${imagePath}`, sql`${commercials.serialId} = ${serial}`));
return result[0] as CommercialPropertyListing;
}
async findByUserId(userId: string, table: typeof businesses | typeof commercials): Promise<BusinessListing[] | CommercialPropertyListing[]> {
@@ -86,13 +88,24 @@ export class ListingsService {
return createdListing as BusinessListing | CommercialPropertyListing;
}
async updateListing(id: string, data: BusinessListing | CommercialPropertyListing, table: typeof businesses | typeof commercials): Promise<BusinessListing | CommercialPropertyListing> {
async updateCommercialPropertyListing(id: string, data: CommercialPropertyListing): Promise<BusinessListing | CommercialPropertyListing> {
data.updated = new Date();
data.created = new Date(data.created);
const [updateListing] = await this.conn.update(table).set(data).where(eq(table.id, id)).returning();
const imageOrder = await this.fileService.getPropertyImages(data.imagePath, String(data.serialId));
let difference = imageOrder.filter(x => !data.imageOrder.includes(x)).concat(data.imageOrder.filter(x => !imageOrder.includes(x)));
if (difference.length > 0) {
this.logger.warn(`changes between image directory and imageOrder in listing ${data.serialId}: ${difference.join(',')}`);
data.imageOrder = imageOrder;
}
const [updateListing] = await this.conn.update(commercials).set(data).where(eq(commercials.id, id)).returning();
return updateListing as BusinessListing | CommercialPropertyListing;
}
async updateBusinessListing(id: string, data: BusinessListing): Promise<BusinessListing | CommercialPropertyListing> {
data.updated = new Date();
data.created = new Date(data.created);
const [updateListing] = await this.conn.update(businesses).set(data).where(eq(businesses.id, id)).returning();
return updateListing as BusinessListing | CommercialPropertyListing;
}
async deleteListing(id: string, table: typeof businesses | typeof commercials): Promise<void> {
await this.conn.delete(table).where(eq(table.id, id));
}
@@ -106,23 +119,17 @@ export class ListingsService {
// ##############################################################
// Images for commercial Properties
// ##############################################################
async updateImageOrder(id: string, imageOrder: string[]) {
const listing = (await this.findById(id, commercials)) as unknown as CommercialPropertyListing;
listing.imageOrder = imageOrder;
await this.updateListing(listing.id, listing, commercials);
}
async deleteImage(id: string, name: string) {
const listing = (await this.findById(id, commercials)) as unknown as CommercialPropertyListing;
const index = listing.imageOrder.findIndex(im => im === name);
if (index > -1) {
listing.imageOrder.splice(index, 1);
await this.updateListing(listing.id, listing, commercials);
await this.updateCommercialPropertyListing(listing.id, listing);
}
}
async addImage(imagePath: string, imagename: string) {
const listing = (await this.findByImagePath(imagePath)) as unknown as CommercialPropertyListing;
async addImage(imagePath: string, serial: string, imagename: string) {
const listing = (await this.findByImagePath(imagePath, serial)) as unknown as CommercialPropertyListing;
listing.imageOrder.push(imagename);
await this.updateListing(listing.id, listing, commercials);
await this.updateCommercialPropertyListing(listing.id, listing);
}
}