diff --git a/API/db/app.db b/API/db/app.db index e85f4c448850ec4990b567bd869ec13c5a97fb75..b9f76f706498f88f8a0278596752a3792021fdff 100644 Binary files a/API/db/app.db and b/API/db/app.db differ diff --git a/API/src/database/Database.ts b/API/src/database/Database.ts index b903197f87a86601699d44349b081c92975523f0..17402400abcfe1549f4af64efdf4b267defc258e 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 41a9dc8f4d64747505945169c1c5f8dc5a706a69..5c9db808c55e4b7369a19ec5b337009139348a9d 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 405dfe29b65dc493a6d66f6abc0440c79f2cdff1..a6f41f8b2704d2920d4c29671e55e860da42a814 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 ad5e3e325b61733eb9b346b2f9b8c8b54a97806f..e1119cd890019ef0d22613e92683d42c0b2c0e1a 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 e899c1c4e64276e714c9fbc81d4e1dfb7fb50d68..d1c2fc1aa94b912f2ec7fedca65e9bebb4680534 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 0000000000000000000000000000000000000000..e130427da2a82e81134ef6c4397450f71db8474d --- /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 0000000000000000000000000000000000000000..82f9bbc1a862c26509c3ddbb09787e5e2aac591d --- /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); + // }); + // } +}