generated from nhcarrigan/template
208c11d153
### Explanation _No response_ ### Issue _No response_ ### Attestations - [ ] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [ ] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [ ] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [ ] I have pinned the dependencies to a specific patch version. ### Style - [ ] I have run the linter and resolved any errors. - [ ] My pull request uses an appropriate title, matching the conventional commit standards. - [ ] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Reviewed-on: #64
211 lines
6.1 KiB
TypeScript
211 lines
6.1 KiB
TypeScript
/**
|
|
* @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";
|
|
import {
|
|
validateUrl,
|
|
validateRating,
|
|
validateStringLength,
|
|
validateDataUrl,
|
|
MAX_LENGTHS,
|
|
} from "../utils/validation";
|
|
|
|
export class MusicService {
|
|
private prisma = prisma;
|
|
|
|
constructor() {}
|
|
|
|
/**
|
|
* Validate music data for security.
|
|
*/
|
|
private validateMusicData(data: CreateMusicDto | UpdateMusicDto): void {
|
|
// Validate string lengths
|
|
if (!validateStringLength(data.title, MAX_LENGTHS.TITLE)) {
|
|
throw new Error(`Title must be ${MAX_LENGTHS.TITLE} characters or less.`);
|
|
}
|
|
if (!validateStringLength(data.artist, MAX_LENGTHS.AUTHOR)) {
|
|
throw new Error(`Artist must be ${MAX_LENGTHS.AUTHOR} characters or less.`);
|
|
}
|
|
if (!validateStringLength(data.notes, MAX_LENGTHS.NOTES)) {
|
|
throw new Error(`Notes must be ${MAX_LENGTHS.NOTES} characters or less.`);
|
|
}
|
|
if (!validateStringLength(data.coverArt, MAX_LENGTHS.URL)) {
|
|
throw new Error(`Cover art URL must be ${MAX_LENGTHS.URL} characters or less.`);
|
|
}
|
|
|
|
// Validate rating
|
|
if (data.rating !== undefined && !validateRating(data.rating)) {
|
|
throw new Error("Rating must be an integer between 0 and 10.");
|
|
}
|
|
|
|
// Validate cover art URL
|
|
if (data.coverArt) {
|
|
if (data.coverArt.startsWith("data:")) {
|
|
const sizeInBytes = data.coverArt.length * 0.75;
|
|
if (sizeInBytes > MAX_LENGTHS.DATA_URL) {
|
|
throw new Error("Cover image must be under 5MB.");
|
|
}
|
|
if (!validateDataUrl(data.coverArt)) {
|
|
throw new Error("Invalid image data URL.");
|
|
}
|
|
} else {
|
|
if (!validateStringLength(data.coverArt, MAX_LENGTHS.URL)) {
|
|
throw new Error(`Cover image URL must be ${MAX_LENGTHS.URL} characters or less.`);
|
|
}
|
|
if (!validateUrl(data.coverArt)) {
|
|
throw new Error("Invalid cover image URL. Only http and https URLs are allowed.");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Validate tags
|
|
if (data.tags) {
|
|
for (const tag of data.tags) {
|
|
if (!validateStringLength(tag, MAX_LENGTHS.TAGS)) {
|
|
throw new Error(`Each tag must be ${MAX_LENGTHS.TAGS} characters or less.`);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Validate link URLs
|
|
if (data.links) {
|
|
for (const link of data.links) {
|
|
if (!validateUrl(link.url)) {
|
|
throw new Error(`Invalid link URL: ${link.title}. Only http and https URLs are allowed.`);
|
|
}
|
|
if (!validateStringLength(link.title, MAX_LENGTHS.TITLE)) {
|
|
throw new Error(`Link title must be ${MAX_LENGTHS.TITLE} characters or less.`);
|
|
}
|
|
if (!validateStringLength(link.url, MAX_LENGTHS.URL)) {
|
|
throw new Error(`Link URL must be ${MAX_LENGTHS.URL} characters or less.`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 as unknown as MusicType,
|
|
status: music.status as unknown as MusicStatus,
|
|
dateAdded: music.dateAdded,
|
|
dateStarted: music.dateStarted || undefined,
|
|
dateCompleted: music.dateCompleted || undefined,
|
|
dateFinished: music.dateFinished || undefined,
|
|
tags: music.tags ?? [],
|
|
links: music.links ?? [],
|
|
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 as unknown as MusicType,
|
|
status: music.status as unknown as MusicStatus,
|
|
dateAdded: music.dateAdded,
|
|
dateStarted: music.dateStarted || undefined,
|
|
dateCompleted: music.dateCompleted || undefined,
|
|
dateFinished: music.dateFinished || undefined,
|
|
tags: music.tags ?? [],
|
|
links: music.links ?? [],
|
|
createdAt: music.createdAt,
|
|
updatedAt: music.updatedAt,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create new music.
|
|
*/
|
|
async createMusic(data: CreateMusicDto): Promise<Music> {
|
|
// Validate input
|
|
this.validateMusicData(data);
|
|
|
|
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 as unknown as MusicType,
|
|
status: music.status as unknown as MusicStatus,
|
|
dateAdded: music.dateAdded,
|
|
dateStarted: music.dateStarted || undefined,
|
|
dateCompleted: music.dateCompleted || undefined,
|
|
dateFinished: music.dateFinished || undefined,
|
|
tags: music.tags ?? [],
|
|
links: music.links ?? [],
|
|
createdAt: music.createdAt,
|
|
updatedAt: music.updatedAt,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Update music by ID.
|
|
*/
|
|
async updateMusic(id: string, data: UpdateMusicDto): Promise<Music> {
|
|
// Validate input
|
|
this.validateMusicData(data);
|
|
|
|
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 as unknown as MusicType,
|
|
status: music.status as unknown as MusicStatus,
|
|
dateAdded: music.dateAdded,
|
|
dateStarted: music.dateStarted || undefined,
|
|
dateCompleted: music.dateCompleted || undefined,
|
|
dateFinished: music.dateFinished || undefined,
|
|
tags: music.tags ?? [],
|
|
links: music.links ?? [],
|
|
createdAt: music.createdAt,
|
|
updatedAt: music.updatedAt,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Delete music by ID.
|
|
*/
|
|
async deleteMusic(id: string): Promise<void> {
|
|
await this.prisma.music.delete({
|
|
where: { id },
|
|
});
|
|
}
|
|
} |