diff --git a/src/app/auth/register/register.component.html b/src/app/auth/register/register.component.html index 2c52bec..d9d4431 100644 --- a/src/app/auth/register/register.component.html +++ b/src/app/auth/register/register.component.html @@ -9,6 +9,14 @@ + + + + + + + + diff --git a/src/app/auth/register/register.component.ts b/src/app/auth/register/register.component.ts index 131ea4e..8bb5240 100644 --- a/src/app/auth/register/register.component.ts +++ b/src/app/auth/register/register.component.ts @@ -2,6 +2,7 @@ import { Component } from '@angular/core'; import { FormControl, FormGroup, Validators } from '@angular/forms'; import { AuthService } from '../../services/auth/auth.service'; import { Router } from '@angular/router'; +import { UserService } from '../../services/data/user.service'; @Component({ selector: 'app-register', @@ -12,10 +13,11 @@ export class RegisterComponent { registerForm = new FormGroup({ email: new FormControl('', [Validators.email]), password: new FormControl(''), + username: new FormControl(''), rePassword: new FormControl('') }); - constructor(private router: Router, private auth: AuthService) { + constructor(private router: Router, private auth: AuthService, private user: UserService) { this.registerForm.addValidators([Validators.required, Validators.minLength(3)]); } @@ -26,9 +28,17 @@ export class RegisterComponent { this.registerForm.get('password')?.value as String, this.registerForm.get('rePassword')?.value as String, ) - .then(()=> { this.router.navigateByUrl('/auth/login') }) - .catch(()=> { alert('failed to register') }); - // TODO: better error messages + .then((cred) => { + this.user.createUser( + this.registerForm.get('username')?.value as String, + this.registerForm.get('email')?.value as String, + cred.user?.uid as String + ).then(() => { this.router.navigateByUrl('/auth/login') }).catch( + () => { alert('failed to create user'); } + ) + }) + .catch(() => { alert('failed to register') }); + // TODO: better error messages else alert("failed to register"); } diff --git a/src/app/services/data/post.service.ts b/src/app/services/data/post.service.ts index 630a998..c07d435 100644 --- a/src/app/services/data/post.service.ts +++ b/src/app/services/data/post.service.ts @@ -1,66 +1,29 @@ import { Injectable } from '@angular/core'; import { Post } from '../../../model/Post'; import { AuthService } from '../auth/auth.service'; +import { AngularFirestore, Reference } from '@angular/fire/compat/firestore'; +import { User } from '../../../model/User'; +import { UserService } from './user.service'; +import { user } from '@angular/fire/auth'; @Injectable({ providedIn: 'root' }) export class PostService { - constructor(private auth: AuthService) { } - - examplePosts: Array = new Array( - { - user: { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - }, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - { - user: { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - }, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - { - user: { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - }, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - { - user: { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - }, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - ); + constructor(private auth: AuthService, private afs: AngularFirestore) { } getFeed(length: Number, start: Number): Promise> { return new Promise>((resolve, reject) => { - // TODO: fetch feed if (this.auth.checkAllowed()) { - resolve(this.examplePosts) + this.afs.collection('post').valueChanges().subscribe(data => { + data.forEach(i => { + this.afs.doc((i as any).user).valueChanges().subscribe(user => { + i.user = user as User; + }); + }); + resolve(data); + }); } else { reject() } @@ -68,32 +31,52 @@ export class PostService { } getPosts(username: String, lenght: Number, start: Number) { + console.log(username) return new Promise>((resolve, reject) => { - // TODO: fetch feed if (this.auth.checkAllowed()) { - resolve(this.examplePosts) + if (username == undefined) { + this.auth.getToken().subscribe(cred => { + this.afs.collection('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => { + this.afs.collection('post', ref => ref.where('user', '==', this.afs.doc('/user/' + user[0].username).ref)).valueChanges().subscribe(data => { + data.forEach(i => { + i.user = user[0] as User; + }) + resolve(data) + }); + }) + }); + } else { + this.afs.collection('user').doc(username as string).valueChanges().subscribe(user => { + this.afs.collection('post', ref => ref.where('user', '==', this.afs.doc('/user/' + username).ref)).valueChanges().subscribe(data => { + data.forEach(i => { + i.user = user as User; + }) + resolve(data) + }); + }); + } } else { reject() } }); } + // FIX: duplicate posts newPost(content: String, image: String) { - return new Promise((resolve, reject) => { - // TODO: make post + return new Promise((resolve, reject) => { + console.log('new post') if (this.auth.checkAllowed()) { - this.examplePosts.push({ - user: { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - }, - content: content, - image: image, - likes: 0, - liked: false + this.auth.getToken().subscribe(data => { + this.afs.collection('user', ref => ref.where('id', '==', data?.uid)).valueChanges().subscribe(user => { + this.afs.collection('post').add({ + user: this.afs.doc('/user/' + user[0].username).ref, + content: content, + image: image, + }) + .then(() => resolve()) + .catch(() => reject()); + }); }); - resolve(true); } else { reject(); } diff --git a/src/app/services/data/user.service.ts b/src/app/services/data/user.service.ts index acb2feb..695a45f 100644 --- a/src/app/services/data/user.service.ts +++ b/src/app/services/data/user.service.ts @@ -1,28 +1,40 @@ import { Injectable } from '@angular/core'; import { User } from '../../../model/User'; import { AuthService } from '../auth/auth.service'; +import { AngularFirestore } from '@angular/fire/compat/firestore'; +import { user } from '@angular/fire/auth'; @Injectable({ providedIn: 'root' }) export class UserService { - - exampleUser: User = { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - } - - constructor(private auth: AuthService) { } + constructor(private auth: AuthService, private afs: AngularFirestore) { } getUser(username: String): Promise { return new Promise((resolve, reject) => { - // TODO: get user if (this.auth.checkAllowed()) { - resolve(this.exampleUser); + if (username != undefined) { + this.afs.collection('user', ref => ref.where('username', '==', username)).valueChanges().subscribe(data => { resolve(data[0] as User) }); + } else { + this.auth.getToken().subscribe(cred => { + this.afs.collection('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => { resolve(user[0]) }) + }); + } } else { reject(); } }); } + + createUser(username: String, email: String, id: String) { + return new Promise((resolve, reject) => { + this.afs.collection('user').doc(username as string).set({ + username: username, + email: email, + picture: "/assets/placeholder-profile-picture.png", + id: id + }); + resolve(); + }); + } } diff --git a/src/model/User.ts b/src/model/User.ts index b60e0e1..60c8b3e 100644 --- a/src/model/User.ts +++ b/src/model/User.ts @@ -1,5 +1,6 @@ export interface User { username: String; + email: String; picture: String; followed: Boolean; } \ No newline at end of file