Skip to content

Commit

Permalink
Start building an area query
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Haarhoff committed May 26, 2024
1 parent efc1a64 commit e93cbc0
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/queries/area/construct-view-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {pipe} from 'fp-ts/lib/function';
import {User} from '../../types';
import {Dependencies} from '../../dependencies';
import * as TE from 'fp-ts/TaskEither';
import {readModels} from '../../read-models';
import {FailureWithStatus} from '../../types/failureWithStatus';
import {ViewModel} from './view-model';

export const constructViewModel =
(deps: Dependencies) =>
(user: User): TE.TaskEither<FailureWithStatus, ViewModel> =>
pipe(
deps.getAllEvents(),
TE.map(events => ({
user: user,
isSuperUser: readModels.superUsers.is(user.memberNumber)(events),
areaName: '3D Printers',
}))
);
27 changes: 27 additions & 0 deletions src/queries/area/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {Request, Response} from 'express';
import {pipe} from 'fp-ts/lib/function';
import * as TE from 'fp-ts/TaskEither';
import {Dependencies} from '../../dependencies';
import {getUserFromSession} from '../../authentication';
import {logInRoute} from '../../authentication/configure-auth-routes';
import {failureWithStatus} from '../../types/failureWithStatus';
import {StatusCodes} from 'http-status-codes';
import {constructViewModel} from './construct-view-model';
import {render} from './render';

export const area =
(deps: Dependencies) => async (req: Request, res: Response) => {
await pipe(
req.session,
getUserFromSession(deps),
TE.fromOption(() =>
failureWithStatus('You are not logged in.', StatusCodes.UNAUTHORIZED)()
),
TE.chain(constructViewModel(deps)),
TE.map(render),
TE.matchW(
() => res.redirect(logInRoute),
page => res.status(200).send(page)
)
)();
};
11 changes: 11 additions & 0 deletions src/queries/area/render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {pipe} from 'fp-ts/lib/function';
import {pageTemplate} from '../../templates';
import {html} from '../../types/html';
import * as O from 'fp-ts/Option';
import {ViewModel} from './view-model';

export const render = (viewModel: ViewModel) =>
pipe(
html` <h1>${viewModel.areaName}</h1> `,
pageTemplate(viewModel.areaName, O.some(viewModel.user))
);
6 changes: 6 additions & 0 deletions src/queries/area/view-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {User} from '../../types';

export type ViewModel = {
areaName: string;
user: User;
};
2 changes: 2 additions & 0 deletions src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {areas} from './areas';
import {landing} from './landing';
import {superUsers} from './super-users';
import asyncHandler from 'express-async-handler';
import {area} from './area';

export const queries = {
areas: flow(areas, asyncHandler),
area: flow(area, asyncHandler),
superUsers: flow(superUsers, asyncHandler),
landing: flow(landing, asyncHandler),
};
1 change: 1 addition & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const createRouter = (deps: Dependencies, conf: Config): Router => {
router.get('/', queries.landing(deps));

router.get('/areas', queries.areas(deps));
router.get('/areas/:area', queries.area(deps));
router.get('/areas/create', http.formGet(deps, commands.area.create));
router.post(
'/areas/create',
Expand Down

0 comments on commit e93cbc0

Please sign in to comment.