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 { GroupsService } from './groups.service';
|
||||
import { GroupCreateDto, GroupUpdateDto } from 'src/_dto/anlagen/groups';
|
||||
|
||||
@Controller('rk2/groups')
|
||||
export class GroupsController {
|
||||
constructor(private readonly service: GroupsService) { }
|
||||
|
||||
@Get()
|
||||
getAll() {
|
||||
return this.service.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
getOne(@Param('id') id: string) {
|
||||
return this.service.findOne(id);
|
||||
}
|
||||
|
||||
@Get('childs/:id')
|
||||
getChilds(@Param('id') id: string) {
|
||||
return this.service.findChilds(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: GroupCreateDto) {
|
||||
console.log(dto)
|
||||
return this.service.create(dto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: GroupUpdateDto) {
|
||||
return this.service.update(id, dto);
|
||||
}
|
||||
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.service.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GroupsController } from './groups.controller';
|
||||
import { GroupsService } from './groups.service';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Group, GroupSchema } from 'src/_schemas/anlagen/groups';
|
||||
|
||||
@Module({
|
||||
imports: [MongooseModule.forFeature([{ name: Group.name, schema: GroupSchema }],"anlagen")],
|
||||
controllers: [GroupsController],
|
||||
providers: [GroupsService]
|
||||
})
|
||||
export class GroupsModule { }
|
||||
@@ -0,0 +1,79 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { GroupCreateDto, GroupResponseDto, GroupUpdateDto } from 'src/_dto/anlagen/groups';
|
||||
import { Group, GroupDocument } from 'src/_schemas/anlagen/groups';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class GroupsService {
|
||||
constructor(@InjectModel(Group.name,"anlagen") private model: Model<Group>) { }
|
||||
|
||||
async findAll(): Promise<GroupResponseDto[]> {
|
||||
const responsData = await this.model.find().exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const item = await this.model.findById(id).exec();
|
||||
if (!item) throw new NotFoundException(`Group ${id} nicht gefunden`)
|
||||
return this.toDto(item);
|
||||
}
|
||||
|
||||
async findChilds(id:string): Promise<GroupResponseDto[]> {
|
||||
const responsData = await this.model.find({parentId:new Types.ObjectId(id)}).exec();
|
||||
|
||||
return responsData.map((item) => this.toDto(item));
|
||||
}
|
||||
|
||||
async create(dto: GroupCreateDto):Promise<GroupResponseDto> {
|
||||
const newItem = new this.model(this.toDocument(dto));
|
||||
const result = await newItem.save();
|
||||
return this.toDto(result);
|
||||
}
|
||||
|
||||
|
||||
async update(id: string, dto: GroupUpdateDto) {
|
||||
let update = this.toDocument(dto);
|
||||
console.log(update);
|
||||
const changedItem = await this.model.findByIdAndUpdate(id, {
|
||||
$set: {
|
||||
...update
|
||||
|
||||
}
|
||||
}, { returnDocument: 'after', runValidators: true });
|
||||
|
||||
if (!changedItem) throw new NotFoundException(`Group ${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(`Group ${id} nicht gefunden`);
|
||||
return this.toDto(removedItem);
|
||||
}
|
||||
|
||||
|
||||
|
||||
toDto(dbData: GroupDocument): GroupResponseDto {
|
||||
return {
|
||||
id: dbData._id.toString(),
|
||||
name: dbData.name,
|
||||
container: dbData.container,
|
||||
nr: dbData.nr,
|
||||
parentId: dbData.parentId?.toString()
|
||||
}
|
||||
}
|
||||
|
||||
toDocument(dto: GroupCreateDto|GroupUpdateDto) {
|
||||
return {
|
||||
name: dto.name,
|
||||
container: dto.container,
|
||||
nr: dto.nr,
|
||||
parentId: dto.parentId ? new Types.ObjectId(dto.parentId) : null
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user