init: scaffold complet Sakuin — backend NestJS + frontend SvelteKit + CI/CD + deploy VPS
All checks were successful
CI/CD — Build & Deploy / Build & Deploy (push) Successful in 29s
All checks were successful
CI/CD — Build & Deploy / Build & Deploy (push) Successful in 29s
Backend: 5 modules (auth, user, work, list, health), AniList GraphQL proxy, SuperOAuth PKCE introspection, XP system, migrations TypeORM. Frontend: SvelteKit adapter-node, PWA manifest, dark theme, pages home/search/list/profile/callback. Infra: CI/CD Gitea vps-runner, Apache vhost SSL, pm2 sakuin-backend + sakuin-frontend, port 4002. License: BSL 1.1 (Apache 2.0 en 2028).
This commit is contained in:
137
backend/src/list/list.service.ts
Normal file
137
backend/src/list/list.service.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { UserWork, ListStatus } from './user-work.entity';
|
||||
import { WorkService } from '../work/work.service';
|
||||
import { UserService } from '../user/user.service';
|
||||
|
||||
const XP_ADD_WORK = 10;
|
||||
const XP_PROGRESS = 5;
|
||||
const XP_COMPLETE = 100;
|
||||
const XP_SCORE = 15;
|
||||
|
||||
@Injectable()
|
||||
export class ListService {
|
||||
constructor(
|
||||
@InjectRepository(UserWork)
|
||||
private readonly userWorkRepo: Repository<UserWork>,
|
||||
private readonly workService: WorkService,
|
||||
private readonly userService: UserService,
|
||||
) {}
|
||||
|
||||
async getUserList(userId: number, status?: ListStatus) {
|
||||
const where: any = { userId };
|
||||
if (status) where.status = status;
|
||||
|
||||
return this.userWorkRepo.find({
|
||||
where,
|
||||
relations: ['work'],
|
||||
order: { updatedAt: 'DESC' },
|
||||
});
|
||||
}
|
||||
|
||||
async addToList(
|
||||
userId: number,
|
||||
anilistId: number,
|
||||
status: ListStatus,
|
||||
): Promise<UserWork> {
|
||||
const work = await this.workService.findOrCreateFromAniList(anilistId);
|
||||
|
||||
const existing = await this.userWorkRepo.findOne({
|
||||
where: { userId, workId: work.id },
|
||||
});
|
||||
if (existing) {
|
||||
existing.status = status;
|
||||
return this.userWorkRepo.save(existing);
|
||||
}
|
||||
|
||||
const userWork = this.userWorkRepo.create({
|
||||
userId,
|
||||
workId: work.id,
|
||||
status,
|
||||
progress: 0,
|
||||
});
|
||||
const saved = await this.userWorkRepo.save(userWork);
|
||||
await this.userService.addXp(userId, XP_ADD_WORK);
|
||||
return saved;
|
||||
}
|
||||
|
||||
async updateProgress(
|
||||
userId: number,
|
||||
userWorkId: number,
|
||||
progress: number,
|
||||
): Promise<UserWork> {
|
||||
const uw = await this.userWorkRepo.findOne({
|
||||
where: { id: userWorkId, userId },
|
||||
relations: ['work'],
|
||||
});
|
||||
if (!uw) throw new NotFoundException('Entry not found');
|
||||
|
||||
const oldProgress = uw.progress;
|
||||
uw.progress = progress;
|
||||
|
||||
const total = uw.work.totalEpisodes || uw.work.totalChapters;
|
||||
if (total && progress >= total && uw.status !== ListStatus.COMPLETED) {
|
||||
uw.status = ListStatus.COMPLETED;
|
||||
uw.completedAt = new Date();
|
||||
await this.userService.addXp(userId, XP_COMPLETE);
|
||||
}
|
||||
|
||||
const saved = await this.userWorkRepo.save(uw);
|
||||
|
||||
if (progress > oldProgress) {
|
||||
const delta = progress - oldProgress;
|
||||
await this.userService.addXp(userId, delta * XP_PROGRESS);
|
||||
}
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
async updateStatus(
|
||||
userId: number,
|
||||
userWorkId: number,
|
||||
status: ListStatus,
|
||||
): Promise<UserWork> {
|
||||
const uw = await this.userWorkRepo.findOne({
|
||||
where: { id: userWorkId, userId },
|
||||
});
|
||||
if (!uw) throw new NotFoundException('Entry not found');
|
||||
|
||||
uw.status = status;
|
||||
if (status === ListStatus.COMPLETED) {
|
||||
uw.completedAt = new Date();
|
||||
await this.userService.addXp(userId, XP_COMPLETE);
|
||||
}
|
||||
|
||||
return this.userWorkRepo.save(uw);
|
||||
}
|
||||
|
||||
async setScore(
|
||||
userId: number,
|
||||
userWorkId: number,
|
||||
score: number,
|
||||
): Promise<UserWork> {
|
||||
const uw = await this.userWorkRepo.findOne({
|
||||
where: { id: userWorkId, userId },
|
||||
});
|
||||
if (!uw) throw new NotFoundException('Entry not found');
|
||||
|
||||
const hadScore = uw.score !== null;
|
||||
uw.score = score;
|
||||
const saved = await this.userWorkRepo.save(uw);
|
||||
|
||||
if (!hadScore) {
|
||||
await this.userService.addXp(userId, XP_SCORE);
|
||||
}
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
async removeFromList(userId: number, userWorkId: number): Promise<void> {
|
||||
const uw = await this.userWorkRepo.findOne({
|
||||
where: { id: userWorkId, userId },
|
||||
});
|
||||
if (!uw) throw new NotFoundException('Entry not found');
|
||||
await this.userWorkRepo.remove(uw);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user