feat: 支持画廊图片更新
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { StudioConfig } from '@prisma/client'
|
||||
import { StudioConfig as PrismaStudioConfig } from '@prisma/client'
|
||||
import { PrismaService } from '../prisma/prisma.service'
|
||||
import { UpdateStudioDto } from './dto/update-studio.dto'
|
||||
|
||||
@@ -7,28 +7,71 @@ import { UpdateStudioDto } from './dto/update-studio.dto'
|
||||
export class StudioService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async getInfo(): Promise<StudioConfig> {
|
||||
async getInfo() {
|
||||
const existing = await this.prisma.studioConfig.findFirst()
|
||||
|
||||
if (existing) {
|
||||
return existing
|
||||
return this.normalizeStudioConfig(existing)
|
||||
}
|
||||
|
||||
return this.prisma.studioConfig.create({
|
||||
const created = await this.prisma.studioConfig.create({
|
||||
data: {
|
||||
name: '普拉提工作室',
|
||||
},
|
||||
})
|
||||
|
||||
return this.normalizeStudioConfig(created)
|
||||
}
|
||||
|
||||
async updateInfo(dto: UpdateStudioDto): Promise<StudioConfig> {
|
||||
const existing = await this.getInfo()
|
||||
|
||||
const updated = await this.prisma.studioConfig.update({
|
||||
where: { id: existing.id },
|
||||
data: { ...dto },
|
||||
async updateInfo(dto: UpdateStudioDto) {
|
||||
const existing = await this.prisma.studioConfig.findFirst({
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
return { ...updated }
|
||||
const data = {
|
||||
...dto,
|
||||
photos: dto.photos ? this.normalizePhotos(dto.photos) : undefined,
|
||||
}
|
||||
|
||||
const record = existing
|
||||
? await this.prisma.studioConfig.update({
|
||||
where: { id: existing.id },
|
||||
data,
|
||||
})
|
||||
: await this.prisma.studioConfig.create({
|
||||
data: { name: '普拉提工作室', ...data },
|
||||
})
|
||||
|
||||
return this.normalizeStudioConfig(record)
|
||||
}
|
||||
|
||||
private normalizeStudioConfig(config: PrismaStudioConfig) {
|
||||
return {
|
||||
...config,
|
||||
latitude: config.latitude == null ? null : Number(config.latitude),
|
||||
longitude: config.longitude == null ? null : Number(config.longitude),
|
||||
photos: this.normalizePhotos(config.photos),
|
||||
}
|
||||
}
|
||||
|
||||
private normalizePhotos(value: unknown): string[] {
|
||||
if (!Array.isArray(value)) {
|
||||
return []
|
||||
}
|
||||
|
||||
const deduped = new Set<string>()
|
||||
|
||||
value.forEach((item) => {
|
||||
if (typeof item !== 'string') {
|
||||
return
|
||||
}
|
||||
|
||||
const trimmed = item.trim()
|
||||
if (trimmed) {
|
||||
deduped.add(trimmed)
|
||||
}
|
||||
})
|
||||
|
||||
return [...deduped]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user