Skip to content

Commit

Permalink
i made it!!11!
Browse files Browse the repository at this point in the history
  • Loading branch information
orl0pl committed Jun 23, 2023
1 parent a989a50 commit fc27eeb
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 63 deletions.
35 changes: 14 additions & 21 deletions db/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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 },
Expand All @@ -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: "" },
Expand All @@ -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",
],
};
46 changes: 7 additions & 39 deletions db/notes.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
]
},
{
"id": "5",
"id": 5,
"name": "test",
"password": "12345678",
"roles": [
Expand All @@ -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": [
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions routes/adminRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions routes/infoRoutes.ts
Original file line number Diff line number Diff line change
@@ -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");
}
}
3 changes: 3 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions views/addInfo.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Zaloguj się</title>
<link rel="stylesheet" href="<%= url %>static/katex/katex.min.css">
<script defer src="<%= url %>static/katex/katex.min.js"></script>
<script src="<%= url %>static/marked/marked.min.js"></script>
<script defer src="<%= url %>static/marked/lib/marked.umd.js"></script>
<script defer src="<%= url %>static/katex/contrib/auto-render.min.js"
onload="renderMathInElement(document.body);"></script>
<link rel="stylesheet" href="<%= url %>static/style-g.css">
<link rel="stylesheet" href="<%= url %>static/style.css">
<link rel="icon" type="image/x-icon" href="<%= url %>static/favicon.ico">
<title>Edytor</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
body > form {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
flex: 0;
background-color: var(--md-sys-color-surface-container);
padding: 32px;
border-radius: 16px;
gap: 16px
}
body > form button,input {
width: 100%;
}
</style>
</head>
<body>
<form method="post">
<span class="MDI" headline-small>
<%= mi('book-open-blank-variant') %>
</span>
<span headline-small>Dodaj informację</span>
<textarea style="min-width: 20vw; min-height: 20vh" name="content" ></textarea>

<button type="submit">Dodaj informacje</button>
</form>
</body>
</html>
39 changes: 38 additions & 1 deletion views/subject.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@
<a href="/s/<%= element.id %>" class="headline-small">
<%= element.name %>
</a>
<div class="infos-wrapper">
<div class="infos-wrapper" style="padding: 4px">
<div class="title" label-large>
<span class="MDI">
<%= mi('information-outline') %>
</span>
<span>Informacje</span>
</div>
<div class="infos">
<% element.infos.forEach(element=> { %>
<div class="info">
Expand Down Expand Up @@ -107,6 +108,42 @@
<span headline-large class="name">
<%= subject.name %>
</span>
<div class="infos-wrapper">
<div class="title" label-large>
<span class="MDI">
<%= mi('information-outline') %>
</span>
<span>Informacje</span>
</div>
<a href="add-info/" body-large>
<button class="outline">
<span class="MDI">
<%= mi('plus') %>
</span>
Dodaj informacje
</button>
</a>
<div class="infos">
<% subject.infos.forEach(subject=> { %>
<div class="info">
<span label-medium>
<span class="updateDate">
<%= timeAgo.format(subject.updateDate * 1000) %>
</span>
-
<span class="madeBy">
<%= persons[subject.addedBy].name %>
</span>
</span>
<span class="content">
<%= subject.content %>
</span>
</div>
<% }) %>
</div>
</div>
<div class="button-wrapper">
<span class="title" label-large>
<span class="MDI" body-large>
Expand Down

0 comments on commit fc27eeb

Please sign in to comment.