Initial commit: NestJS NextJS pnpm monorepo
This commit is contained in:
commit
610dcf202b
175 files changed
+25172
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { ArticleVariantService } from './article-variant.service';
|
||||
import { ArticleVariantCreateDto, ArticleVariantDto, ArticleVariantUpdateDto } from '../_dto/article-variant.dto';
|
||||
|
||||
@Controller('article-variant')
|
||||
export class ArticleVariantController {
|
||||
constructor(
|
||||
private readonly articleVariantService: ArticleVariantService,
|
||||
) { }
|
||||
|
||||
@Get()
|
||||
findAll():Promise<ArticleVariantDto[]> {
|
||||
|
||||
return this.articleVariantService.findAll();
|
||||
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.articleVariantService.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() createArticleVariantDto: ArticleVariantCreateDto) {
|
||||
return this.articleVariantService.create(createArticleVariantDto);
|
||||
}
|
||||
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateArticleVariantDto: ArticleVariantUpdateDto) {
|
||||
return this.articleVariantService.update(id, updateArticleVariantDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.articleVariantService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ArticleVariantService } from './article-variant.service';
|
||||
|
||||
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { ArticleVariant, ArticleVariantSchema } from '../_entities/article-variant.entity';
|
||||
import { ArticleVariantController } from './article-variant.controller';
|
||||
import { ArticleVariantRepository } from './repository/article-variant.repository';
|
||||
|
||||
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([
|
||||
{ name: ArticleVariant.name, schema: ArticleVariantSchema },
|
||||
])
|
||||
],
|
||||
controllers:[ArticleVariantController],
|
||||
providers: [ArticleVariantService,ArticleVariantRepository],
|
||||
exports:[ ArticleVariantService,ArticleVariantRepository]
|
||||
})
|
||||
export class ArticleVariantModule { }
|
||||
@@ -0,0 +1,36 @@
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ArticleVariantRepository } from './repository/article-variant.repository';
|
||||
import { ArticleVariantCreateDto, ArticleVariantDto, ArticleVariantUpdateDto } from '../_dto/article-variant.dto';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class ArticleVariantService {
|
||||
|
||||
constructor(
|
||||
private readonly articleVariantRepository: ArticleVariantRepository,
|
||||
) { }
|
||||
|
||||
create(dto: ArticleVariantCreateDto) {
|
||||
return this.articleVariantRepository.create(dto);
|
||||
}
|
||||
|
||||
findAll(): Promise<ArticleVariantDto[]> {
|
||||
return this.articleVariantRepository.findAll();
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<ArticleVariantDto> {
|
||||
return await this.articleVariantRepository.findOne(id);
|
||||
}
|
||||
|
||||
update(id: string, dto: ArticleVariantUpdateDto) {
|
||||
return this.articleVariantRepository.update(id, dto);
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
return this.articleVariantRepository.remove(id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
|
||||
export const articleVariantGetQuery = [
|
||||
{
|
||||
$lookup: {
|
||||
from: 'articles',
|
||||
localField: 'articleId',
|
||||
foreignField: '_id',
|
||||
as: 'article',
|
||||
}
|
||||
},
|
||||
{ $unwind: '$article' },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'manufacturers',
|
||||
localField: 'manufacturerId',
|
||||
foreignField: '_id',
|
||||
as: 'manufacturer',
|
||||
},
|
||||
},
|
||||
{ $unwind: '$manufacturer' },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'categories',
|
||||
localField: 'article.categoryId',
|
||||
foreignField: '_id',
|
||||
as: 'category',
|
||||
},
|
||||
},
|
||||
{ $unwind: '$category' },
|
||||
{
|
||||
$project: {
|
||||
packagingUnit: 1,
|
||||
supplierArticleNumber: 1,
|
||||
|
||||
article: {
|
||||
_id: 1,
|
||||
name: 1,
|
||||
unit: 1,
|
||||
|
||||
category: {
|
||||
_id: '$category._id',
|
||||
name: '$category.name',
|
||||
isConsumable: '$category.isConsumable',
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
manufacturer: {
|
||||
_id: 1,
|
||||
name: 1
|
||||
},
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { ArticleVariantCreateDto, ArticleVariantDto, ArticleVariantUpdateDto } from 'src/catalog/_dto/article-variant.dto';
|
||||
import { ArticleVariant } from 'src/catalog/_entities/article-variant.entity';
|
||||
import { articleVariantGetQuery } from '../querys/article-variant-get.query';
|
||||
import { ArticleVariantUpsertDto } from 'src/catalog/_dto/upsert-article.dto';
|
||||
import { BulkWriteResult } from 'mongodb';
|
||||
|
||||
@Injectable()
|
||||
export class ArticleVariantRepository {
|
||||
|
||||
constructor(
|
||||
@InjectModel(ArticleVariant.name)
|
||||
private articleVariantModel: Model<ArticleVariant>,
|
||||
) { }
|
||||
|
||||
|
||||
async findAll(): Promise<ArticleVariantDto[]> {
|
||||
return this.articleVariantModel
|
||||
.aggregate<ArticleVariantDto>([...articleVariantGetQuery])
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<ArticleVariantDto> {
|
||||
const result = await this.articleVariantModel
|
||||
.aggregate<ArticleVariantDto>([
|
||||
{
|
||||
$match: { _id: new Types.ObjectId(id) }
|
||||
},
|
||||
...articleVariantGetQuery]
|
||||
);
|
||||
|
||||
return result[0];
|
||||
}
|
||||
|
||||
create(dto: ArticleVariantCreateDto) {
|
||||
return this.articleVariantModel.create({
|
||||
articleId: new Types.ObjectId(dto.articleId),
|
||||
manufacturerId: new Types.ObjectId(dto.manufacturerId),
|
||||
supplierArticleNumber: dto.supplierArticleNumber,
|
||||
packagingUnit: dto.packagingUnit,
|
||||
extra: dto.extra
|
||||
});
|
||||
}
|
||||
|
||||
createMany(dto: ArticleVariantCreateDto[]) {
|
||||
const withObjId = dto.map((variant) => ({ ...variant, manufacturerId: new Types.ObjectId(variant.manufacturerId) }));
|
||||
return this.articleVariantModel.insertMany(withObjId)
|
||||
|
||||
}
|
||||
|
||||
update(id: string, dto: ArticleVariantUpdateDto) {
|
||||
return this.articleVariantModel.findByIdAndUpdate(id, dto, { new: true });
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
return this.articleVariantModel.findByIdAndDelete(id);
|
||||
}
|
||||
|
||||
upsertVariants(articleId: string, dto: ArticleVariantUpsertDto[]): Promise<BulkWriteResult> {
|
||||
const ops = dto.map((v) => {
|
||||
if (v._id) {
|
||||
if (v.deleted) {
|
||||
return {
|
||||
deleteOne: {
|
||||
filter: {
|
||||
_id: new Types.ObjectId(v._id),
|
||||
articleId: new Types.ObjectId(articleId),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
}
|
||||
return {
|
||||
updateOne: {
|
||||
filter: { _id: new Types.ObjectId(v._id), articleId: new Types.ObjectId(articleId) },
|
||||
update: {
|
||||
$set: {
|
||||
manufacturerId: new Types.ObjectId(v.manufacturerId),
|
||||
supplierArticleNumber: v.supplierArticleNumber,
|
||||
packagingUnit: v.packagingUnit,
|
||||
extra: v.extra,
|
||||
}
|
||||
}
|
||||
},
|
||||
upsert: false, // Upsert false, um Fehler bei Duplikaten zu kontrollieren
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
insertOne: {
|
||||
document: {
|
||||
articleId: new Types.ObjectId(articleId),
|
||||
manufacturerId: new Types.ObjectId(v.manufacturerId),
|
||||
supplierArticleNumber: v.supplierArticleNumber,
|
||||
packagingUnit: v.packagingUnit,
|
||||
extra: v.extra,
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
return this.articleVariantModel.bulkWrite(ops);
|
||||
} catch (err) {
|
||||
if (err.code === 11000) {
|
||||
throw new BadRequestException(
|
||||
'Es darf keine Variant mit gleicher manufacturerId für diesen Artikel geben',
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
test() { }
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
upsertVariants1(articleId: string, dto: ArticleVariantUpsertDto[]) {
|
||||
const ops = dto.map((v) => {
|
||||
if (v._id) {
|
||||
return {
|
||||
updateOne: {
|
||||
filter: { _id: v._id, articleId },
|
||||
update: {
|
||||
$set: {
|
||||
manufacturerId: v.manufacturerId,
|
||||
supplierArticleNumber: v.supplierArticleNumber,
|
||||
packagingUnit: v.packagingUnit,
|
||||
extra: v.extra,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
insertOne: {
|
||||
document: {
|
||||
articleId: new Types.ObjectId(articleId),
|
||||
manufacturerId: new Types.ObjectId(v.manufacturerId),
|
||||
supplierArticleNumber: v.supplierArticleNumber,
|
||||
packagingUnit: v.packagingUnit,
|
||||
extra: v.extra,
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
try {
|
||||
return this.articleVariantModel.bulkWrite(ops);
|
||||
} catch (err) {
|
||||
if (err.code === 11000) {
|
||||
throw new BadRequestException(
|
||||
'Es darf keine Variant mit gleicher manufacturerId für diesen Artikel geben',
|
||||
);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
Reference in new issue
Block a user