Skip to content

Commit 17f757f

Browse files
committed
Add create course form
1 parent c4d63ed commit 17f757f

File tree

8 files changed

+209
-6
lines changed

8 files changed

+209
-6
lines changed

package-lock.json

Lines changed: 90 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
"body-parser": "^1.19.0",
3939
"bson": "^4.0.4",
4040
"compression": "^1.7.4",
41+
"connect-flash": "^0.1.1",
4142
"convict": "^6.0.0",
43+
"cookie-parser": "^1.4.5",
44+
"cookie-session": "^1.4.0",
4245
"copy": "^0.3.2",
4346
"errorhandler": "^1.5.1",
4447
"express": "^4.17.1",
@@ -56,11 +59,14 @@
5659
"winston": "^3.2.1"
5760
},
5861
"devDependencies": {
59-
"@types/nunjucks": "^3.1.3",
6062
"@types/aws-lambda": "^8.10.51",
63+
"@types/connect-flash": "0.0.35",
64+
"@types/cookie-parser": "^1.4.2",
65+
"@types/cookie-session": "^2.0.41",
6166
"@types/cucumber": "^6.0.1",
6267
"@types/faker": "^4.1.12",
6368
"@types/jest": "^25.2.3",
69+
"@types/nunjucks": "^3.1.3",
6470
"@types/supertest": "^2.0.9",
6571
"cucumber": "^6.0.5",
6672
"faker": "^4.1.0",

src/apps/backoffice/frontend/app.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,27 @@ import helmet from 'helmet';
44
import compress from 'compression';
55
import { registerRoutes } from './routes';
66
import path from 'path';
7+
import cookieSession from 'cookie-session';
8+
import cookieParser from 'cookie-parser';
9+
import flash from 'connect-flash';
710
import nunjucks from 'nunjucks';
811

912
const app: express.Express = express();
1013

1114
app.set('port', process.env.PORT || 8032);
1215

16+
app.use(cookieParser());
17+
app.use(
18+
cookieSession({
19+
name: 'Backoffice Frontend Codely session',
20+
keys: ['Codely'],
21+
22+
// Cookie Options
23+
maxAge: 24 * 60 * 60 * 1000 // 24 hours
24+
})
25+
);
26+
app.use(flash());
27+
1328
// Templates
1429
app.set('view engine', 'html');
1530
nunjucks.configure(path.join(__dirname, '/templates'), {

src/apps/backoffice/frontend/config/dependency-injection/application.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ services:
2020
method: 'createClient'
2121
arguments: ['mooc']
2222

23+
Mooc.courses.CourseRepository:
24+
class: ../../../../../Contexts/Mooc/Courses/infrastructure/persistence/MongoCourseRepository
25+
arguments: ['@Mooc.shared.ConnectionManager']
26+
27+
Mooc.shared.EventBus:
28+
class: ../../../../../Contexts/Shared/infrastructure/EventBus/InMemoryAsyncEventBus
29+
arguments: []
30+
31+
Mooc.courses.CourseCreator:
32+
class: ../../../../../Contexts/Mooc/Courses/application/CourseCreator
33+
arguments: ['@Mooc.courses.CourseRepository', '@Mooc.shared.EventBus']
34+
2335
Apps.Backoffice.Frontend.controllers.CoursesGetController:
2436
class: ../../controllers/CoursesGetController
2537
arguments: ['@Mooc.coursesCounter.CoursesCounterFinder']
38+
39+
Apps.Backoffice.Frontend.controllers.CoursesPostController:
40+
class: ../../controllers/CoursesPostController
41+
arguments: ['@Mooc.courses.CourseCreator']

src/apps/backoffice/frontend/controllers/CoursesGetController.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Request, Response } from 'express';
22
import { CoursesCounterFinder } from '../../../../Contexts/Mooc/CoursesCounter/application/Find/CoursesCounterFinder';
3+
import { Uuid } from '../../../../Contexts/Shared/domain/value-object/Uuid';
34

45
export class CoursesGetController {
56
constructor(private coursesCounterFinder: CoursesCounterFinder) {}
@@ -11,7 +12,8 @@ export class CoursesGetController {
1112
title: 'Welcome',
1213
description: 'CodelyTV - Backoffice',
1314
courses_counter: courses.total,
14-
new_course_id: 'xxxx'
15+
new_course_id: Uuid.random(),
16+
flash: req.flash()
1517
});
1618
}
1719
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Request, Response } from 'express';
2+
import { CreateCourseRequest } from '../../../../Contexts/Mooc/Courses/application/CreateCourseRequest';
3+
import { CourseCreator } from '../../../../Contexts/Mooc/Courses/application/CourseCreator';
4+
5+
export class CoursesPostController {
6+
constructor(private courseCreator: CourseCreator) {}
7+
8+
async run(req: Request, res: Response) {
9+
// TODO: validation
10+
// req.flash('errors.id', 'Flash Message Added');
11+
12+
await this.createCourse(req, res);
13+
}
14+
15+
private async createCourse(req: Request, res: Response) {
16+
await this.courseCreator.run({ id: req.body.id, name: req.body.name, duration: req.body.duration });
17+
req.flash('message', `Felicidades, el curso ${req.body.name} ha sido creado!`);
18+
res.redirect('/courses');
19+
}
20+
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import { Express } from 'express';
22
import container from '../config/dependency-injection';
33
import { CoursesGetController } from '../controllers/CoursesGetController';
4+
import { CoursesPostController } from '../controllers/CoursesPostController';
45

56
export const register = (app: Express) => {
6-
const controller: CoursesGetController = container.get('Apps.Backoffice.Frontend.controllers.CoursesGetController');
7-
app.get('/courses', controller.run.bind(controller));
7+
const coursesGetController: CoursesGetController = container.get(
8+
'Apps.Backoffice.Frontend.controllers.CoursesGetController'
9+
);
10+
const coursesPostController: CoursesPostController = container.get(
11+
'Apps.Backoffice.Frontend.controllers.CoursesPostController'
12+
);
13+
14+
app.get('/courses', coursesGetController.run.bind(coursesGetController));
15+
app.post('/courses', coursesPostController.run.bind(coursesPostController));
816
};
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1-
hello
1+
<form class="w-full max-w-lg float-right" method="post" action="/courses">
2+
<h2 class="block uppercase tracking-wide text-gray-700 text-3xl font-bold mb-2">Crear curso</h2>
3+
<div class="flex flex-wrap mb-6 -mx-3">
4+
<div class="w-full md:w-full px-3 mb-6 md:mb-0">
5+
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-first-name">
6+
Identificador
7+
</label>
8+
<input class="{% if flash['errors.id'] %}border border-red-500{% endif %} appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
9+
id="grid-first-name" type="text" name="id" placeholder="En formado UUID"
10+
value="{{new_course_id}}">
11+
12+
{% if flash['errors.id'] %}
13+
<p class="text-red-500 text-xs italic">{{ flash['errors.id'] }}</p>
14+
{% endif %}
15+
</div>
16+
</div>
17+
<div class="flex flex-wrap -mx-3 mb-6">
18+
<div class="w-full md:w-1/2 px-3 mb-6 md:mb-0">
19+
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-first-name">
20+
Nombre
21+
</label>
22+
<input class="{% if flash['errors.name'] %}border border-red-500{% endif %} appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
23+
id="grid-first-name" type="text" name="name" placeholder="DDD en PHP"
24+
value="">
25+
26+
{% if flash['errors.name'] %}
27+
<p class="text-red-500 text-xs italic">{{ flash['errors.name'] }}</p>
28+
{% endif %}
29+
</div>
30+
<div class="w-full md:w-1/2 px-3">
31+
<label class="block uppercase tracking-wide text-gray-700 text-xs font-bold mb-2" for="grid-last-name">
32+
Duración (en inglés)
33+
</label>
34+
<input class="{% if flash['errors.duration'] %}border border-red-500{% endif %} appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500"
35+
id="grid-last-name" type="text" name="duration" placeholder="8 days"
36+
value="xxx">
37+
{% if flash['errors.duration'] %}
38+
<p class="text-red-500 text-xs italic">{{ flash['errors.duration'] }}</p>
39+
{% endif %}
40+
</div>
41+
</div>
42+
<div class="flex flex-wrap mb-6">
43+
<button class="md:w-full bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
44+
type="submit">
45+
Crear curso!
46+
</button>
47+
</div>
48+
</form>

0 commit comments

Comments
 (0)