Newer
Older
import { PrismaClient } from '@prisma/client';
import * as fs from 'node:fs';
import path from 'node:path';
class HomeService {
const entitiesDB = await prisma.home_entity.findMany({
orderBy: [{ entity_order: 'asc' }]
});
const entitiesImages = [];
const entitiesToReturn = entitiesDB.map((entity) => {
if (!entity.image_width) return entity;
const imagePath = path.join(path.resolve(), `/public/images/${entity.entity_uuid}.jpg`);
const file = fs.readFileSync(imagePath);
console.log('file in readFile: ', file);
// const blob = new Blob([file], { type: 'image/jpeg' });
// console.log('blob: ', blob);
const buffer = Buffer.from(file, 'base64');
entitiesImages.push(buffer);
console.log('entitiesImages', entitiesImages);
return entity;
});
return {
entities: entitiesToReturn,
entitiesImages: entitiesImages
};
async getHomeBackgroundUrl() {
return prisma.setting.findFirst({
where: {
setting_name: 'homeBackgroundUrl'
}
});
}
if (body.image_blob) {
const imagePath = path.join(path.resolve(), `/public/images/image.jpg`);
let newImagePath = imagePath.split('\\');
newImagePath.splice(-1);
console.log('1 newImagePath', newImagePath);
newImagePath.push(`${body.entity_uuid}.jpg`);
console.log('2 newImagePath', newImagePath);
newImagePath = newImagePath.join('\\');
console.log('3 newImagePath', newImagePath);
fs.rename(imagePath, newImagePath, function (err) {
if (err) console.log('ERROR in fs.rename: ' + err);
});
delete body.image_blob;
body.image_path = newImagePath;
console.log('body image: ', body);
return prisma.home_entity.create({ data: { ...body } });
}
async createImageEntity(body) {
const imagePath = path.join(path.resolve(), `/public/images/image.jpg`);
fs.writeFileSync(imagePath, body);
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
}
async editEntity(body) {
return prisma.home_entity.update({
where: {
entity_uuid: body.entity_uuid
},
data: { ...body }
});
}
async deleteEntity(body) {
const deletedEntity = await prisma.home_entity.findFirst({
where: {
entity_uuid: body.entity_uuid
}
});
await prisma.home_entity.delete({
where: {
entity_uuid: body.entity_uuid
}
});
return deletedEntity;
}
async changeOrderEntity(body) {
const allEntities = await prisma.home_entity.findMany({
orderBy: [{ entity_order: 'asc' }]
});
const currentEntity = allEntities.find((entity) => entity.entity_uuid === body.entity_uuid);
const nextEntity =
body.direction === 'up'
? allEntities.reverse().find((entity) => entity.entity_order < currentEntity.entity_order)
: allEntities.find((entity) => entity.entity_order > currentEntity.entity_order);
await prisma.home_entity.update({
where: {
entity_uuid: currentEntity.entity_uuid
},
data: {
entity_order: nextEntity.entity_order
}
});
await prisma.home_entity.update({
where: {
entity_uuid: nextEntity.entity_uuid
},
data: {
entity_order: currentEntity.entity_order
}
});
return body;
}
async changeHomeBackgroundUrl(body) {
const homeBackgroundUrl = await prisma.setting.findFirst({
where: {
setting_name: 'homeBackgroundUrl'
}
});
if (homeBackgroundUrl) {
return prisma.setting.update({
where: {
setting_name: 'homeBackgroundUrl'
},
data: { ...body }
});
}
return prisma.setting.create({ data: { ...body } });
}