From 2493ecd1ea79d731f5857ebfac2195240a37738f Mon Sep 17 00:00:00 2001 From: florimondmanca Date: Wed, 31 Jan 2024 15:30:57 +0100 Subject: [PATCH] Redirect to month of event --- .../FairCalendar/Controller/AddEventController.ts | 3 ++- .../FairCalendar/Controller/DeleteEventController.ts | 11 ++++++++++- .../FairCalendar/Controller/EditEventController.ts | 8 +++++++- src/Infrastructure/FairCalendar/Routing/urls.ts | 12 ++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/Infrastructure/FairCalendar/Routing/urls.ts diff --git a/src/Infrastructure/FairCalendar/Controller/AddEventController.ts b/src/Infrastructure/FairCalendar/Controller/AddEventController.ts index 2b5d3918..7dac423f 100644 --- a/src/Infrastructure/FairCalendar/Controller/AddEventController.ts +++ b/src/Infrastructure/FairCalendar/Controller/AddEventController.ts @@ -25,6 +25,7 @@ import { GetProjectsQuery } from 'src/Application/Project/Query/GetProjectsQuery import { GetCooperativeQuery } from 'src/Application/Settings/Query/GetCooperativeQuery'; import { ArrayUtils } from 'src/Infrastructure/Common/Utils/ArrayUtils'; import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; +import { makeMonthUrl } from '../Routing/urls'; @Controller('app/faircalendar/events/add') @UseGuards(IsAuthenticatedGuard) @@ -99,7 +100,7 @@ export class AddEventController { ) ); - res.redirect(303, this.resolver.resolve('faircalendar_index')); + res.redirect(303, makeMonthUrl(this.resolver, new Date(startDate))); } catch (e) { throw new BadRequestException(e.message); } diff --git a/src/Infrastructure/FairCalendar/Controller/DeleteEventController.ts b/src/Infrastructure/FairCalendar/Controller/DeleteEventController.ts index 9534f42c..f9f584c8 100644 --- a/src/Infrastructure/FairCalendar/Controller/DeleteEventController.ts +++ b/src/Infrastructure/FairCalendar/Controller/DeleteEventController.ts @@ -9,6 +9,7 @@ import { } from '@nestjs/common'; import { Response } from 'express'; import { ICommandBus } from 'src/Application/ICommandBus'; +import { IQueryBus } from 'src/Application/IQueryBus'; import { IsAuthenticatedGuard } from 'src/Infrastructure/HumanResource/User/Security/IsAuthenticatedGuard'; import { LoggedUser } from 'src/Infrastructure/HumanResource/User/Decorator/LoggedUser'; import { User } from 'src/Domain/HumanResource/User/User.entity'; @@ -16,6 +17,9 @@ import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO'; import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName'; import { DeleteEventCommand } from 'src/Application/FairCalendar/Command/DeleteEventCommand'; import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; +import { EventView } from 'src/Application/FairCalendar/View/EventView'; +import { GetEventByIdQuery } from 'src/Application/FairCalendar/Query/GetEventByIdQuery'; +import { makeMonthUrl } from '../Routing/urls'; @Controller('app/faircalendar/events/delete') @UseGuards(IsAuthenticatedGuard) @@ -23,6 +27,8 @@ export class DeleteEventController { constructor( @Inject('ICommandBus') private readonly commandBus: ICommandBus, + @Inject('IQueryBus') + private readonly queryBus: IQueryBus, private readonly resolver: RouteNameResolver ) {} @@ -34,8 +40,11 @@ export class DeleteEventController { @Res() res: Response ) { try { + const event: EventView = await this.queryBus.execute( + new GetEventByIdQuery(dto.id) + ); await this.commandBus.execute(new DeleteEventCommand(dto.id, user)); - res.redirect(303, this.resolver.resolve('faircalendar_index')); + res.redirect(303, makeMonthUrl(this.resolver, new Date(event.date))); } catch (e) { throw new BadRequestException(e.message); } diff --git a/src/Infrastructure/FairCalendar/Controller/EditEventController.ts b/src/Infrastructure/FairCalendar/Controller/EditEventController.ts index 10862d99..7d267685 100644 --- a/src/Infrastructure/FairCalendar/Controller/EditEventController.ts +++ b/src/Infrastructure/FairCalendar/Controller/EditEventController.ts @@ -30,6 +30,8 @@ import { EditEventDTO } from '../DTO/EditEventDTO'; import { UpdateEventCommand } from 'src/Application/FairCalendar/Command/UpdateEventCommand'; import { GetEventByIdQuery } from 'src/Application/FairCalendar/Query/GetEventByIdQuery'; import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; +import { makeMonthUrl } from '../Routing/urls'; +import { EventView } from 'src/Application/FairCalendar/View/EventView'; @Controller('app/faircalendar/events/edit') @UseGuards(IsAuthenticatedGuard) @@ -100,7 +102,11 @@ export class EditEventController { ) ); - res.redirect(303, this.resolver.resolve('faircalendar_index')); + const event: EventView = await this.queryBus.execute( + new GetEventByIdQuery(idDto.id) + ); + + res.redirect(303, makeMonthUrl(this.resolver, new Date(event.date))); } catch (e) { throw new BadRequestException(e.message); } diff --git a/src/Infrastructure/FairCalendar/Routing/urls.ts b/src/Infrastructure/FairCalendar/Routing/urls.ts new file mode 100644 index 00000000..2886b2f3 --- /dev/null +++ b/src/Infrastructure/FairCalendar/Routing/urls.ts @@ -0,0 +1,12 @@ +import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver'; + +export function makeMonthUrl(resolver: RouteNameResolver, date: Date): string { + const url = resolver.resolve('faircalendar_index'); + + const params = new URLSearchParams({ + month: (date.getMonth() + 1).toString(), + year: date.getFullYear().toString() + }); + + return url + '?' + params.toString(); +}