feat: 重构追溯码及上链出库流程
This commit is contained in:
@@ -1,65 +0,0 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsArray,
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsInt,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
MaxLength,
|
||||
ValidateNested,
|
||||
} from 'class-validator';
|
||||
import { NodeType } from '@artveris/shared';
|
||||
import { MediaItemDto } from '../../artwork/dto/artwork.dto';
|
||||
|
||||
export class CreateNodeDto {
|
||||
@ApiProperty({ description: '节点类型', enum: NodeType })
|
||||
@IsEnum(NodeType)
|
||||
nodeType!: NodeType;
|
||||
|
||||
@ApiProperty({ description: '节点标题' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@MaxLength(80)
|
||||
title!: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '发生时间' })
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
occurredAt?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '地点' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(120)
|
||||
location?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '说明' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MaxLength(2000)
|
||||
description?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '图片/视频', type: [MediaItemDto] })
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => MediaItemDto)
|
||||
mediaFiles?: MediaItemDto[];
|
||||
|
||||
@ApiPropertyOptional({ description: '排序' })
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
sortOrder?: number;
|
||||
}
|
||||
|
||||
export class UpdateNodeDto extends CreateNodeDto {}
|
||||
|
||||
export class ReorderNodeDto {
|
||||
@ApiProperty({ description: '按目标顺序排列的节点 ID 列表', type: [Number] })
|
||||
@IsArray()
|
||||
@IsInt({ each: true })
|
||||
ids!: number[];
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put } from '@nestjs/common';
|
||||
import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { RoleType } from '@artveris/shared';
|
||||
import { TraceNodeService } from './trace-node.service';
|
||||
import { CreateNodeDto, ReorderNodeDto, UpdateNodeDto } from './dto/trace-node.dto';
|
||||
import { Roles } from '../common/decorators/roles.decorator';
|
||||
|
||||
@ApiTags('溯源节点')
|
||||
@ApiBearerAuth()
|
||||
@@ -16,32 +13,4 @@ export class TraceNodeController {
|
||||
list(@Param('artworkId', ParseIntPipe) artworkId: number) {
|
||||
return this.nodeService.listByArtwork(artworkId);
|
||||
}
|
||||
|
||||
@Post('artworks/:artworkId/nodes')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '新增溯源节点' })
|
||||
create(@Param('artworkId', ParseIntPipe) artworkId: number, @Body() dto: CreateNodeDto) {
|
||||
return this.nodeService.create(artworkId, dto);
|
||||
}
|
||||
|
||||
@Put('artworks/:artworkId/nodes/reorder')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '调整节点顺序' })
|
||||
reorder(@Param('artworkId', ParseIntPipe) artworkId: number, @Body() dto: ReorderNodeDto) {
|
||||
return this.nodeService.reorder(artworkId, dto);
|
||||
}
|
||||
|
||||
@Put('nodes/:id')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '更新溯源节点' })
|
||||
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateNodeDto) {
|
||||
return this.nodeService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete('nodes/:id')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '删除溯源节点' })
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.nodeService.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,5 @@ import { TraceNodeController } from './trace-node.controller';
|
||||
@Module({
|
||||
controllers: [TraceNodeController],
|
||||
providers: [TraceNodeService],
|
||||
exports: [TraceNodeService],
|
||||
})
|
||||
export class TraceNodeModule {}
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { ErrorCode } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { CreateNodeDto, ReorderNodeDto, UpdateNodeDto } from './dto/trace-node.dto';
|
||||
|
||||
@Injectable()
|
||||
export class TraceNodeService {
|
||||
@@ -20,11 +16,6 @@ export class TraceNodeService {
|
||||
.map((i) => ({ ...i, url: this.storage.getUrl(i.key) }));
|
||||
}
|
||||
|
||||
private async ensureArtwork(artworkId: number) {
|
||||
const a = await this.prisma.artwork.findFirst({ where: { id: artworkId, deletedAt: null } });
|
||||
if (!a) throw new BusinessException(ErrorCode.ARTWORK_NOT_FOUND, '艺术品不存在', 404);
|
||||
}
|
||||
|
||||
async listByArtwork(artworkId: number) {
|
||||
const nodes = await this.prisma.traceNode.findMany({
|
||||
where: { artworkId },
|
||||
@@ -32,64 +23,4 @@ export class TraceNodeService {
|
||||
});
|
||||
return nodes.map((n) => ({ ...n, mediaFiles: this.mapMedia(n.mediaFiles) }));
|
||||
}
|
||||
|
||||
async create(artworkId: number, dto: CreateNodeDto) {
|
||||
await this.ensureArtwork(artworkId);
|
||||
const count = await this.prisma.traceNode.count({ where: { artworkId } });
|
||||
const node = await this.prisma.traceNode.create({
|
||||
data: {
|
||||
artworkId,
|
||||
nodeType: dto.nodeType,
|
||||
title: dto.title,
|
||||
occurredAt: dto.occurredAt ? new Date(dto.occurredAt) : null,
|
||||
location: dto.location,
|
||||
description: dto.description,
|
||||
mediaFiles: (dto.mediaFiles ?? []) as unknown as Prisma.InputJsonValue,
|
||||
sortOrder: dto.sortOrder ?? count,
|
||||
},
|
||||
});
|
||||
return { ...node, mediaFiles: this.mapMedia(node.mediaFiles) };
|
||||
}
|
||||
|
||||
async update(id: number, dto: UpdateNodeDto) {
|
||||
await this.ensureNode(id);
|
||||
const node = await this.prisma.traceNode.update({
|
||||
where: { id },
|
||||
data: {
|
||||
nodeType: dto.nodeType,
|
||||
title: dto.title,
|
||||
occurredAt: dto.occurredAt ? new Date(dto.occurredAt) : null,
|
||||
location: dto.location,
|
||||
description: dto.description,
|
||||
mediaFiles: (dto.mediaFiles ?? []) as unknown as Prisma.InputJsonValue,
|
||||
sortOrder: dto.sortOrder,
|
||||
},
|
||||
});
|
||||
return { ...node, mediaFiles: this.mapMedia(node.mediaFiles) };
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
await this.ensureNode(id);
|
||||
await this.prisma.traceNode.delete({ where: { id } });
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
async reorder(artworkId: number, dto: ReorderNodeDto) {
|
||||
await this.ensureArtwork(artworkId);
|
||||
await this.prisma.$transaction(
|
||||
dto.ids.map((id, index) =>
|
||||
this.prisma.traceNode.updateMany({
|
||||
where: { id, artworkId },
|
||||
data: { sortOrder: index },
|
||||
}),
|
||||
),
|
||||
);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
private async ensureNode(id: number) {
|
||||
const n = await this.prisma.traceNode.findUnique({ where: { id } });
|
||||
if (!n) throw new BusinessException(ErrorCode.NOT_FOUND, '节点不存在', 404);
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user