File size: 2,099 Bytes
5b324f1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import path, { resolve } from "path";
import AppDataSource from "../dbConnecter";
import { Model } from "../entities/model";
import fs from "fs";
import { deleteFiles } from "../../utils/file-uploader";
import { getFileNameInPath } from "../../utils";
const modelRepository = AppDataSource.getRepository(Model);
export const createModel = async (name: string, fileUrl: string, fileName: string) => {
const model = new Model();
model.name = name;
model.fileUrl = fileUrl;
model.fileName = fileName;
await modelRepository.save(model);
};
export const deleteModel = async (id: string) => {
const model = await modelRepository.findOne({
where: { id },
});
if (model) {
try {
await modelRepository.remove(model);
await deleteFiles([`monopoly/models/${model.fileName}`]);
} catch (e: any) {
throw new Error(`删除Model失败:${e.message}`);
}
} else {
throw new Error("不存在的Model");
}
};
export const updateModel = async (id: string, name: string, fileUrl?: string, fileName?: string) => {
const model = await modelRepository.findOne({ where: { id } });
if (model) {
model.name = name;
if (fileUrl && fileName) {
deleteFiles([`monopoly/models/${getFileNameInPath(model.fileUrl)}`]);
model.fileUrl = fileUrl;
model.fileName = fileName;
}
await modelRepository.save(model);
} else {
throw new Error("不存在的Model");
}
};
export const getModelById = async (id: string) => {
const model = await modelRepository.findOne({
select: ["id", "name", "fileUrl"],
where: { id },
});
if (model) {
return model;
} else {
return null;
}
};
export const getModelList = async (page: number, size: number) => {
const total = await modelRepository.count();
if (page > 0) {
const modelList = await modelRepository.find({
skip: (page - 1) * size,
take: size,
order: { createTime: "DESC" },
});
return { modelList, total };
} else {
const modelList = await modelRepository.find({ order: { createTime: "DESC" } });
return { modelList, total };
}
// const total = Math.round((await modelRepository.count()) / size);
};
|