From f905b26e89e21b071d9213961306d3d1ecacc5dc Mon Sep 17 00:00:00 2001 From: Kahn Yuan Date: Sun, 8 Dec 2024 01:09:44 +0800 Subject: [PATCH] optimizm allocation. --- src/controller/allocation.controller.ts | 21 +++++++++++++++++---- src/controller/pool.controller.ts | 4 ++-- src/core/service/allocation.service.ts | 23 +++++++++++++++++++++-- src/core/type/doc/allocation.ts | 22 +++++++++------------- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/controller/allocation.controller.ts b/src/controller/allocation.controller.ts index 20fe034..88f31ae 100644 --- a/src/controller/allocation.controller.ts +++ b/src/controller/allocation.controller.ts @@ -1,9 +1,19 @@ import { CoreApiResponse } from '@core/api/coreApiResponse'; import { + AllocationListQuery, CreateAllocationBody, UpdateAllocationStatusBody, } from '@core/type/doc/allocation'; -import { Body, Controller, Inject, Param, Post, Put } from '@nestjs/common'; +import { + Body, + Controller, + Get, + Inject, + Param, + Post, + Put, + Query, +} from '@nestjs/common'; import { AllocationService } from '@service/allocation.service'; @Controller('allocation') @@ -13,12 +23,15 @@ export class AllocationController { private readonly allocationService: AllocationService, ) {} - // @Get('list') - // async getAllocations(@Query() query: AllocationListQuery) {} + @Get('list') + async getAllocations(@Query() query: AllocationListQuery) { + const result = await this.allocationService.getAllocationList(query); + return CoreApiResponse.success(result); + } @Post('create') async createAllocation(@Body() body: CreateAllocationBody) { - const result = this.allocationService.create(body); + const result = await this.allocationService.create(body); return CoreApiResponse.success(result); } diff --git a/src/controller/pool.controller.ts b/src/controller/pool.controller.ts index 2a2bd89..4a4f8f4 100644 --- a/src/controller/pool.controller.ts +++ b/src/controller/pool.controller.ts @@ -23,7 +23,7 @@ export class PoolController { @Post('create') async createPool(@Body() body: CreatePoolBody) { - const result = this.poolService.createPool(body); + const result = await this.poolService.createPool(body); return CoreApiResponse.success(result); } @@ -35,7 +35,7 @@ export class PoolController { @Post('claim') async claim(@Body() body: ClaimBody) { - const result = this.poolService.contributorClaim(body); + const result = await this.poolService.contributorClaim(body); return CoreApiResponse.success(result); } } diff --git a/src/core/service/allocation.service.ts b/src/core/service/allocation.service.ts index 7d7bb5a..5eb559a 100644 --- a/src/core/service/allocation.service.ts +++ b/src/core/service/allocation.service.ts @@ -13,7 +13,23 @@ import { PrismaService } from 'nestjs-prisma'; export class AllocationService { constructor(private prisma: PrismaService) {} - async getAllocationList(query: AllocationListQuery) {} + async getAllocationList(query: AllocationListQuery) { + const { projectId } = query; + const project = await this.prisma.project.findFirst({ + where: { + id: projectId, + }, + }); + if (!project) { + throw new HttpException( + Code.NOT_FOUND_ERROR.message, + Code.NOT_FOUND_ERROR.code, + ); + } + return this.prisma.allocation.findMany({ + where: { projectId, deleted: false }, + }); + } async create(body: CreateAllocationBody) { const { title, projectId, contributors, ratios, credits, operatorId } = @@ -66,8 +82,11 @@ export class AllocationService { }), ); }); + await this.prisma.$transaction(fns); - return this.prisma.$transaction(fns); + return { + id, + }; } async updateAllocationState( diff --git a/src/core/type/doc/allocation.ts b/src/core/type/doc/allocation.ts index 7c3444f..4a8ca50 100644 --- a/src/core/type/doc/allocation.ts +++ b/src/core/type/doc/allocation.ts @@ -1,12 +1,10 @@ import { AuthBody } from '@core/type/doc/auth'; import { PaginateQuery } from '@core/type/doc/common'; import { ApiProperty } from '@nestjs/swagger'; -import { Type } from 'class-transformer'; import { ArrayNotEmpty, IsArray, IsNotEmpty, - IsNumber, IsOptional, IsString, } from 'class-validator'; @@ -17,17 +15,15 @@ export class AllocationListQuery extends PaginateQuery { @ApiProperty({ type: 'string' }) projectId: string; - @IsOptional() - @IsNumber() - @Type(() => Number) - @ApiProperty({ type: 'number' }) - dateFrom?: number; - - @IsOptional() - @IsNumber() - @Type(() => Number) - @ApiProperty({ type: 'number' }) - dateTo?: number; + // @IsOptional() + // @Type(() => Number) + // @ApiProperty({ type: 'number' }) + // dateFrom?: number; + // + // @IsOptional() + // @Type(() => Number) + // @ApiProperty({ type: 'number' }) + // dateTo?: number; } export class UpdateAllocationStatusBody extends AuthBody {