110 lines
2.8 KiB
TypeScript
110 lines
2.8 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Body,
|
|
Param,
|
|
Query,
|
|
Req,
|
|
UseGuards,
|
|
ParseIntPipe,
|
|
} from '@nestjs/common';
|
|
import { ListService } from './list.service';
|
|
import { ListStatus } from './user-work.entity';
|
|
import { AuthGuard } from '../auth/auth.guard';
|
|
import { UserService } from '../user/user.service';
|
|
import { AddToListDto } from './dto/add-to-list.dto';
|
|
import { UpdateProgressDto } from './dto/update-progress.dto';
|
|
import { UpdateStatusDto } from './dto/update-status.dto';
|
|
import { SetScoreDto } from './dto/set-score.dto';
|
|
|
|
@Controller('api/list')
|
|
@UseGuards(AuthGuard)
|
|
export class ListController {
|
|
constructor(
|
|
private readonly listService: ListService,
|
|
private readonly userService: UserService,
|
|
) {}
|
|
|
|
@Get()
|
|
async getList(@Req() req: any, @Query('status') status?: ListStatus) {
|
|
const user = await this.userService.findOrCreate({
|
|
id: req.user.id,
|
|
nickname: req.user.nickname,
|
|
avatar: req.user.avatar,
|
|
});
|
|
return this.listService.getUserList(user.id, status);
|
|
}
|
|
|
|
@Post()
|
|
async addToList(
|
|
@Req() req: any,
|
|
@Body() body: AddToListDto,
|
|
) {
|
|
const user = await this.userService.findOrCreate({
|
|
id: req.user.id,
|
|
nickname: req.user.nickname,
|
|
avatar: req.user.avatar,
|
|
});
|
|
return this.listService.addToList(user.id, body.anilistId, body.status);
|
|
}
|
|
|
|
@Put(':id/progress')
|
|
async updateProgress(
|
|
@Req() req: any,
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Body() body: UpdateProgressDto,
|
|
) {
|
|
const user = await this.userService.findOrCreate({
|
|
id: req.user.id,
|
|
nickname: req.user.nickname,
|
|
avatar: req.user.avatar,
|
|
});
|
|
return this.listService.updateProgress(user.id, id, body.progress);
|
|
}
|
|
|
|
@Put(':id/status')
|
|
async updateStatus(
|
|
@Req() req: any,
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Body() body: UpdateStatusDto,
|
|
) {
|
|
const user = await this.userService.findOrCreate({
|
|
id: req.user.id,
|
|
nickname: req.user.nickname,
|
|
avatar: req.user.avatar,
|
|
});
|
|
return this.listService.updateStatus(user.id, id, body.status);
|
|
}
|
|
|
|
@Put(':id/score')
|
|
async setScore(
|
|
@Req() req: any,
|
|
@Param('id', ParseIntPipe) id: number,
|
|
@Body() body: SetScoreDto,
|
|
) {
|
|
const user = await this.userService.findOrCreate({
|
|
id: req.user.id,
|
|
nickname: req.user.nickname,
|
|
avatar: req.user.avatar,
|
|
});
|
|
return this.listService.setScore(user.id, id, body.score);
|
|
}
|
|
|
|
@Delete(':id')
|
|
async remove(
|
|
@Req() req: any,
|
|
@Param('id', ParseIntPipe) id: number,
|
|
) {
|
|
const user = await this.userService.findOrCreate({
|
|
id: req.user.id,
|
|
nickname: req.user.nickname,
|
|
avatar: req.user.avatar,
|
|
});
|
|
await this.listService.removeFromList(user.id, id);
|
|
return { deleted: true };
|
|
}
|
|
}
|