From 2a472f66cdd7394820bf1f227e28354d6f426f4d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 7 May 2023 23:05:00 +0200 Subject: [PATCH] =?UTF-8?q?Regisztr=C3=A1ci=C3=B3,=20term=C3=A9kszerkeszt?= =?UTF-8?q?=C3=A9s=20fix,=20DB=20hook=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- .../app/product-edit/product-edit.component.ts | 16 +++++++++++----- .../product-list/product-list.component.html | 5 +++++ .../app/product-list/product-list.component.ts | 2 +- .../src/app/register/register.component.html | 16 +++++++++++++++- client/src/app/register/register.component.ts | 16 ++++++++++++++++ client/src/app/services/product.service.ts | 2 +- client/src/app/services/user.service.ts | 5 +++++ server/.idea/vcs.xml | 7 +++++++ server/db/productSchema.js | 2 +- server/usersRouter.js | 18 +++++++++++++++++- 11 files changed, 83 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f36d6ff..413bfbc 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,10 @@ A projekt alapértelmezetten localhost:27017 cimen csatlakozik a MongoDB-hez, a A projekt mappájában adjuk ki a következő parancsokat: ```bash -cd frontend +cd client npm install npm run build -cd ../backend +cd ../server npm install npm start ``` @@ -30,8 +30,8 @@ Bejelentkezés után választhatunk a terméklista és a felhasználólista köz Fontos pontok: * Statikus hosztolás: eleve jó helyre buildelődik a frontend * Adatbázis: órai felépités -* Adatbázis hook: jelszótitkositás és termék ID generálás +* Adatbázis hook: jelszótitkositás és termék azonositó generálás * Route-ok adottak -* Van navigáció frontenden (terméklista/felhasználólista, termék szerkesztés) +* Van navigáció frontenden (terméklista/felhasználólista, termék szerkesztés/login/register) * Service-eken keresztül kommunikál a backenddel * Van auth guard diff --git a/client/src/app/product-edit/product-edit.component.ts b/client/src/app/product-edit/product-edit.component.ts index b4eeb03..b44cb06 100644 --- a/client/src/app/product-edit/product-edit.component.ts +++ b/client/src/app/product-edit/product-edit.component.ts @@ -2,7 +2,7 @@ import { Component } from '@angular/core'; import { ProductService } from '../services/product.service'; import { Product } from '../model/product.model'; import { ActivatedRoute } from '@angular/router'; -import { flatMap, mergeMap, Observable } from 'rxjs'; +import { firstValueFrom, flatMap, mergeMap, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { FormControl, FormGroup } from '@angular/forms'; @@ -14,13 +14,19 @@ import { FormControl, FormGroup } from '@angular/forms'; export class ProductEditComponent { form = new FormGroup({ name: new FormControl() - }) + }); + + product: Product | undefined; constructor(private service: ProductService, route: ActivatedRoute) { - service.get(route.snapshot.params['id']).subscribe((value: Product) => this.form.patchValue(value)); + service.get(route.snapshot.params['id']).subscribe((value: Product) => { + this.form.patchValue(value); + this.product = value; + }); } - save() { - this.service.edit(this.form.value as Product); + async save() { + const product = await firstValueFrom(this.service.edit({...this.product, ...this.form.value} as Product)); + console.log("Edited product:", product); } } diff --git a/client/src/app/product-list/product-list.component.html b/client/src/app/product-list/product-list.component.html index 8b6ace7..f77952b 100644 --- a/client/src/app/product-list/product-list.component.html +++ b/client/src/app/product-list/product-list.component.html @@ -15,6 +15,11 @@ {{row.price}} + + Azonositó + {{row.id}} + + Műveletek diff --git a/client/src/app/product-list/product-list.component.ts b/client/src/app/product-list/product-list.component.ts index 2f866cc..2b7adde 100644 --- a/client/src/app/product-list/product-list.component.ts +++ b/client/src/app/product-list/product-list.component.ts @@ -22,7 +22,7 @@ export class ProductListComponent implements AfterViewInit { products: Observable /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ - displayedColumns = ['name', 'description', 'price', 'actions']; + displayedColumns = ['name', 'description', 'price', 'id', 'actions']; constructor(private service: ProductService, public userService: UserService) { this.products = service.getList().pipe(map(value => value as Product[])); diff --git a/client/src/app/register/register.component.html b/client/src/app/register/register.component.html index 6b0ba2e..ad447dc 100644 --- a/client/src/app/register/register.component.html +++ b/client/src/app/register/register.component.html @@ -1 +1,15 @@ -

register works!

+
+ + Felhasználónév + + + + Jelszó + + + + Születési dátum + + + +
diff --git a/client/src/app/register/register.component.ts b/client/src/app/register/register.component.ts index 21647df..c6338e2 100644 --- a/client/src/app/register/register.component.ts +++ b/client/src/app/register/register.component.ts @@ -1,4 +1,7 @@ import { Component } from '@angular/core'; +import { FormControl, FormGroup } from '@angular/forms'; +import { UserService } from '../services/user.service'; +import { Router } from '@angular/router'; @Component({ selector: 'app-register', @@ -6,5 +9,18 @@ import { Component } from '@angular/core'; styleUrls: ['./register.component.css'] }) export class RegisterComponent { + form = new FormGroup({ + username: new FormControl(), + password: new FormControl(), + birthdate: new FormControl() + }); + constructor(private userService: UserService, private router: Router) { + } + + async doRegister() { + const val = this.form.value; + await this.userService.register(val.username, val.password, val.birthdate); + await this.router.navigate(['/']); + } } diff --git a/client/src/app/services/product.service.ts b/client/src/app/services/product.service.ts index b9815ad..17b7799 100644 --- a/client/src/app/services/product.service.ts +++ b/client/src/app/services/product.service.ts @@ -15,7 +15,7 @@ export class ProductService { } edit(data: Product) { - return this.http.patch('/api/products/' + data.id, data); + return this.http.patch('/api/products/' + data._id, data); } get(id: string) { diff --git a/client/src/app/services/user.service.ts b/client/src/app/services/user.service.ts index 885fae9..2084dcf 100644 --- a/client/src/app/services/user.service.ts +++ b/client/src/app/services/user.service.ts @@ -44,6 +44,11 @@ export class UserService { } } + async register(username: string, password: string, birthdate: string) { + const user = await firstValueFrom(this.http.post('/api/users/register', {username, password, birthdate})); + console.log("Registered user:", user); + } + getList() { return this.http.get('/api/users') } diff --git a/server/.idea/vcs.xml b/server/.idea/vcs.xml index 6c0b863..be4a9c1 100644 --- a/server/.idea/vcs.xml +++ b/server/.idea/vcs.xml @@ -1,5 +1,12 @@ + + + diff --git a/server/db/productSchema.js b/server/db/productSchema.js index 3cf5cf2..158acd8 100644 --- a/server/db/productSchema.js +++ b/server/db/productSchema.js @@ -26,7 +26,7 @@ const productSchema = new mongoose.Schema({ productSchema.pre('save', function(next) { const product = this; if(product.isModified('name')) { - product.name = product.name.replaceAll(/[ !?$.,-]/g, ''); + product.id = product.name.replaceAll(/[ !?$.,-]/g, '').toLowerCase(); } return next(); }); diff --git a/server/usersRouter.js b/server/usersRouter.js index fb79a53..a9237dc 100644 --- a/server/usersRouter.js +++ b/server/usersRouter.js @@ -36,7 +36,23 @@ router.route('/logout').post((req, res) => { } else { return res.status(403).json({status: 'NOTOK'}); } -}) +}); + +router.route('/register').post(async (req, res) => { + const user = new User({ + username: req.body.username, + password: req.body.password, + accessLevel: 1, + birthdate: req.body.birthdate, + }); + + try { + const newUser = await user.save(); + res.status(201).json(newUser); + } catch (error) { + res.status(400).json({ message: error.message }); + } +}); router.route('/status').get((req, res) => { if (req.isAuthenticated()) {