import { Body, Controller, Get, Param, Post } from '@nestjs/common'; import { JobsService } from './jobs.service'; import { ApiBody, ApiConsumes, ApiParam, ApiProperty, ApiTags } from '@nestjs/swagger'; class ClaimCompleteDto { @ApiProperty({ description: 'Identity of the completer', example: 'my-identity' }) completer: string; @ApiProperty({ description: 'Identity of the item to complete', example: 'my-item' }) item: string; @ApiProperty({ description: 'Data to store with the completion', example: { foo: 'bar' } }) data: any; } @Controller('jobs') @ApiTags('jobs') export class JobsController { constructor( private readonly jobsService: JobsService, ) { } @Get(':jobName/stats') @ApiParam({ name: 'jobName', required: true }) async getStats(@Param('jobName') jobName: string) { return { todoCount: await this.jobsService.getTodoItemCount(jobName), claimedCount: await this.jobsService.getClaimedItemCount(jobName), doneCount: await this.jobsService.getDoneItemCount(jobName), }; } @Get(':jobName/todo') @ApiParam({ name: 'jobName', required: true }) async getTodoItems(@Param('jobName') jobName: string) { return this.jobsService.getTodoItems(jobName); } @Get(':jobName/leaderboard') @ApiParam({ name: 'jobName', required: true }) async getLeaderboard(@Param('jobName') jobName: string) { return this.jobsService.getLeaderboard(jobName); } @Post(':jobName/add') @ApiParam({ name: 'jobName', required: true }) @ApiConsumes('text/plain') @ApiBody({ type: String, description: "Items to add, one per line separated by newline characters" }) async addItemsToJob(@Param('jobName') jobName: string, @Body() items: string) { return this.jobsService.addItemsToJob(jobName, items.split('\n')); } @Post(':jobName/claim') @ApiParam({ name: 'jobName', required: true }) @ApiConsumes('text/plain') @ApiBody({ type: String, description: "Claimer identity string" }) async claimJobItem(@Param('jobName') jobName: string, @Body() claimer: string) { return this.jobsService.claimJobItem(jobName, claimer); } @Post(':jobName/complete') @ApiParam({ name: 'jobName', required: true }) async completeJobItem(@Param('jobName') jobName: string, @Body() body: ClaimCompleteDto) { return this.jobsService.completeJobItem(jobName, body.item, body.completer, body.data); } @Post(':jobName/reset-claimed') @ApiParam({ name: 'jobName', required: true }) async resetClaimed(@Param('jobName') jobName: string) { return this.jobsService.resetClaimedItems(jobName); } }