Skip to content

Commit

Permalink
finished making admin routes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
orl0pl committed Jun 23, 2023
1 parent 69f9609 commit a989a50
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 15 deletions.
19 changes: 18 additions & 1 deletion db/notes.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,29 @@
"editor",
"user"
]
},
{
"id": 4,
"name": "user",
"password": "user",
"roles": [
"user"
]
},
{
"id": "5",
"name": "test",
"password": "12345678",
"roles": [
"user",
"editor"
]
}
],
"subjects": [
{
"id": 0,
"name": "Example Subject",
"name": "Matma",
"infos": [
{
"id": 0,
Expand Down
138 changes: 129 additions & 9 deletions routes/adminRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Request, Response } from "express";
import ReactDOMServer from 'react-dom/server';
import AdminPanel from "../views/adminPanel";
import { data } from "../server";
import iconmapper from "../utils/iconmapper";
import saveChangesToNotes from "../utils/saveNotes";
import { randomBytes } from "crypto";
export function adminPanelRoute(req: Request, res: Response) {
if (req.account?.roles.includes("admin")) {
const jsx = ReactDOMServer.renderToString(AdminPanel({
Expand All @@ -13,39 +16,156 @@ export function adminPanelRoute(req: Request, res: Response) {
res.send("not admin");
}
}
export function editSubjectRoute(req: Request, res: Response) {
if (req.account?.roles.includes("admin")) {
res.send("admin");
export function editSubjectRoute(req: Request<{ id: number }>, res: Response) {
if (req.account?.roles.includes("admin")&&data.subjects[req.params.id]) {
res.render("editSubject", {
url: '../../../',
mi: iconmapper,
oldName: data.subjects[req.params.id].name
})
}
else {
res.send("not admin");
}
}
export function deleteSubjectRoute(req: Request, res: Response) {
if (req.account?.roles.includes("admin")) {
res.send("admin");
export function editSubjectPOSTRoute(req: Request<{ id: number }, {}, { name: string }>, res: Response) {
if (req.account?.roles.includes("admin")&&data.subjects[req.params.id]&&req.body.name) {
data.subjects[req.params.id].name = req.body.name;
saveChangesToNotes();
res.send("zmieniono nazwę");
}
else {
res.send("not admin");
}
}
export function deleteSubjectRoute(req: Request<{ id: number }>, res: Response) {
if (req.account?.roles.includes("admin")&&data.subjects[req.params.id]) {
res.render('delete', {
url: '../../../',
deletionTypeName: 'przedmiot',
mi: iconmapper,
verificationCode: `${req.params.id}-${randomBytes(2).toString('hex')}`,
})
}
else {
res.send("not admin");
}
}
export function deleteSubjectPOSTRoute(req: Request<{ id: number }>, res: Response) {
if (req.account?.roles.includes("admin")&&data.subjects[req.params.id]) {
data.subjects.splice(req.params.id, 1);
saveChangesToNotes();
res.send("przedmiot usunięty");
}
else {
res.send("not admin");
}
}
export function addSubjectRoute(req: Request, res: Response) {
if (req.account?.roles.includes("admin")) {
res.send("admin");
res.render("addSubject", {
url: '../../',
mi: iconmapper
})
}
else {
res.send("not admin");
}
}
export function addSubjectPOSTRoute(req: Request<{},{},{ name: string }>, res: Response) {
if (req.account?.roles.includes("admin")&&req.body.name) {
data.subjects.push({
id: data.subjects.length,
name: req.body.name,
infos: [],
lessons: []
})
saveChangesToNotes();
res.send("added subject");
}
else {
res.send("not admin");
}
}
export function addPersonRoute(req: Request, res: Response) {
if (req.account?.roles.includes("admin")) {
res.send("admin");
res.render("addPerson", {
url: '../../',
mi: iconmapper,
})
}
else {
res.send("not admin");
}
}
export function addPersonPOSTRoute(req: Request<{},{},{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');
if (req.body.admin == 'on') roles.push('admin');

if (req.account?.roles.includes("admin")&&req.body.name&&req.body.password) {
console.log(req.body);
data.persons.push({
id: data.persons.length,
name: req.body.name,
password: req.body.password,
roles: roles,
})
saveChangesToNotes();
res.send("added person");
}
else {
res.send("not admin");
}
}
export function editPersonRolesRoute(req: Request<{ id: number }>, res: Response){
if (req.account?.roles.includes("admin")) {
res.render("editPerson", {
url: '../../../',
mi: iconmapper,
})
}
else {
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){
var roles: Array<'user' | 'editor' | 'admin'> = []
if (req.body.user == 'on') roles.push('user');
if (req.body.editor == 'on') roles.push('editor');
if (req.body.admin == 'on') roles.push('admin');

if (req.account?.roles.includes("admin")) {
console.log(req.body);
const editedUser = {
id: req.params.id,
name: req.body.name,
password: req.body.password,
roles: roles,
}
data.persons[req.params.id] = editedUser;
saveChangesToNotes();
res.send("edited person");
}
else {
res.send("not admin");
}
}
export function deletePersonRoute(req: Request<{ id: number }>, res: Response){
if (req.account?.roles.includes("admin")) {
res.render('delete', {
url: '../../../',
deletionTypeName: 'osobę',
mi: iconmapper,
verificationCode: `${req.params.id}-${randomBytes(2).toString('hex')}`,
})
}
else {
res.send("not admin");
}
}
export function editPersonRoles(req: Request, res: Response){
export function deletePersonPOSTRoute(req: Request, res: Response){
if (req.account?.roles.includes("admin")) {
res.send("admin");
}
Expand Down
9 changes: 7 additions & 2 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {searchPOSTRoute, searchRoute} from './routes/searchRoutes';
import historyRoute from './routes/historyRoute';
import { addLessonPOSTRoute, addLessonRoute } from './routes/addLessonRoutes';
import { deleteNoteRoute, deleteNotePOSTRoute, deleteExerciseRoute, deleteExercisePOSTRoute, deleteLessonRoute, deleteLessonPOSTRoute } from './routes/deleteRoutes';
import { addPersonRoute, addSubjectRoute, adminPanelRoute, deleteSubjectRoute, editPersonRoles, editSubjectRoute } from './routes/adminRoutes';
import { addPersonPOSTRoute, addPersonRoute, addSubjectPOSTRoute, addSubjectRoute, adminPanelRoute, deleteSubjectPOSTRoute, deleteSubjectRoute, editPersonRolesPOSTRoute, editPersonRolesRoute, editSubjectPOSTRoute, editSubjectRoute } from './routes/adminRoutes';
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 @@ -108,10 +108,15 @@ app.post('/search', searchPOSTRoute)

app.get('/adminpanel', adminPanelRoute)
app.get('/adminpanel/edit/:id', editSubjectRoute)
app.post('/adminpanel/edit/:id', editSubjectPOSTRoute)
app.get('/adminpanel/delete/:id', deleteSubjectRoute)
app.post('/adminpanel/delete/:id', deleteSubjectPOSTRoute)
app.get('/adminpanel/add', addSubjectRoute)
app.post('/adminpanel/add', addSubjectPOSTRoute)
app.get('/adminpanel/add-person', addPersonRoute)
app.get('/adminpanel/edit-roles/:id', editPersonRoles)
app.post('/adminpanel/add-person', addPersonPOSTRoute)
app.get('/adminpanel/edit-roles/:id', editPersonRolesRoute)
app.post('/adminpanel/edit-roles/:id', editPersonRolesPOSTRoute)

app.listen(1447, () => {
console.log(`⚡️[NOTAMARK]: Running at http://localhost:1447`);
Expand Down
2 changes: 1 addition & 1 deletion static/mainstyles.css
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ body {
border-radius: 8px;
}

input {
input[type="text"], input[type="password"] {
all: unset;
background-color: var(--md-sys-color-surface-container-highest);
padding: 12px 16px;
Expand Down
2 changes: 1 addition & 1 deletion static/style-g.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ body {
min-height: 100%;
}

input {
input:not([type="checkbox"]) {
all: unset;
background-color: var(--md-sys-color-surface-container-highest);
padding: 12px 16px;
Expand Down
2 changes: 1 addition & 1 deletion static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ body {
flex: 1;
}

textarea, input, span#realDateOrReference {
textarea, input:not([type="checkbox"]), span#realDateOrReference {
all: unset;
resize: none;
flex: 1;
Expand Down
55 changes: 55 additions & 0 deletions views/addPerson.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!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('account-plus') %>
</span>
<span headline-small>Dodaj osobę</span>
<input type="text" name="name" placeholder="Nazwa użytkownika">
<input type="password" name="password" placeholder="Hasło">
Role:
Użytkownik: <input type="checkbox" name="user" >
Edytor: <input type="checkbox" name="editor">
Administrator: <input type="checkbox" name="admin">
<button type="submit">Dodaj osobę</button>
</form>
</body>
</html>
50 changes: 50 additions & 0 deletions views/addSubject.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!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('plus-box-multiple') %>
</span>
<span headline-small>Dodaj przedmiot</span>
<input type="text" name="name" placeholder="Nazwa przedmiotu">
<button type="submit">Dodaj przedmiot</button>
</form>
</body>
</html>
5 changes: 5 additions & 0 deletions views/adminPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export default function AdminPanel({ data, url }: { data: DataBase, url: string
<Icon icon="pencil" />
</button>
</a>
{/* <a href={`delete-person/${person.id}`} style={{ "textDecoration": "none" }}>
<button id="delete" style={{ backgroundColor: "var(--md-sys-color-error-container)", color: "var(--md-sys-color-on-error-container)" }}>
<Icon icon="trash-can" />
</button>
</a> */}
</SubjectListItemGroup>
</SubjectListItem>

Expand Down
Loading

0 comments on commit a989a50

Please sign in to comment.