diff --git a/server/database/data.json b/server/database/data.json index da72eb0..8ff3ec5 100644 --- a/server/database/data.json +++ b/server/database/data.json @@ -1,104 +1 @@ -{ - "data": [ - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false, - false - ] -} +{"data":[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false]} \ No newline at end of file diff --git a/server/routes/index.js b/server/routes/index.js index e744cab..85658e6 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -3,9 +3,28 @@ const fs = require('fs'); var router = express.Router(); +const path = __dirname + '/../database/data.json' + /* GET home page. */ router.get('/', function(req, res, next) { - res.json(JSON.parse(fs.readFileSync(__dirname + '/../database/data.json').toString())); + res.json(JSON.parse(fs.readFileSync(path).toString())); +}); + +router.post('/login', (req, res, _next) => { + if (req.body.username === 'Mediatheek' && req.body.password === '@MediatheekHetHeerenlanden!') { + res.json(true) + } else { + res.json(false); + } +}); + +router.post('/update/:index', (req, res, _next) => { + if (req.body.username === 'Mediatheek' && req.body.password === '@MediatheekHetHeerenlanden!') { + const data = JSON.parse(fs.readFileSync(path).toString()); + data.data[Number(req.params.index)] = data.data[Number(req.params.index)] ? false : true; + fs.writeFileSync(path, JSON.stringify(data)); + } + res.send(null); }); module.exports = router; diff --git a/src/app/app.module.ts b/src/app/app.module.ts index b6e6294..22065e7 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,6 +1,7 @@ import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { HttpClientModule } from '@angular/common/http'; +import { ReactiveFormsModule } from '@angular/forms'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; @@ -16,7 +17,8 @@ import { LoginComponent } from './components/login/login.component'; imports: [ BrowserModule, AppRoutingModule, - HttpClientModule + HttpClientModule, + ReactiveFormsModule ], providers: [], bootstrap: [AppComponent] diff --git a/src/app/components/home/home.component.html b/src/app/components/home/home.component.html index 953253e..17cf23a 100644 --- a/src/app/components/home/home.component.html +++ b/src/app/components/home/home.component.html @@ -1,6 +1,13 @@ -
+Log uit +
{{ i + 1 }}. {{ item ? 'Gereserveerd' : 'Niet gereserveerd' }} Reserveer
+ +
+
+ +
+
diff --git a/src/app/components/home/home.component.ts b/src/app/components/home/home.component.ts index 9c957d6..806ab39 100644 --- a/src/app/components/home/home.component.ts +++ b/src/app/components/home/home.component.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { Observable } from 'rxjs'; import { ApiData, DataService } from 'src/app/services/data.service'; +import { Router } from '@angular/router'; @Component({ selector: 'app-home', @@ -10,11 +11,31 @@ import { ApiData, DataService } from 'src/app/services/data.service'; export class HomeComponent implements OnInit { data: Observable; + loggedIn: boolean; - constructor(private dataService: DataService) { } + constructor( + private dataService: DataService, + private router: Router + ) { } ngOnInit() { this.data = this.dataService.getData(); + + const username = localStorage.getItem('username'); + const password = localStorage.getItem('password'); + this.dataService.login(username, password).subscribe(l => this.loggedIn = l); + } + + updateItem(index: number) { + const username = localStorage.getItem('username'); + const password = localStorage.getItem('password'); + + this.dataService.update(index, username, password).subscribe(res => this.data = this.dataService.getData()); + } + + logout() { + localStorage.clear(); + this.router.navigate(['/login']); } } diff --git a/src/app/components/login/login.component.html b/src/app/components/login/login.component.html index 147cfc4..3c70198 100644 --- a/src/app/components/login/login.component.html +++ b/src/app/components/login/login.component.html @@ -1 +1,9 @@ -

login works!

+
+ De combinatie van de gebruikersnaam en wachtwoord is niet herkend. +
+ +
+ + + +
diff --git a/src/app/components/login/login.component.ts b/src/app/components/login/login.component.ts index 12de138..2046cfb 100644 --- a/src/app/components/login/login.component.ts +++ b/src/app/components/login/login.component.ts @@ -1,4 +1,7 @@ import { Component, OnInit } from '@angular/core'; +import { FormBuilder, Validators } from '@angular/forms'; +import { DataService } from 'src/app/services/data.service'; +import { Router } from '@angular/router'; @Component({ selector: 'app-login', @@ -7,9 +10,34 @@ import { Component, OnInit } from '@angular/core'; }) export class LoginComponent implements OnInit { - constructor() { } + form = this.fb.group({ + username: ['', Validators.required], + password: ['', Validators.required] + }); + + alert = false; + + constructor( + private fb: FormBuilder, + private data: DataService, + private router: Router + ) { } ngOnInit() { } + onSubmit() { + const value = this.form.value; + this.data.login(value.username, value.password).subscribe(loggedIn => { + if (loggedIn) { + localStorage.setItem('username', value.username); + localStorage.setItem('password', value.password); + this.router.navigate(['/']); + } else { + this.alert = true; + setTimeout(() => this.alert = false, 4000); + } + }); + } + } diff --git a/src/app/services/data.service.ts b/src/app/services/data.service.ts index bdaa70c..e47a0ea 100644 --- a/src/app/services/data.service.ts +++ b/src/app/services/data.service.ts @@ -11,9 +11,19 @@ export interface ApiData { }) export class DataService { + apiUrl = 'http://localhost:3000'; + constructor(private http: HttpClient) { } getData(): Observable { - return this.http.get('http://localhost:3000'); + return this.http.get(this.apiUrl); + } + + login(username: string, password: string): Observable { + return this.http.post(this.apiUrl + '/login', {username, password}); + } + + update(index: number, username: string, password: string): Observable { + return this.http.post(this.apiUrl + '/update/' + index, {username, password}); } }