From e1baf6b13f9d0242f766b723b26a2973fea8fbb6 Mon Sep 17 00:00:00 2001 From: "alec.schmidt" <alec.schmidt@etu.hesge.ch> Date: Wed, 7 Jun 2023 16:21:06 +0200 Subject: [PATCH] basic question service --- API/db/app.db | Bin 36864 -> 36864 bytes API/src/database/Database.ts | 6 ++-- API/src/routes/BaseRoutes.ts | 29 +++++++++--------- frontend/src/app/Types/types.ts | 1 + frontend/src/app/admin/admin.component.html | 2 +- frontend/src/app/admin/admin.component.ts | 8 ++++- .../app/services/questions.service.spec.ts | 16 ++++++++++ .../src/app/services/questions.service.ts | 26 ++++++++++++++++ 8 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 frontend/src/app/services/questions.service.spec.ts create mode 100644 frontend/src/app/services/questions.service.ts diff --git a/API/db/app.db b/API/db/app.db index e85f4c448850ec4990b567bd869ec13c5a97fb75..b9f76f706498f88f8a0278596752a3792021fdff 100644 GIT binary patch delta 185 zcmZozz|^pSX@WGP$V3@uMv;vP3;Fr^-!L%oZ)4!!#(#x>KmYR0f(o<vC$Ey{iIQPt zWssK_FU?CyP0P$nO@T8KQ*tx&#F$wbq(vpmGV_vC^GXtvl2ZzD5-U@S7#J8tL`B6D zb5fJR7^Ieq{|5sT|9uAj`}}V}aI>JnDSjoMr2M=r9%gw?xWU}a(u`08nHd-uI5z*$ MXBJ@OTI3J_0L~CNzW@LL delta 185 zcmZozz|^pSX@WGPz(g5mMuCk93;FpO7#NuNuQ2ex;lIMapMUvgL4{fTlUK>}M5Pv& z6yzjUrWP?UFo=qZN|vM+mw+%tP>h+CL0VL@EHf`THLoNwDLDlqF2u;nASx=Jn3I|e z#wod(d0b#S-Z1e0;C};xn*|L{@hkBp<>zJPmlm-xFfed}4Cdr!mS%(+$jr_x%DMTE MKC=KL=OTvy08{HW@c;k- diff --git a/API/src/database/Database.ts b/API/src/database/Database.ts index b903197..1740240 100644 --- a/API/src/database/Database.ts +++ b/API/src/database/Database.ts @@ -94,7 +94,7 @@ class DBHandler { const request = "UPDATE users \ SET username = '"+ a.username +"', password = '"+ a.password +"' \ - WHERE id = " + a.id.toString(); + WHERE id = " + req.params.id; asyncdb.all(request) .then(() => res.status(StatusCodes.OK).end()) @@ -153,7 +153,7 @@ class DBHandler { const a = req.body as Question_t; const request = "UPDATE questions \ SET question='"+ a.question +"', category='"+ a.category +"' \ - WHERE id=" + a.id.toString(); + WHERE id=" + req.params.id; asyncdb.all(request) @@ -164,7 +164,7 @@ class DBHandler { async deleteQuestion(req:express.Request, res:express.Response) { const a = req.body as Question_t; - asyncdb.all("DELETE FROM questions WHERE id =" + a.id) + asyncdb.all("DELETE FROM questions WHERE id =" + req.params.id) .then(() => res.status(StatusCodes.OK).end()) .catch(e => console.log(e)); } diff --git a/API/src/routes/BaseRoutes.ts b/API/src/routes/BaseRoutes.ts index 41a9dc8..5c9db80 100644 --- a/API/src/routes/BaseRoutes.ts +++ b/API/src/routes/BaseRoutes.ts @@ -26,7 +26,7 @@ function isAdmin(token: string): Boolean { if (token == null) return false; jwt.verify(token, process.env.TOKEN_SECRET, (err:any, user:User_t) => { - console.log(err); + // console.log(err); if (err) return false; adminToken = user.type === "admin" @@ -41,7 +41,7 @@ function tokenDecode(req: express.Request): User_t { const token = req.headers['authorization'] && req.headers['authorization'].split(' ')[1] jwt.verify(token, process.env.TOKEN_SECRET, (err:any, user:User_t) => { - console.log(err); + // console.log(err); if (err) user_data = undefined; else @@ -85,17 +85,19 @@ router.get(ROUTE+'/user', (req: express.Request, res: express.Response) => { DBHandler.getUsers(res); }); -router.patch(ROUTE+'/user', (req: express.Request, res: express.Response) => { +router.patch(ROUTE+'/user/:id', (req: express.Request, res: express.Response) => { const a = req.body as User_t; - if (a.username === undefined || - a.password === undefined || - a.type === undefined) { + // if (a.username === undefined || + // a.password === undefined || + // a.type === undefined) { - res.statusMessage = "invalid JSON"; - res.status(StatusCodes.BAD_REQUEST).end(); - return; - } + // res.statusMessage = "invalid JSON"; + // res.status(StatusCodes.BAD_REQUEST).end(); + // return; + // } + + console.log(a); if (a.type === "admin") if (!isAdmin(req.headers['authorization'] && req.headers['authorization'].split(' ')[1])) @@ -109,11 +111,10 @@ router.patch(ROUTE+'/user', (req: express.Request, res: express.Response) => { if(exists) return res.status(StatusCodes.CONFLICT).end(); - DBHandler.updateUser(req, res); }) - }); +}); router.delete(ROUTE+"/user/:id", (req: express.Request, res: express.Response) => { DBHandler.deleteUser(req, res); @@ -139,11 +140,11 @@ router.get(ROUTE+'/question', (req: express.Request, res: express.Response) => { DBHandler.getQuestions(req, res); }) -router.patch(ROUTE+'/question', (req: express.Request, res: express.Response) => { +router.patch(ROUTE+'/question/:id', (req: express.Request, res: express.Response) => { DBHandler.updateQuestion(req, res); }) -router.delete(ROUTE+'/question', (req: express.Request, res: express.Response) => { +router.delete(ROUTE+'/question/:id', (req: express.Request, res: express.Response) => { DBHandler.deleteQuestion(req, res); }) diff --git a/frontend/src/app/Types/types.ts b/frontend/src/app/Types/types.ts index 405dfe2..a6f41f8 100644 --- a/frontend/src/app/Types/types.ts +++ b/frontend/src/app/Types/types.ts @@ -11,6 +11,7 @@ export type QandA = { }; export type Question = { + id: number; question: string; category: string; } diff --git a/frontend/src/app/admin/admin.component.html b/frontend/src/app/admin/admin.component.html index ad5e3e3..e1119cd 100644 --- a/frontend/src/app/admin/admin.component.html +++ b/frontend/src/app/admin/admin.component.html @@ -23,7 +23,7 @@ <td>{{question.question}}</td> <td>{{question.category}}</td> <td>UPDATE</td> - <td>DELETE</td> + <td><button (click)="deleteQuestion(question)">DELETE</button></td> </tr> </tbody> </table> \ No newline at end of file diff --git a/frontend/src/app/admin/admin.component.ts b/frontend/src/app/admin/admin.component.ts index e899c1c..d1c2fc1 100644 --- a/frontend/src/app/admin/admin.component.ts +++ b/frontend/src/app/admin/admin.component.ts @@ -2,6 +2,7 @@ import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Answer, QandA, Question, User } from '../Types/types'; import { UsersService } from '../services/users.service'; +import { QuestionsService } from '../services/questions.service'; @Component({ selector: 'app-admin', @@ -16,7 +17,8 @@ export class AdminComponent implements OnInit { constructor( private http: HttpClient, - private userController: UsersService) { } + private userController: UsersService, + private questionController: QuestionsService) { } ngOnInit(): void { @@ -36,4 +38,8 @@ export class AdminComponent implements OnInit { deleteUser(user:User) { this.userController.deleteUser(user); } + + deleteQuestion(question: Question) { + this.questionController.deleteQuestion(question); + } } diff --git a/frontend/src/app/services/questions.service.spec.ts b/frontend/src/app/services/questions.service.spec.ts new file mode 100644 index 0000000..e130427 --- /dev/null +++ b/frontend/src/app/services/questions.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { QuestionsService } from './questions.service'; + +describe('QuestionsService', () => { + let service: QuestionsService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(QuestionsService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/services/questions.service.ts b/frontend/src/app/services/questions.service.ts new file mode 100644 index 0000000..82f9bbc --- /dev/null +++ b/frontend/src/app/services/questions.service.ts @@ -0,0 +1,26 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { Question } from '../Types/types'; + +const ROUTE = "http://0.0.0.0:30992/API/v1/question" + +@Injectable({ + providedIn: 'root' +}) +export class QuestionsService { + + constructor( + private http: HttpClient) { } + + deleteQuestion(question: Question) { + this.http.delete(ROUTE + "/" + question.id.toString()).subscribe(res => { + console.log("PLZ REFRESH") + }); + } + + // addUser(user: User) { + // this.http.post<User>(ROUTE, user).subscribe(res => { + // this.auth.login(user.username, user.password); + // }); + // } +} -- GitLab