@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.
95 lines (94 loc) • 2.87 kB
JavaScript
import { Tag } from "../../entities/tag.entity.js";
import { PrinterTag } from "../../entities/printer-tag.entity.js";
import { NotFoundException } from "../../exceptions/runtime.exceptions.js";
import { BaseService } from "./base.service.js";
import { PrinterTagDto } from "../interfaces/printer-tag.dto.js";
import { validate } from "class-validator";
//#region src/services/orm/printer-tag.service.ts
var PrinterTagService = class extends BaseService(PrinterTag, PrinterTagDto) {
tagRepository;
constructor(typeormService) {
super(typeormService);
this.tagRepository = typeormService.getDataSource().getRepository(Tag);
}
async listTags() {
const tags = await this.tagRepository.find();
const tagRecords = {};
for (const tag of tags) tagRecords[tag.id] = {
id: tag.id,
name: tag.name,
color: tag.color,
printers: []
};
for (const tag of tags) tagRecords[tag.id].printers = await this.repository.findBy({ tagId: tag.id });
return Object.values(tagRecords);
}
async getTag(tagId) {
const tag = await this.tagRepository.findOneBy({ id: tagId });
if (!tag) throw new NotFoundException("Tag does not exist");
return tag;
}
async getPrintersByTag(tagId) {
const tag = await this.getTag(tagId);
const printerTags = await this.repository.findBy({ tagId: tag.id });
return {
id: tag.id,
name: tag.name,
color: tag.color,
printers: printerTags
};
}
async createTag(dto) {
await validate(dto);
const entity = this.tagRepository.create(dto);
await validate(entity);
const tag = await this.tagRepository.save(entity);
return await this.getPrintersByTag(tag.id);
}
async updateTagName(tagId, name) {
const entity = await this.getTag(tagId);
const updateDto = { name };
await validate(updateDto);
await validate(Object.assign(entity, updateDto));
await this.tagRepository.update(entity.id, updateDto);
}
async updateTagColor(tagId, color) {
const entity = await this.getTag(tagId);
const updateDto = { color };
await validate(updateDto);
await validate(Object.assign(entity, updateDto));
await this.tagRepository.update(entity.id, updateDto);
}
async deleteTag(tagId) {
const tag = await this.getTag(tagId);
await this.tagRepository.delete({ id: tag.id });
}
async addPrinterToTag(tagId, printerId) {
const tag = await this.getTag(tagId);
const alreadyExisting = await this.repository.findOneBy({
tagId: tag.id,
printerId
});
if (alreadyExisting) return alreadyExisting;
return await this.create({
tagId: tag.id,
printerId
});
}
async removePrinterFromTag(tagId, printerId) {
await this.getTag(tagId);
await this.repository.delete({
tagId,
printerId
});
}
toDto(entity) {
return {
printerId: entity.printerId,
tagId: entity.tagId
};
}
};
//#endregion
export { PrinterTagService };
//# sourceMappingURL=printer-tag.service.js.map