generated from nhcarrigan/template
feat: initial prototype works
I can log in and create a book! Woo!
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
import { JwtPayload, User } from "@library/shared-types";
|
||||
import { PrismaClient } from "../../generated/prisma";
|
||||
import { prisma } from "../lib/prisma";
|
||||
|
||||
export class AuthService {
|
||||
private prisma: PrismaClient;
|
||||
private prisma = prisma;
|
||||
|
||||
constructor(private readonly app: FastifyInstance) {
|
||||
this.prisma = new PrismaClient();
|
||||
}
|
||||
constructor(private readonly app: FastifyInstance) {}
|
||||
|
||||
/**
|
||||
* Generate JWT token for user.
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Book, BookStatus, CreateBookDto, UpdateBookDto } from "@library/shared-types";
|
||||
import { prisma } from "../lib/prisma";
|
||||
|
||||
export class BookService {
|
||||
private prisma = prisma;
|
||||
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Get all books.
|
||||
*/
|
||||
async getAllBooks(): Promise<Book[]> {
|
||||
const books = await this.prisma.book.findMany({
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
|
||||
return books.map((book) => ({
|
||||
...book,
|
||||
status: book.status.toLowerCase() as BookStatus,
|
||||
dateAdded: book.dateAdded,
|
||||
dateFinished: book.dateFinished || undefined,
|
||||
createdAt: book.createdAt,
|
||||
updatedAt: book.updatedAt,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get book by ID.
|
||||
*/
|
||||
async getBookById(id: string): Promise<Book | null> {
|
||||
const book = await this.prisma.book.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!book) return null;
|
||||
|
||||
return {
|
||||
...book,
|
||||
status: book.status.toLowerCase() as BookStatus,
|
||||
dateAdded: book.dateAdded,
|
||||
dateFinished: book.dateFinished || undefined,
|
||||
createdAt: book.createdAt,
|
||||
updatedAt: book.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new book.
|
||||
*/
|
||||
async createBook(data: CreateBookDto): Promise<Book> {
|
||||
const book = await this.prisma.book.create({
|
||||
data: {
|
||||
...data,
|
||||
status: data.status.toUpperCase() as any,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
...book,
|
||||
status: book.status.toLowerCase() as BookStatus,
|
||||
dateAdded: book.dateAdded,
|
||||
dateFinished: book.dateFinished || undefined,
|
||||
createdAt: book.createdAt,
|
||||
updatedAt: book.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update book by ID.
|
||||
*/
|
||||
async updateBook(id: string, data: UpdateBookDto): Promise<Book> {
|
||||
const updateData = { ...data };
|
||||
if (updateData.status) {
|
||||
updateData.status = updateData.status.toUpperCase() as any;
|
||||
}
|
||||
|
||||
const book = await this.prisma.book.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
return {
|
||||
...book,
|
||||
status: book.status.toLowerCase() as BookStatus,
|
||||
dateAdded: book.dateAdded,
|
||||
dateFinished: book.dateFinished || undefined,
|
||||
createdAt: book.createdAt,
|
||||
updatedAt: book.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete book by ID.
|
||||
*/
|
||||
async deleteBook(id: string): Promise<void> {
|
||||
await this.prisma.book.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Game, GameStatus, CreateGameDto, UpdateGameDto } from "@library/shared-types";
|
||||
import { prisma } from "../lib/prisma";
|
||||
|
||||
export class GameService {
|
||||
private prisma = prisma;
|
||||
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Get all games.
|
||||
*/
|
||||
async getAllGames(): Promise<Game[]> {
|
||||
const games = await this.prisma.game.findMany({
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
|
||||
return games.map((game) => ({
|
||||
...game,
|
||||
status: game.status.toLowerCase() as GameStatus,
|
||||
dateAdded: game.dateAdded,
|
||||
dateCompleted: game.dateCompleted || undefined,
|
||||
createdAt: game.createdAt,
|
||||
updatedAt: game.updatedAt,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get game by ID.
|
||||
*/
|
||||
async getGameById(id: string): Promise<Game | null> {
|
||||
const game = await this.prisma.game.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!game) return null;
|
||||
|
||||
return {
|
||||
...game,
|
||||
status: game.status.toLowerCase() as GameStatus,
|
||||
dateAdded: game.dateAdded,
|
||||
dateCompleted: game.dateCompleted || undefined,
|
||||
createdAt: game.createdAt,
|
||||
updatedAt: game.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new game.
|
||||
*/
|
||||
async createGame(data: CreateGameDto): Promise<Game> {
|
||||
const game = await this.prisma.game.create({
|
||||
data: {
|
||||
...data,
|
||||
status: data.status.toUpperCase() as any,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
...game,
|
||||
status: game.status.toLowerCase() as GameStatus,
|
||||
dateAdded: game.dateAdded,
|
||||
dateCompleted: game.dateCompleted || undefined,
|
||||
createdAt: game.createdAt,
|
||||
updatedAt: game.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update game by ID.
|
||||
*/
|
||||
async updateGame(id: string, data: UpdateGameDto): Promise<Game> {
|
||||
const updateData = { ...data };
|
||||
if (updateData.status) {
|
||||
updateData.status = updateData.status.toUpperCase() as any;
|
||||
}
|
||||
|
||||
const game = await this.prisma.game.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
return {
|
||||
...game,
|
||||
status: game.status.toLowerCase() as GameStatus,
|
||||
dateAdded: game.dateAdded,
|
||||
dateCompleted: game.dateCompleted || undefined,
|
||||
createdAt: game.createdAt,
|
||||
updatedAt: game.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete game by ID.
|
||||
*/
|
||||
async deleteGame(id: string): Promise<void> {
|
||||
await this.prisma.game.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
export { AuthService } from "./auth.service";
|
||||
export { GameService } from "./game.service";
|
||||
export { BookService } from "./book.service";
|
||||
export { MusicService } from "./music.service";
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Music, MusicStatus, MusicType, CreateMusicDto, UpdateMusicDto } from "@library/shared-types";
|
||||
import { prisma } from "../lib/prisma";
|
||||
|
||||
export class MusicService {
|
||||
private prisma = prisma;
|
||||
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Get all music.
|
||||
*/
|
||||
async getAllMusic(): Promise<Music[]> {
|
||||
const musicItems = await this.prisma.music.findMany({
|
||||
orderBy: { updatedAt: "desc" },
|
||||
});
|
||||
|
||||
return musicItems.map((music) => ({
|
||||
...music,
|
||||
type: music.type.toLowerCase() as MusicType,
|
||||
status: music.status.toLowerCase() as MusicStatus,
|
||||
dateAdded: music.dateAdded,
|
||||
dateCompleted: music.dateCompleted || undefined,
|
||||
createdAt: music.createdAt,
|
||||
updatedAt: music.updatedAt,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get music by ID.
|
||||
*/
|
||||
async getMusicById(id: string): Promise<Music | null> {
|
||||
const music = await this.prisma.music.findUnique({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
if (!music) return null;
|
||||
|
||||
return {
|
||||
...music,
|
||||
type: music.type.toLowerCase() as MusicType,
|
||||
status: music.status.toLowerCase() as MusicStatus,
|
||||
dateAdded: music.dateAdded,
|
||||
dateCompleted: music.dateCompleted || undefined,
|
||||
createdAt: music.createdAt,
|
||||
updatedAt: music.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new music.
|
||||
*/
|
||||
async createMusic(data: CreateMusicDto): Promise<Music> {
|
||||
const music = await this.prisma.music.create({
|
||||
data: {
|
||||
...data,
|
||||
type: data.type.toUpperCase() as any,
|
||||
status: data.status.toUpperCase() as any,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
...music,
|
||||
type: music.type.toLowerCase() as MusicType,
|
||||
status: music.status.toLowerCase() as MusicStatus,
|
||||
dateAdded: music.dateAdded,
|
||||
dateCompleted: music.dateCompleted || undefined,
|
||||
createdAt: music.createdAt,
|
||||
updatedAt: music.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update music by ID.
|
||||
*/
|
||||
async updateMusic(id: string, data: UpdateMusicDto): Promise<Music> {
|
||||
const updateData = { ...data };
|
||||
if (updateData.type) {
|
||||
updateData.type = updateData.type.toUpperCase() as any;
|
||||
}
|
||||
if (updateData.status) {
|
||||
updateData.status = updateData.status.toUpperCase() as any;
|
||||
}
|
||||
|
||||
const music = await this.prisma.music.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
return {
|
||||
...music,
|
||||
type: music.type.toLowerCase() as MusicType,
|
||||
status: music.status.toLowerCase() as MusicStatus,
|
||||
dateAdded: music.dateAdded,
|
||||
dateCompleted: music.dateCompleted || undefined,
|
||||
createdAt: music.createdAt,
|
||||
updatedAt: music.updatedAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete music by ID.
|
||||
*/
|
||||
async deleteMusic(id: string): Promise<void> {
|
||||
await this.prisma.music.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user