2026_08_31
This commit is contained in:
commit
73015386e9
2320 files changed
+80291
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
|
||||
import { SignalsService } from './signals.service';
|
||||
import { SignalCreateDto, SignalUpdateDto } from 'src/_dto/anlagen/signals';
|
||||
|
||||
@Controller('rk2/signals')
|
||||
export class SignalsController {
|
||||
constructor(private readonly service: SignalsService) { }
|
||||
|
||||
@Get()
|
||||
getAll() {
|
||||
return this.service.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
getOne(@Param('id') id: string) {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Get("byComponent/:id")
|
||||
getByComponent(@Param('id') id: string) {
|
||||
return this.service.findByComponent(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: SignalCreateDto) {
|
||||
console.log(dto)
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: SignalUpdateDto) {
|
||||
return this.service.update(id, dto);
|
||||
}
|
||||
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.service.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SignalsService } from './signals.service';
|
||||
import { SignalsController } from './signals.controller';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Signal, SignalSchema } from 'src/_schemas/anlagen/singnals';
|
||||
import { AnlageModule } from 'src/rk-anlagen/anlage/anlage.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([{ name: Signal.name, schema: SignalSchema }], "anlagen"),
|
||||
AnlageModule
|
||||
],
|
||||
providers: [SignalsService],
|
||||
controllers: [SignalsController]
|
||||
})
|
||||
export class SignalsModule { }
|
||||
@@ -0,0 +1,97 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { SignalCreateDto, SignalResponseDto, SignalUpdateDto } from 'src/_dto/anlagen/signals';
|
||||
import { Signal, SignalDocument } from 'src/_schemas/anlagen/singnals';
|
||||
import { AnlageService } from 'src/rk-anlagen/anlage/anlage.service';
|
||||
|
||||
@Injectable()
|
||||
export class SignalsService {
|
||||
constructor(
|
||||
@InjectModel(Signal.name, "anlagen") private model: Model<Signal>,
|
||||
private readonly anlageService: AnlageService
|
||||
) { }
|
||||
|
||||
async findAll(): Promise<SignalResponseDto[]> {
|
||||
const responsData = await this.model.find().exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<SignalResponseDto> {
|
||||
const item = await this.model.findById(id).exec();
|
||||
if (!item) throw new NotFoundException(`Signal ${id} nicht gefunden`)
|
||||
return this.toDto(item);
|
||||
}
|
||||
|
||||
async findByComponent(componentId:string): Promise<SignalResponseDto[]> {
|
||||
const responsData = await this.model.find({componentId:new Types.ObjectId(componentId)}).exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
|
||||
async create(dto: SignalCreateDto): Promise<SignalResponseDto> {
|
||||
let currentAnlage = await this.anlageService.getSelected();
|
||||
|
||||
if(!currentAnlage) throw new NotFoundException('Es ist keine Anlage ausgewählt');
|
||||
|
||||
|
||||
const newItem = new this.model(this.toDocument(dto,currentAnlage.id));
|
||||
|
||||
const result = await newItem.save();
|
||||
return this.toDto(result);
|
||||
}
|
||||
|
||||
|
||||
async update(id: string, dto: SignalUpdateDto) {
|
||||
let currentAnlage = await this.anlageService.getSelected();
|
||||
let anlage = dto.anlageId ? dto.anlageId : currentAnlage.id;
|
||||
if(!anlage) throw new NotFoundException('Anlage Id muss gesetzt sein')
|
||||
|
||||
let update = this.toDocument(dto,anlage);
|
||||
console.log(update);
|
||||
const changedItem = await this.model.findByIdAndUpdate(id, {
|
||||
$set: {
|
||||
...update
|
||||
|
||||
}
|
||||
}, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!changedItem) throw new NotFoundException(`Signal ${id} nicht gefunden`);
|
||||
|
||||
return this.toDto(changedItem);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const removedItem = await this.model.findByIdAndDelete(id, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!removedItem) throw new NotFoundException(`Signal ${id} nicht gefunden`);
|
||||
return this.toDto(removedItem);
|
||||
}
|
||||
|
||||
|
||||
|
||||
toDto(dbData: SignalDocument): SignalResponseDto {
|
||||
return {
|
||||
id: dbData._id.toString(),
|
||||
anlageId: dbData.anlageId.toString(),
|
||||
name: dbData.name,
|
||||
type: dbData.type,
|
||||
description: dbData.description,
|
||||
ioId: dbData.ioId?.toString(),
|
||||
componentId: dbData.componentId?.toString(),
|
||||
}
|
||||
}
|
||||
|
||||
toDocument(dto: SignalCreateDto | SignalUpdateDto,anlage:string) {
|
||||
return {
|
||||
anlageId: new Types.ObjectId(anlage),
|
||||
name: dto.name,
|
||||
type: dto.type,
|
||||
description: dto.description,
|
||||
ioId: dto.ioId ? new Types.ObjectId(dto.ioId) : null,
|
||||
componentId: dto.componentId ? new Types.ObjectId(dto.componentId) : null
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user