-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from COS301-SE-2023/api_refactor_dev
Api refactor dev
- Loading branch information
Showing
27 changed files
with
452 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
libs/api/event/feature/src/commands/update-event-floorlayout.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { UpdateEventFloorLayoutImgCommand, IUpdateFloorLayoutImage, IUpdateEventFloorLayoutImgResponse } from '@event-participation-trends/api/event/util'; | ||
import { CommandHandler, EventPublisher, ICommandHandler } from '@nestjs/cqrs'; | ||
import { Status } from'@event-participation-trends/api/user/util'; | ||
import { UpdateFloorLayoutImage } from '../models'; | ||
import { Types } from 'mongoose'; | ||
import { EventRepository } from '@event-participation-trends/api/event/data-access'; | ||
import { UserRepository } from '@event-participation-trends/api/user/data-access'; | ||
import { HttpException } from '@nestjs/common'; | ||
|
||
@CommandHandler(UpdateEventFloorLayoutImgCommand) | ||
export class UpdateEventFloorLayoutImgHandler implements ICommandHandler<UpdateEventFloorLayoutImgCommand, IUpdateEventFloorLayoutImgResponse> { | ||
constructor( | ||
private readonly publisher: EventPublisher, | ||
private readonly eventRepository: EventRepository, | ||
private readonly userRepository: UserRepository, | ||
) {} | ||
|
||
async execute(command: UpdateEventFloorLayoutImgCommand) { | ||
console.log(`${UpdateEventFloorLayoutImgHandler.name}`); | ||
|
||
const request = command.request; | ||
|
||
const eventIdObj = <Types.ObjectId> <unknown> request.eventId; | ||
const imageIdObj = <Types.ObjectId> <unknown> request.imageId; | ||
|
||
const eventDoc = await this.eventRepository.getEventById(eventIdObj); | ||
const userDoc = await this.userRepository.getUser(request.managerEmail || ""); | ||
const imageDoc = await this.eventRepository.getImageById(imageIdObj); | ||
|
||
if(eventDoc && userDoc && !eventDoc[0].Manager?.equals(userDoc[0]._id)) | ||
throw new HttpException(`Bad Request: Manager with Email ${request.managerEmail} does not manage Event with id ${request.eventId}`, 400); | ||
|
||
if(imageDoc && imageDoc[0].eventId && !eventDoc[0]._id?.equals(imageDoc[0].eventId)) | ||
throw new HttpException(`Bad Request: Manager with Email ${request.managerEmail} does not manage photo with id ${request.imageId}`, 400); | ||
|
||
const data: IUpdateFloorLayoutImage={ | ||
eventId: eventIdObj, | ||
imageId: imageIdObj, | ||
managerEmail: request.managerEmail, | ||
imgBase64: request.imgBase64, | ||
imageObj: request.imageObj, | ||
imageScale: request.imageScale, | ||
imageType: request.imageType, | ||
} | ||
|
||
const event = this.publisher.mergeObjectContext(UpdateFloorLayoutImage.fromData(data)); | ||
event.update(); | ||
event.commit(); | ||
|
||
return { status : Status.SUCCESS }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
libs/api/event/feature/src/events/update-event-floorlayout.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { IUpdateFloorLayoutImage, UpdateEventFloorLayoutImgEvent } from '@event-participation-trends/api/event/util'; | ||
import { EventRepository } from '@event-participation-trends/api/event/data-access'; | ||
import { IEventHandler, EventsHandler } from '@nestjs/cqrs'; | ||
import { EmailService } from '@event-participation-trends/api/email/feature' | ||
import { EmailContent, EmailSubject} from '@event-participation-trends/api/email/util'; | ||
import { UserRepository } from '@event-participation-trends/api/user/data-access'; | ||
|
||
@EventsHandler(UpdateEventFloorLayoutImgEvent) | ||
export class UpdateEventFloorLayoutImgEventHandler implements IEventHandler<UpdateEventFloorLayoutImgEvent> { | ||
constructor( | ||
private readonly eventRepository: EventRepository, | ||
private readonly userRepository: UserRepository, | ||
private readonly emailService: EmailService, | ||
) {} | ||
|
||
async handle(event: UpdateEventFloorLayoutImgEvent) { | ||
console.log(`${UpdateEventFloorLayoutImgEventHandler.name}`); | ||
|
||
const request = <IUpdateFloorLayoutImage> event.event; | ||
|
||
if(request.eventId && request.imageId){ | ||
let emailContent= "Updated Event Floorlayout Image: "+ EmailContent.NEW_LINE; | ||
const eventDoc = await this.eventRepository.getEventById(request.eventId); | ||
emailContent += "Event Name: " + eventDoc[0].Name + EmailContent.NEW_LINE; | ||
|
||
if(request.imgBase64){ | ||
await this.eventRepository.updateEventFloorlayoutImageimgBase64(request.imageId, request.imgBase64); | ||
emailContent+= "\t imgBase64 has been updated" + EmailContent.NEW_LINE; | ||
} | ||
|
||
if(request.imageObj){ | ||
await this.eventRepository.updateEventFloorlayoutImageimageObj(request.imageId, request.imageObj); | ||
emailContent+= "\t New imageObj: "+ request.imageObj + EmailContent.NEW_LINE; | ||
} | ||
|
||
if(request.imageScale){ | ||
await this.eventRepository.updateEventFloorlayoutImageimageScale(request.imageId, request.imageScale); | ||
emailContent+= "\t New imageScale: "+ request.imageScale.toString() + EmailContent.NEW_LINE; | ||
} | ||
|
||
if(request.imageType){ | ||
await this.eventRepository.updateEventFloorlayoutImageimageType(request.imageId,request.imageType); | ||
emailContent+= "\t New imageType: "+ request.imageType + EmailContent.NEW_LINE; | ||
} | ||
|
||
let userDoc; | ||
if(eventDoc[0].Manager != null && eventDoc[0].Manager != undefined) | ||
userDoc = await this.userRepository.getUserById(eventDoc[0].Manager); | ||
|
||
if(userDoc != undefined && userDoc != null && userDoc[0]) | ||
this.emailService.sendEmail( | ||
userDoc[0]?.Email || "", | ||
EmailSubject.EVENT_FLOORLAYOUT_IMAGE_UPDATED, | ||
emailContent | ||
); | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
libs/api/event/feature/src/models/update-image-floorlayout.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
IUpdateFloorLayoutImage, | ||
UpdateEventFloorLayoutImgEvent, | ||
} from '@event-participation-trends/api/event/util'; | ||
import { Types } from 'mongoose'; | ||
import { AggregateRoot } from '@nestjs/cqrs'; | ||
|
||
export class UpdateFloorLayoutImage extends AggregateRoot implements IUpdateFloorLayoutImage { | ||
constructor( | ||
public eventId: Types.ObjectId | undefined | null, | ||
public imageId: Types.ObjectId | undefined | null, | ||
public managerEmail: string | undefined | null, | ||
public imgBase64: string | undefined | null, | ||
public imageObj: string | undefined | null, | ||
public imageScale: number | undefined | null, | ||
public imageType: string | undefined | null, | ||
){ | ||
super(); | ||
} | ||
|
||
update(){ | ||
this.apply(new UpdateEventFloorLayoutImgEvent(this.toJSON())); | ||
} | ||
|
||
static fromData(event: IUpdateFloorLayoutImage): UpdateFloorLayoutImage { | ||
const instance = new UpdateFloorLayoutImage( | ||
event.eventId, | ||
event.imageId, | ||
event.managerEmail, | ||
event.imgBase64, | ||
event.imageObj, | ||
event.imageScale, | ||
event.imageType, | ||
); | ||
return instance; | ||
} | ||
|
||
toJSON(): IUpdateFloorLayoutImage { | ||
return { | ||
eventId: this.eventId, | ||
imageId: this.imageId, | ||
managerEmail: this.managerEmail, | ||
imgBase64: this.imgBase64, | ||
imageObj: this.imageObj, | ||
imageScale: this.imageScale, | ||
imageType: this.imageType, | ||
}; | ||
} | ||
} |
Oops, something went wrong.