Skip to content

Commit

Permalink
Redirect to month of event
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Jan 31, 2024
1 parent ed5a16f commit ea50c88
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ 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';
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)
export class DeleteEventController {
constructor(
@Inject('ICommandBus')
private readonly commandBus: ICommandBus,
@Inject('IQueryBus')
private readonly queryBus: IQueryBus,
private readonly resolver: RouteNameResolver
) {}

Expand All @@ -34,8 +40,9 @@ 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -40,7 +42,7 @@ export class EditEventController {
@Inject('IQueryBus')
private readonly queryBus: IQueryBus,
private readonly resolver: RouteNameResolver
) {}
) { }

@Get(':id')
@WithName('faircalendar_events_edit')
Expand Down Expand Up @@ -100,7 +102,9 @@ 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);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Infrastructure/FairCalendar/Routing/urls.ts
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit ea50c88

Please sign in to comment.