Skip to content

Commit

Permalink
att review sistem
Browse files Browse the repository at this point in the history
  • Loading branch information
hit25082000 committed Nov 1, 2024
1 parent d736bc2 commit 0360a5a
Show file tree
Hide file tree
Showing 45 changed files with 1,713 additions and 237 deletions.
10 changes: 7 additions & 3 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component } from '@angular/core';
import { MockDataInsertion } from './core/services/mock-data.service';
import { Component, inject, OnInit } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { AuthService } from './auth/services/auth.service';
import { Router } from '@angular/router';
Expand All @@ -13,8 +14,11 @@ import { SpeechToggleComponent } from './features/speech-toggle/speech-toggle.co
styleUrl: './app.component.scss'
})

export class AppComponent {
constructor(private auth: AuthService, private router: Router) {
export class AppComponent implements OnInit {
mock = inject(MockDataInsertion)
constructor(private auth: AuthService, private router: Router) {}

ngOnInit() {
//this.mock.insertMockData();
}
}
25 changes: 23 additions & 2 deletions src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,32 @@ import { AboutComponent } from './pages/about/about.component';
import { PromoComponent } from './pages/promo/promo.component';
import { GamesComponent } from './pages/games/games.component';
import { AuthGuard } from './auth/guards/auth.guard';
import { AdminComponent } from './pages/admin/admin.component';
import { GameManagementComponent } from './features/game-management/game-management.component';
import { LanguageManagementComponent } from './features/language-management/language-management.component';
import { AdminGuard } from './auth/guards/admin.guard';

export const routes: Routes = [
{path: '',
component: MainComponent},
{ path: 'admin', component: AdminComponent,
canActivate: [AuthGuard, AdminGuard],
children: [
{
path: '',
redirectTo: 'games-management',
pathMatch: 'full'
},
{
path: 'games-management',
component: GameManagementComponent,
},
{
path: 'language-management',
component: LanguageManagementComponent,
},
]
},
{path: 'index',
component: MainComponent,
children: [
Expand Down Expand Up @@ -38,6 +60,5 @@ export const routes: Routes = [
component: LoginComponent},
{path: 'register',
component: RegisterComponent},
//{path: 'unauthorized', component: UnauthorizedComponent},
//{path: '**', component: NotFoundComponent},
{path: '**', component: LoginComponent},
];
33 changes: 33 additions & 0 deletions src/app/auth/guards/admin.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AuthService } from '../services/auth.service';
import { Observable, of } from 'rxjs';
import { map, take, switchMap } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
})
export class AdminGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}

canActivate(): Observable<boolean> {
return this.authService.user$.pipe(
take(1),
switchMap(user => {
if (!user) {
this.router.navigate(['/login']);
return of(false);
}
return this.authService.isAdmin$;
}),
map(isAdmin => {
if (isAdmin) {
return true;
} else {
this.router.navigate(['/index/games']);
return false;
}
})
);
}
}
52 changes: 17 additions & 35 deletions src/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
import { inject, Injectable } from '@angular/core';
import { Auth, createUserWithEmailAndPassword, deleteUser, getAuth, sendEmailVerification, sendPasswordResetEmail, signInWithCredential, signInWithCustomToken, signInWithEmailAndPassword, updateCurrentUser, updateEmail, updatePassword, updateProfile, User, user } from '@angular/fire/auth';
import { Subscription } from 'rxjs';
import { Auth, signInWithEmailAndPassword, createUserWithEmailAndPassword, signOut, user } from '@angular/fire/auth';
import { BehaviorSubject, Observable, map } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class AuthService {
private auth: Auth = inject(Auth);
private isAdminSubject = new BehaviorSubject<boolean>(false);
isAdmin$ = this.isAdminSubject.asObservable();
user$ = user(this.auth);
userSubscription: Subscription;
isLoggedIn$: Observable<boolean>;

constructor() {
this.userSubscription = this.user$.subscribe((aUser: any | null) => {
console.log(aUser);
})
this.isLoggedIn$ = this.user$.pipe(map(user => !!user));
this.user$.subscribe(user => this.checkAdminStatus(user?.email));
}

getCurrentUser(){
return this.auth.currentUser;
}

async register(email : string,password : string){
var userCredential = await createUserWithEmailAndPassword(this.auth, email, password)

return sendEmailVerification(userCredential.user)
async login(email: string, password: string): Promise<void> {
await signInWithEmailAndPassword(this.auth, email, password);
}

login(email : string,password : string){
return signInWithEmailAndPassword(this.auth,email, password);
async register(email: string, password: string): Promise<void> {
await createUserWithEmailAndPassword(this.auth, email, password);
}

logout(){
this.auth.signOut()
async logout(): Promise<void> {
await signOut(this.auth);
}

passwordReset(email : string){
sendPasswordResetEmail(this.auth, email)
.then(() => {
// Password reset email sent!
// ..
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
});
private checkAdminStatus(email: string | null | undefined): void {
this.isAdminSubject.next(email?.toLowerCase() === '[email protected]');
}

isLoggedIn(): boolean {
return !!this.auth.currentUser;
}

ngOnDestroy() {
this.auth.signOut()
getCurrentUser() {
return this.auth.currentUser;
}
}
12 changes: 12 additions & 0 deletions src/app/core/models/game.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Language } from "./language.model";

export interface Game {
id: string;
img: string;
alt: string;
name: string;
genre: string;
description: string;
language: Language;
}

9 changes: 9 additions & 0 deletions src/app/core/models/language.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Game } from "./game.model";

export interface Language {
id?: string;
name: string;
flag: string;
culturalCuriosities: string[];
games: Game[];
}
9 changes: 9 additions & 0 deletions src/app/core/models/review.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Review {
id?: string;
userId: string;
gameId: string;
rating: number;
comment: string;
userName: string;
createdAt: Date;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TestBed } from '@angular/core/testing';

import { ProductService } from './product.service';
import { ProductService } from './mock-data.service';

describe('ProductService', () => {
let service: ProductService;
Expand Down
65 changes: 65 additions & 0 deletions src/app/core/services/firestore.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { inject, Injectable } from '@angular/core';
import { collection, collectionData, Firestore, doc, docData, setDoc, deleteDoc } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

@Injectable({
providedIn: 'root'
})
export class FirestoreService {
firestore: Firestore = inject(Firestore);

constructor() {}

getCollection<T>(path: string): Observable<T[]> {
return collectionData(collection(this.firestore, path), {
idField: 'id',
}) as Observable<T[]>;
}

getDocument<T>(path: string, id: string): Observable<T> {
return docData(doc(this.firestore, path, id), { idField: 'id' }) as Observable<T>;
}

createDocument<T>(
path: string,
id: string,
data: any
): Observable<void> { // Alterado para retornar um Observable
const docRef = doc(this.firestore, path, id);
return new Observable<void>((observer) => {
setDoc(docRef, { ...data }).then(() => {
observer.next(); // Notifica que a operação foi concluída
observer.complete(); // Completa o Observable
}).catch((error) => observer.error(error)); // Notifica erro, se ocorrer
});
}

generateId(path: string, id?: string): Observable<string> {
const taskCollection = collection(this.firestore, path);
const docRef = id ? doc(taskCollection, id) : doc(taskCollection);
return new Observable<string>((observer) => {
observer.next(docRef.id);
observer.complete();
});
}

updateDocument<T>(path: string, id: string, data: Partial<T>): Observable<void> {
const docRef = doc(this.firestore, path, id);
return new Observable<void>((observer) => {
setDoc(docRef, data, { merge: true }).then(() => {
observer.next();
observer.complete();
}).catch((error) => observer.error(error));
});
}

deleteDocument(path: string, id: string): Observable<void> {
const docRef = doc(this.firestore, path, id);
return new Observable<void>((observer) => {
deleteDoc(docRef).then(() => {
observer.next();
observer.complete();
}).catch((error) => observer.error(error));
});
}
}
16 changes: 16 additions & 0 deletions src/app/core/services/game.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { GameService } from './game.service';

describe('GameService', () => {
let service: GameService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(GameService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
45 changes: 45 additions & 0 deletions src/app/core/services/game.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Injectable } from '@angular/core';
import { FirestoreService } from './firestore.service';
import { Observable } from 'rxjs';
import { Game } from '../models/game.model';

@Injectable({
providedIn: 'root'
})
export class GameService {
private path = 'games'; // Nome da coleção no Firestore

constructor(private firestoreService: FirestoreService) {}

getGames(): Observable<Game[]> {
return this.firestoreService.getCollection<Game>(this.path);
}

getGame(id: string): Observable<Game> {
return this.firestoreService.getDocument<Game>(this.path, id);
}

createGame(game: Game): Observable<void> {
const id = this.firestoreService.generateId(this.path);
return new Observable<void>((observer) => {
id.subscribe((generatedId) => {
this.firestoreService.createDocument<Game>(this.path, generatedId, { ...game, id: generatedId })
.subscribe({
next: () => {
observer.next();
observer.complete();
},
error: (error) => observer.error(error)
});
});
});
}

updateGame(id: string, game: Partial<Game>): Observable<void> {
return this.firestoreService.updateDocument<Game>(this.path, id, game);
}

deleteGame(id: string): Observable<void> {
return this.firestoreService.deleteDocument(this.path, id);
}
}
16 changes: 16 additions & 0 deletions src/app/core/services/language.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { LanguageService } from './language.service';

describe('LanguageService', () => {
let service: LanguageService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(LanguageService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
Loading

0 comments on commit 0360a5a

Please sign in to comment.