From fc27eeb6c64b97561560dbbfe5e54eb1bad13c36 Mon Sep 17 00:00:00 2001 From: orl0pl Date: Fri, 23 Jun 2023 18:51:42 +0200 Subject: [PATCH] i made it!!11! --- db/converter.ts | 35 ++++++++++++----------------- db/notes.json | 46 ++++++-------------------------------- routes/adminRoutes.ts | 4 ++-- routes/infoRoutes.ts | 31 ++++++++++++++++++++++++++ server.ts | 3 +++ views/addInfo.ejs | 51 +++++++++++++++++++++++++++++++++++++++++++ views/subject.ejs | 39 ++++++++++++++++++++++++++++++++- 7 files changed, 146 insertions(+), 63 deletions(-) create mode 100644 routes/infoRoutes.ts create mode 100644 views/addInfo.ejs diff --git a/db/converter.ts b/db/converter.ts index 1cf8f23..47b79d6 100644 --- a/db/converter.ts +++ b/db/converter.ts @@ -16,9 +16,10 @@ export interface Person { id: number; name: string; password: string; - roles: Array<'editor' | 'admin' | 'user'>; + roles: Array<'admin'|'editor'|'user'>; } + export interface Subject { id: number; name: string; @@ -27,17 +28,10 @@ export interface Subject { } export interface Info { - id: number; - content: string; - contentHistory: InfoContentHistory[]; - addedBy: number; - updateDate: number; -} - -export interface InfoContentHistory { + id: number; content: string; - updateDate: number; addedBy: number; + updateDate: number; } export interface Lesson { @@ -61,13 +55,13 @@ export interface Exercise { export interface Note { id: number; content: string; - contentHistory: NoteContentHistory[]; + contentHistory: ContentHistory[]; realDate: string; updateDate: number; addedBy: number; } -export interface NoteContentHistory { +export interface ContentHistory { content: string; updateDate: number; addedBy: number; @@ -246,7 +240,7 @@ const typeMap: any = { { json: "id", js: "id", typ: 0 }, { json: "name", js: "name", typ: "" }, { json: "password", js: "password", typ: "" }, - { json: "roles", js: "roles", typ: a("") }, + { json: "roles", js: "roles", typ: a(r("Role")) }, ], false), "Subject": o([ { json: "id", js: "id", typ: 0 }, @@ -257,15 +251,9 @@ const typeMap: any = { "Info": o([ { json: "id", js: "id", typ: 0 }, { json: "content", js: "content", typ: "" }, - { json: "contentHistory", js: "contentHistory", typ: a(r("InfoContentHistory")) }, { json: "addedBy", js: "addedBy", typ: 0 }, { json: "updateDate", js: "updateDate", typ: 0 }, ], false), - "InfoContentHistory": o([ - { json: "content", js: "content", typ: "" }, - { json: "updateDate", js: "updateDate", typ: 0 }, - { json: "addedBy", js: "addedBy", typ: 0 }, - ], false), "Lesson": o([ { json: "id", js: "id", typ: 0 }, { json: "topic", js: "topic", typ: "" }, @@ -285,14 +273,19 @@ const typeMap: any = { "Note": o([ { json: "id", js: "id", typ: 0 }, { json: "content", js: "content", typ: "" }, - { json: "contentHistory", js: "contentHistory", typ: a(r("NoteContentHistory")) }, + { json: "contentHistory", js: "contentHistory", typ: a(r("ContentHistory")) }, { json: "realDate", js: "realDate", typ: "" }, { json: "updateDate", js: "updateDate", typ: 3.14 }, { json: "addedBy", js: "addedBy", typ: 0 }, ], false), - "NoteContentHistory": o([ + "ContentHistory": o([ { json: "content", js: "content", typ: "" }, { json: "updateDate", js: "updateDate", typ: 3.14 }, { json: "addedBy", js: "addedBy", typ: 0 }, ], false), + "Role": [ + "admin", + "editor", + "user", + ], }; diff --git a/db/notes.json b/db/notes.json index 168542f..d46ac3a 100644 --- a/db/notes.json +++ b/db/notes.json @@ -45,7 +45,7 @@ ] }, { - "id": "5", + "id": 5, "name": "test", "password": "12345678", "roles": [ @@ -62,38 +62,20 @@ { "id": 0, "content": "Example Description", - "contentHistory": [ - { - "content": "Old Description", - "updateDate": 1685360780, - "addedBy": 1 - }, - { - "content": "Example Description", - "updateDate": 1685367780, - "addedBy": 0 - } - ], "addedBy": 0, "updateDate": 1685367780 }, { "id": 1, "content": "Another Description", - "contentHistory": [ - { - "content": "Another Old Description", - "updateDate": 1685207780, - "addedBy": 2 - }, - { - "content": "Another Description", - "updateDate": 1685267780, - "addedBy": 4 - } - ], "addedBy": 4, "updateDate": 12053973 + }, + { + "addedBy": 2, + "content": "Nie wiem co tu napisać", + "id": 2, + "updateDate": 1687539076 } ], "lessons": [ @@ -220,26 +202,12 @@ { "id": 2, "content": "Another Description", - "contentHistory": [ - { - "content": "Another Old Description", - "updateDate": 1685207780, - "addedBy": 2 - } - ], "addedBy": 2, "updateDate": 12053973 }, { "id": 3, "content": "Yet Another Description", - "contentHistory": [ - { - "content": "Yet Another Old Description", - "updateDate": 1685207780, - "addedBy": 2 - } - ], "addedBy": 2, "updateDate": 12053973 } diff --git a/routes/adminRoutes.ts b/routes/adminRoutes.ts index 8fcf095..4a54a7d 100644 --- a/routes/adminRoutes.ts +++ b/routes/adminRoutes.ts @@ -130,7 +130,7 @@ export function editPersonRolesRoute(req: Request<{ id: number }>, res: Response res.send("not admin"); } } -export function editPersonRolesPOSTRoute(req: Request<{ id: number },{},{name:string, password:string, user: 'on'|undefined, editor: 'on'|undefined, admin: 'on'|undefined}>, res: Response){ +export function editPersonRolesPOSTRoute(req: Request<{ id: string },{},{name:string, password:string, user: 'on'|undefined, editor: 'on'|undefined, admin: 'on'|undefined}>, res: Response){ var roles: Array<'user' | 'editor' | 'admin'> = [] if (req.body.user == 'on') roles.push('user'); if (req.body.editor == 'on') roles.push('editor'); @@ -139,7 +139,7 @@ export function editPersonRolesPOSTRoute(req: Request<{ id: number },{},{name:st if (req.account?.roles.includes("admin")) { console.log(req.body); const editedUser = { - id: req.params.id, + id: parseInt(req.params.id), name: req.body.name, password: req.body.password, roles: roles, diff --git a/routes/infoRoutes.ts b/routes/infoRoutes.ts new file mode 100644 index 0000000..082fd15 --- /dev/null +++ b/routes/infoRoutes.ts @@ -0,0 +1,31 @@ +import { Request, Response } from "express"; +import iconmapper from "../utils/iconmapper"; +import { data } from "../server"; +import saveChangesToNotes from "../utils/saveNotes"; +export function addInfoRoute(req: Request<{ id: number }>, res: Response){ + if(req.account?.roles.includes('editor')) { + res.render("addInfo", { + url: '../../../', + mi: iconmapper, + + }) + } + else { + res.send("not editor"); + } +} +export function addInfoPOSTRoute(req: Request<{ id: number }, {}, {content: string}>, res: Response){ + if(req.account?.roles.includes('editor')&&req.body.content) { + data.subjects[req.params.id].infos.push({ + addedBy: req.account.id, + content: req.body.content, + id: data.subjects[req.params.id].infos.length, + updateDate: Math.floor(Date.now() / 1000) + }) + res.send("added info"); + saveChangesToNotes() + } + else { + res.send("not editor"); + } +} \ No newline at end of file diff --git a/server.ts b/server.ts index ba8a7c4..75d410b 100644 --- a/server.ts +++ b/server.ts @@ -26,6 +26,7 @@ import historyRoute from './routes/historyRoute'; import { addLessonPOSTRoute, addLessonRoute } from './routes/addLessonRoutes'; import { deleteNoteRoute, deleteNotePOSTRoute, deleteExerciseRoute, deleteExercisePOSTRoute, deleteLessonRoute, deleteLessonPOSTRoute } from './routes/deleteRoutes'; import { addPersonPOSTRoute, addPersonRoute, addSubjectPOSTRoute, addSubjectRoute, adminPanelRoute, deleteSubjectPOSTRoute, deleteSubjectRoute, editPersonRolesPOSTRoute, editPersonRolesRoute, editSubjectPOSTRoute, editSubjectRoute } from './routes/adminRoutes'; +import { addInfoPOSTRoute, addInfoRoute } from './routes/infoRoutes'; var json = fs.readFileSync('db/notes.json', 'utf8'); export const data = Convert.toDataBase(json); export var dataRaw: DataBase = JSON.parse(fs.readFileSync('db/notes.json', 'utf8')); @@ -82,6 +83,8 @@ app.set('view engine', 'ejs') app.get('/editor', editorRoute) app.get('/', indexRoute); app.get('/s/:id', subjectRoute) +app.get('/s/:id/add-info', addInfoRoute); +app.post('/s/:id/add-info', addInfoPOSTRoute) app.get('/s/:id/add', addLessonRoute) app.post('/s/:id/add', addLessonPOSTRoute) app.get('/s/:id/l/:lessonid', lessonRoute) diff --git a/views/addInfo.ejs b/views/addInfo.ejs new file mode 100644 index 0000000..00b33ae --- /dev/null +++ b/views/addInfo.ejs @@ -0,0 +1,51 @@ + + + + + Zaloguj się + + + + + + + + + Edytor + + + + +
+ + <%= mi('book-open-blank-variant') %> + + Dodaj informację + + + +
+ + \ No newline at end of file diff --git a/views/subject.ejs b/views/subject.ejs index 09bff4a..27c65e0 100644 --- a/views/subject.ejs +++ b/views/subject.ejs @@ -71,13 +71,14 @@ <%= element.name %> -
+
<%= mi('information-outline') %> Informacje
+
<% element.infos.forEach(element=> { %>
@@ -107,6 +108,42 @@ <%= subject.name %> +
+
+ + <%= mi('information-outline') %> + + Informacje +
+ + + +
+ <% subject.infos.forEach(subject=> { %> +
+ + + <%= timeAgo.format(subject.updateDate * 1000) %> + + - + + <%= persons[subject.addedBy].name %> + + + + <%= subject.content %> + + + +
+ <% }) %> +
+