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 26, 2024
1 parent ed5a16f commit 577ff69
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { GetTasksQuery } from 'src/Application/Task/Query/GetTasksQuery';
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 { MonthUrlBuilder } from '../Util/MonthUrlBuilder';

@Controller('app/faircalendar/events/add')
@UseGuards(IsAuthenticatedGuard)
Expand All @@ -34,7 +34,7 @@ export class AddEventController {
private readonly commandBus: ICommandBus,
@Inject('IQueryBus')
private readonly queryBus: IQueryBus,
private readonly resolver: RouteNameResolver
private readonly monthUrlBuilder: MonthUrlBuilder
) {}

@Get(':startDate--:endDate')
Expand Down Expand Up @@ -99,7 +99,7 @@ export class AddEventController {
)
);

res.redirect(303, this.resolver.resolve('faircalendar_index'));
res.redirect(303, this.monthUrlBuilder.build(new Date(startDate)));
} catch (e) {
throw new BadRequestException(e.message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ 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 { GetEventByIdQuery } from 'src/Application/FairCalendar/Query/GetEventByIdQuery';
import { EventView } from 'src/Application/FairCalendar/View/EventView';
import { IQueryBus } from '@nestjs/cqrs';
import { MonthUrlBuilder } from '../Util/MonthUrlBuilder';

@Controller('app/faircalendar/events/delete')
@UseGuards(IsAuthenticatedGuard)
export class DeleteEventController {
constructor(
@Inject('ICommandBus')
private readonly commandBus: ICommandBus,
private readonly resolver: RouteNameResolver
@Inject('IQueryBus')
private readonly queryBus: IQueryBus,
private readonly monthUrlBuilder: MonthUrlBuilder
) {}

@Post(':id')
Expand All @@ -33,9 +38,14 @@ export class DeleteEventController {
@LoggedUser() user: User,
@Res() res: Response
) {
const event: EventView = await this.queryBus.execute(
new GetEventByIdQuery(dto.id)
);
const date = new Date(event.date);

try {
await this.commandBus.execute(new DeleteEventCommand(dto.id, user));
res.redirect(303, this.resolver.resolve('faircalendar_index'));
res.redirect(303, this.monthUrlBuilder.build(date));
} catch (e) {
throw new BadRequestException(e.message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
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 { EventView } from 'src/Application/FairCalendar/View/EventView';
import { MonthUrlBuilder } from '../Util/MonthUrlBuilder';

@Controller('app/faircalendar/events/edit')
@UseGuards(IsAuthenticatedGuard)
Expand All @@ -39,7 +40,7 @@ export class EditEventController {
private readonly commandBus: ICommandBus,
@Inject('IQueryBus')
private readonly queryBus: IQueryBus,
private readonly resolver: RouteNameResolver
private readonly monthUrlBuilder: MonthUrlBuilder
) {}

@Get(':id')
Expand Down Expand Up @@ -100,7 +101,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, this.monthUrlBuilder.build(new Date(event.date)));
} catch (e) {
throw new BadRequestException(e.message);
}
Expand Down
18 changes: 18 additions & 0 deletions src/Infrastructure/FairCalendar/Util/MonthUrlBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable } from '@nestjs/common';
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';

@Injectable()
export class MonthUrlBuilder {
constructor(private readonly resolver: RouteNameResolver) {}

public build(date: Date): string {
const url = this.resolver.resolve('faircalendar_index');

const querystring = new URLSearchParams({
month: (date.getMonth() + 1).toString(),
year: date.getFullYear().toString()
}).toString();

return url + `?${querystring}`;
}
}
4 changes: 3 additions & 1 deletion src/Infrastructure/FairCalendar/faircalendar.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { FairCalendarOverviewTableFactory } from './Table/FairCalendarOverviewTa
import { TranslationsModule } from '../Translations/translations.module';
import { TablesModule } from '../Tables/tables.module';
import { ExtendedRoutingModule } from '../Common/ExtendedRouting/extendedRouting.module';
import { MonthUrlBuilder } from './Util/MonthUrlBuilder';

@Module({
imports: [
Expand Down Expand Up @@ -63,7 +64,8 @@ import { ExtendedRoutingModule } from '../Common/ExtendedRouting/extendedRouting
GetEventByIdQueryHandler,
DoesEventBelongToUser,
FairCalendarOverviewFactory,
FairCalendarOverviewTableFactory
FairCalendarOverviewTableFactory,
MonthUrlBuilder
]
})
export class FairCalendarModule {}

0 comments on commit 577ff69

Please sign in to comment.