stand2026_10_06
This commit is contained in:
1 parent
73015386e9
commit
30292794ae
25 files changed
+664
-164
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { Controller, Get, Param } from '@nestjs/common';
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
|
||||
import { ComponentsService } from './components.service';
|
||||
import { ComponentCreateDto, ComponentUpdateDto } from 'src/_dto/anlagen/components';
|
||||
|
||||
@Controller('rk2/components')
|
||||
export class ComponentsController {
|
||||
@@ -10,4 +11,25 @@ export class ComponentsController {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Post(':compId')
|
||||
create(@Param('compId') compId: string, @Body() dto: ComponentCreateDto) {
|
||||
console.log(dto)
|
||||
return this.service.create(compId, dto);
|
||||
}
|
||||
|
||||
@Patch(':compId')
|
||||
update(@Param('compId') compId: string, @Body() dto: ComponentUpdateDto) {
|
||||
console.log(dto)
|
||||
return this.service.update(compId, dto);
|
||||
}
|
||||
|
||||
@Delete(':compId')
|
||||
remove(@Param('compId') compId: string) {
|
||||
return this.service.delete(compId);
|
||||
}
|
||||
|
||||
@Delete(':compId/deleteProp/:propName')
|
||||
deleteProp(@Param('compId') compId: string,@Param('propName') propName: string) {
|
||||
return this.service.deletePropertie(compId,propName);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,18 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { forwardRef, Module } from '@nestjs/common';
|
||||
import { ComponentsService } from './components.service';
|
||||
import { ComponentsController } from './components.controller';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Unit, UnitSchema } from 'src/_schemas/anlagen/units';
|
||||
import { SignalsModule } from '../signals/signals.module';
|
||||
|
||||
@Module({
|
||||
controllers: [ComponentsController],
|
||||
providers: [ComponentsService],
|
||||
imports: [MongooseModule.forFeature([{ name: Unit.name, schema: UnitSchema }], "anlagen")],
|
||||
|
||||
imports: [
|
||||
MongooseModule.forFeature([{ name: Unit.name, schema: UnitSchema }], "anlagen"),
|
||||
forwardRef(() => SignalsModule)
|
||||
],
|
||||
exports: [ComponentsService]
|
||||
})
|
||||
export class ComponentsModule { }
|
||||
|
||||
@@ -1,23 +1,28 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { ComponentOfUnitDto, ComponentResponseDto, ComponentTypes } from 'src/_dto/anlagen/components';
|
||||
import { UnitResponseDto } from 'src/_dto/anlagen/units';
|
||||
import { ComponentCreateDto, ComponentOfUnitDto, ComponentResponseDto, ComponentTypes, ComponentUpdateDto } from 'src/_dto/anlagen/components';
|
||||
import { Component } from 'src/_schemas/anlagen/component';
|
||||
import { Unit } from 'src/_schemas/anlagen/units';
|
||||
import { SignalsService } from '../signals/signals.service';
|
||||
|
||||
@Injectable()
|
||||
export class ComponentsService {
|
||||
constructor(@InjectModel(Unit.name, "anlagen") private model: Model<Unit>) { }
|
||||
|
||||
constructor(
|
||||
@InjectModel(Unit.name, "anlagen") private model: Model<Unit>,
|
||||
@Inject(forwardRef(() => SignalsService))
|
||||
private readonly signalsService: SignalsService,
|
||||
) { }
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Find BY component id
|
||||
//-------------------------------------------------------------------------
|
||||
async findOne(id: string): Promise<ComponentOfUnitDto> {
|
||||
if(!Types.ObjectId.isValid(id)) throw new NotFoundException('id ist kein ObjectId')
|
||||
|
||||
|
||||
if (!Types.ObjectId.isValid(id)) throw new NotFoundException('id ist kein ObjectId')
|
||||
|
||||
|
||||
const item = await this.model.findOne({ 'components._id': id }).exec();
|
||||
|
||||
|
||||
|
||||
if (!item) throw new NotFoundException(`Keine Unit mit der Componenten Id: ${id} gefunden`)
|
||||
let comp = item.components.find((c) => c._id?.toString() === id);
|
||||
@@ -28,6 +33,119 @@ export class ComponentsService {
|
||||
};
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Create Component
|
||||
//-------------------------------------------------------------------------
|
||||
async create(unitId: string, dto: ComponentCreateDto): Promise<ComponentResponseDto> {
|
||||
if (!Types.ObjectId.isValid(unitId)) throw new NotFoundException('id ist kein ObjectId')
|
||||
|
||||
const componentId = new Types.ObjectId();
|
||||
|
||||
const create = {
|
||||
_id: componentId,
|
||||
...dto,
|
||||
name: dto.name ? dto.name : "unbenannt",
|
||||
type: dto.type ? dto.type : ComponentTypes.UNKNOWN
|
||||
}
|
||||
|
||||
const item = await this.model.findByIdAndUpdate(unitId,
|
||||
{ $push: { components: create } },
|
||||
{ returnDocument: 'after' }).exec();
|
||||
|
||||
if (!item) throw new NotFoundException(`After Push component: Keine Unit mit der Componenten Id: ${unitId} gefunden`)
|
||||
|
||||
const newComp = await this.findOne(componentId.toString())
|
||||
if (!newComp || !newComp.component) throw new NotFoundException(`Componente wurde nicht erzeugt UnitId: ${unitId}`)
|
||||
|
||||
|
||||
|
||||
return newComp.component
|
||||
|
||||
}
|
||||
|
||||
async delete(compId: string): Promise<any> {
|
||||
if (!Types.ObjectId.isValid(compId)) throw new NotFoundException('id ist kein ObjectId')
|
||||
|
||||
|
||||
const item = await this.model.findOneAndUpdate({ 'components._id': compId },
|
||||
{
|
||||
$pull: {
|
||||
components: { _id: compId }
|
||||
}
|
||||
},
|
||||
{ returnDocument: 'after' }).exec();
|
||||
|
||||
|
||||
if (!item) throw new NotFoundException(`Keine Unit mit der Componenten Id: ${compId} gefunden`)
|
||||
let delSigResult = await this.signalsService.removeSignalsOfComponent(compId);
|
||||
console.log(item._id,delSigResult)
|
||||
return item
|
||||
|
||||
}
|
||||
|
||||
async update(compId: string, dto: ComponentUpdateDto): Promise<any> {
|
||||
if (!Types.ObjectId.isValid(compId)) throw new NotFoundException('id ist kein ObjectId')
|
||||
|
||||
|
||||
const update: Record<string, any> = {};
|
||||
if (dto.name !== undefined) { update['components.$.name'] = dto.name };
|
||||
if (dto.postfix !== undefined) { update['components.$.postfix'] = dto.postfix };
|
||||
if (dto.prefix !== undefined) { update['components.$.prefix'] = dto.prefix };
|
||||
if (dto.index !== undefined) { update['components.$.index'] = dto.index };
|
||||
|
||||
if (dto.properties) {
|
||||
for (const [key, value] of Object.entries(dto.properties)) {
|
||||
update[`components.$.properties.${key}`] = value;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(update);
|
||||
const item = await this.model.findOneAndUpdate({ 'components._id': compId },
|
||||
{ $set: update },
|
||||
{ returnDocument: 'after' }).exec();
|
||||
|
||||
|
||||
|
||||
|
||||
return item
|
||||
|
||||
}
|
||||
|
||||
async deletePropertie(compId: string, propertieName: string): Promise<any> {
|
||||
if (!Types.ObjectId.isValid(compId)) throw new NotFoundException('id ist kein ObjectId')
|
||||
|
||||
const deleteProp: Record<string, any> = {};
|
||||
deleteProp[`components.$.properties.${propertieName}`] = '';
|
||||
|
||||
|
||||
const item = await this.model.findOneAndUpdate(
|
||||
{ 'components._id': compId },
|
||||
{
|
||||
$unset: deleteProp
|
||||
},
|
||||
{ returnDocument: 'after' }
|
||||
).exec();;
|
||||
|
||||
|
||||
return item
|
||||
|
||||
}
|
||||
|
||||
async createSignalRef(compId: string, signalId: string): Promise<ComponentOfUnitDto> {
|
||||
if (!Types.ObjectId.isValid(compId)) throw new NotFoundException('Create Component id ist kein ObjectId')
|
||||
|
||||
|
||||
const item = await this.model.findOneAndUpdate({ 'components._id': compId },
|
||||
{ $addToSet: { 'components.$.signalIds': new Types.ObjectId(signalId) } },
|
||||
{ returnDocument: 'after' }).exec();
|
||||
|
||||
|
||||
if (!item) throw new NotFoundException(`Keine Unit mit der Componenten Id: ${compId} gefunden`)
|
||||
let retValue = this.findOne(compId)
|
||||
|
||||
|
||||
return retValue;
|
||||
}
|
||||
|
||||
|
||||
toDto(component: Component): ComponentResponseDto {
|
||||
@@ -38,7 +156,6 @@ export class ComponentsService {
|
||||
id: component._id!.toString(),
|
||||
name: component.name,
|
||||
type: this.getComponentType(component.type.toString()),
|
||||
signalIds: component.signalIds.map((signal) => signal.toString()),
|
||||
properties: component.properties
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user