generated from nhcarrigan/template
feat: initial prototype works
I can log in and create a book! Woo!
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import { environment } from '../../environments/environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ApiService {
|
||||
private readonly apiUrl = environment.apiUrl;
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
private getHeaders(): HttpHeaders {
|
||||
// Auth token is sent as httpOnly cookie, no need to add manually
|
||||
return new HttpHeaders({
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
}
|
||||
|
||||
get<T>(endpoint: string): Observable<T> {
|
||||
return this.http.get<T>(`${this.apiUrl}${endpoint}`, {
|
||||
headers: this.getHeaders(),
|
||||
withCredentials: true // Important for cookies
|
||||
});
|
||||
}
|
||||
|
||||
post<T>(endpoint: string, body: any): Observable<T> {
|
||||
return this.http.post<T>(`${this.apiUrl}${endpoint}`, body, {
|
||||
headers: this.getHeaders(),
|
||||
withCredentials: true
|
||||
});
|
||||
}
|
||||
|
||||
put<T>(endpoint: string, body: any): Observable<T> {
|
||||
return this.http.put<T>(`${this.apiUrl}${endpoint}`, body, {
|
||||
headers: this.getHeaders(),
|
||||
withCredentials: true
|
||||
});
|
||||
}
|
||||
|
||||
delete<T>(endpoint: string): Observable<T> {
|
||||
return this.http.delete<T>(`${this.apiUrl}${endpoint}`, {
|
||||
headers: this.getHeaders(),
|
||||
withCredentials: true
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Injectable, signal } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { Observable, tap } from 'rxjs';
|
||||
import { ApiService } from './api.service';
|
||||
import { AuthResponse, User } from '@library/shared-types';
|
||||
import { environment } from '../../environments/environment';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthService {
|
||||
private currentUser = signal<User | null>(null);
|
||||
public readonly user = this.currentUser.asReadonly();
|
||||
|
||||
constructor(
|
||||
private api: ApiService,
|
||||
private router: Router
|
||||
) {}
|
||||
|
||||
login(): void {
|
||||
// Redirect to API login endpoint
|
||||
window.location.href = `${environment.apiUrl}/auth/login`;
|
||||
}
|
||||
|
||||
getCurrentUser(): Observable<AuthResponse> {
|
||||
return this.api.get<AuthResponse>('/auth/me').pipe(
|
||||
tap(response => {
|
||||
this.currentUser.set(response.user);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
logout(): Observable<{ message: string }> {
|
||||
return this.api.post<{ message: string }>('/auth/logout', {}).pipe(
|
||||
tap(() => {
|
||||
this.currentUser.set(null);
|
||||
this.router.navigate(['/']);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
isAuthenticated(): boolean {
|
||||
return this.user() !== null;
|
||||
}
|
||||
|
||||
isAdmin(): boolean {
|
||||
return this.user()?.isAdmin === true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { ApiService } from './api.service';
|
||||
import { Book, CreateBookDto, UpdateBookDto } from '@library/shared-types';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class BooksService {
|
||||
constructor(private api: ApiService) {}
|
||||
|
||||
getAllBooks(): Observable<Book[]> {
|
||||
return this.api.get<Book[]>('/books');
|
||||
}
|
||||
|
||||
getBookById(id: string): Observable<Book | null> {
|
||||
return this.api.get<Book | null>(`/books/${id}`);
|
||||
}
|
||||
|
||||
createBook(book: CreateBookDto): Observable<Book> {
|
||||
return this.api.post<Book>('/books', book);
|
||||
}
|
||||
|
||||
updateBook(id: string, book: UpdateBookDto): Observable<Book> {
|
||||
return this.api.put<Book>(`/books/${id}`, book);
|
||||
}
|
||||
|
||||
deleteBook(id: string): Observable<{ success: boolean }> {
|
||||
return this.api.delete<{ success: boolean }>(`/books/${id}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { ApiService } from './api.service';
|
||||
import { Game, CreateGameDto, UpdateGameDto } from '@library/shared-types';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class GamesService {
|
||||
constructor(private api: ApiService) {}
|
||||
|
||||
getAllGames(): Observable<Game[]> {
|
||||
return this.api.get<Game[]>('/games');
|
||||
}
|
||||
|
||||
getGameById(id: string): Observable<Game | null> {
|
||||
return this.api.get<Game | null>(`/games/${id}`);
|
||||
}
|
||||
|
||||
createGame(game: CreateGameDto): Observable<Game> {
|
||||
return this.api.post<Game>('/games', game);
|
||||
}
|
||||
|
||||
updateGame(id: string, game: UpdateGameDto): Observable<Game> {
|
||||
return this.api.put<Game>(`/games/${id}`, game);
|
||||
}
|
||||
|
||||
deleteGame(id: string): Observable<{ success: boolean }> {
|
||||
return this.api.delete<{ success: boolean }>(`/games/${id}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
export { ApiService } from './api.service';
|
||||
export { AuthService } from './auth.service';
|
||||
export { BooksService } from './books.service';
|
||||
export { GamesService } from './games.service';
|
||||
export { MusicService } from './music.service';
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { ApiService } from './api.service';
|
||||
import { Music, CreateMusicDto, UpdateMusicDto } from '@library/shared-types';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class MusicService {
|
||||
constructor(private api: ApiService) {}
|
||||
|
||||
getAllMusic(): Observable<Music[]> {
|
||||
return this.api.get<Music[]>('/music');
|
||||
}
|
||||
|
||||
getMusicById(id: string): Observable<Music | null> {
|
||||
return this.api.get<Music | null>(`/music/${id}`);
|
||||
}
|
||||
|
||||
createMusic(music: CreateMusicDto): Observable<Music> {
|
||||
return this.api.post<Music>('/music', music);
|
||||
}
|
||||
|
||||
updateMusic(id: string, music: UpdateMusicDto): Observable<Music> {
|
||||
return this.api.put<Music>(`/music/${id}`, music);
|
||||
}
|
||||
|
||||
deleteMusic(id: string): Observable<{ success: boolean }> {
|
||||
return this.api.delete<{ success: boolean }>(`/music/${id}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user