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,25 @@
|
||||
import { Types } from 'mongoose';
|
||||
import { ManufacturerDto } from './manufacturer.dto';
|
||||
import { ArticleDto } from './article.dto';
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
|
||||
export class ArticleVariantDto {
|
||||
_id: Types.ObjectId;
|
||||
article: ArticleDto;
|
||||
manufacturer: ManufacturerDto;
|
||||
supplierArticleNumber?: string;
|
||||
packagingUnit?: string;
|
||||
extra?: Record<string, any>;
|
||||
}
|
||||
|
||||
export class ArticleVariantCreateDto {
|
||||
articleId: Types.ObjectId;
|
||||
manufacturerId: Types.ObjectId;
|
||||
supplierArticleNumber?: string;
|
||||
packagingUnit?: string;
|
||||
extra?: Record<string, any>;
|
||||
}
|
||||
|
||||
|
||||
export class ArticleVariantUpdateDto extends PartialType(ArticleVariantCreateDto) {}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import { Types } from 'mongoose';
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
|
||||
import { CategoryDto } from './category.dto';
|
||||
import { ManufacturerDto } from './manufacturer.dto';
|
||||
import { Type } from "class-transformer";
|
||||
import { IsArray, IsMongoId, IsObject, IsOptional, IsString, ValidateNested } from "class-validator";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export class ArticleDto {
|
||||
_id: Types.ObjectId;
|
||||
name: string;
|
||||
category: CategoryDto;
|
||||
attributes?: Record<string, any>;
|
||||
variants: ArticleVariantDto[]
|
||||
}
|
||||
|
||||
|
||||
export class ArticleAndVariantsDto {
|
||||
_id: Types.ObjectId;
|
||||
name: string;
|
||||
categoryId: Types.ObjectId;
|
||||
attributes?: Record<string, any>;
|
||||
variants: {
|
||||
_id:Types.ObjectId;
|
||||
manufacturerId: Types.ObjectId;
|
||||
supplierArticleNumber?: string;
|
||||
packagingUnit?: string;
|
||||
extra?: Record<string, any>;
|
||||
}[]
|
||||
}
|
||||
|
||||
export class ArticleVariantBaseDto {
|
||||
_id: Types.ObjectId;
|
||||
article: ArticleDto;
|
||||
manufacturer: ManufacturerDto;
|
||||
supplierArticleNumber?: string;
|
||||
packagingUnit?: string;
|
||||
extra?: Record<string, any>;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export class ArticleVariantDto {
|
||||
@IsMongoId()
|
||||
manufacturerId: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
supplierArticleNumber?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
packagingUnit?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
extra?: Record<string, any>;
|
||||
}
|
||||
|
||||
|
||||
export class ArticleCreateDto {
|
||||
@IsString()
|
||||
name: string;
|
||||
|
||||
@IsMongoId()
|
||||
categoryId: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
attributes?: Record<string, any>;
|
||||
|
||||
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => ArticleVariantDto)
|
||||
variants: ArticleVariantDto[];
|
||||
}
|
||||
|
||||
export class ArticleUpdateDto extends PartialType(ArticleCreateDto) {}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { PartialType } from "@nestjs/mapped-types";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
export class CategoryCreateDto {
|
||||
name: string;
|
||||
isConsumable: boolean;
|
||||
description?: string;
|
||||
requiredAttributes?: Record<string, any>;
|
||||
parentCategoryId?: Types.ObjectId;
|
||||
|
||||
}
|
||||
|
||||
export class CategoryUpdateDto extends PartialType(CategoryCreateDto) {}
|
||||
|
||||
export class CategoryDto {
|
||||
_id: Types.ObjectId;
|
||||
name: string;
|
||||
isConsumable: boolean;
|
||||
description?: string;
|
||||
requiredAttributes?: Record<string, any>;
|
||||
parentCategoryId?: Types.ObjectId;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { IsOptional, IsString } from "class-validator";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
export class ManufacturerCreateDto {
|
||||
@IsString()
|
||||
name: string;
|
||||
}
|
||||
|
||||
export class ManufacturerDto {
|
||||
_id: Types.ObjectId;
|
||||
name: string;
|
||||
}
|
||||
|
||||
|
||||
export class ManufacturerUpdateDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name: string;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { Type } from "class-transformer";
|
||||
import { IsArray, IsBoolean, IsMongoId, IsObject, IsOptional, IsString, ValidateNested } from "class-validator";
|
||||
|
||||
export class ArticleVariantUpsertDto {
|
||||
@IsOptional()
|
||||
@IsMongoId()
|
||||
_id: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsMongoId()
|
||||
manufacturerId: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
supplierArticleNumber?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
packagingUnit?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
extra?: Record<string, any>;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
deleted?: boolean;
|
||||
}
|
||||
|
||||
export class ArticleUpsertDto {
|
||||
@IsOptional()
|
||||
//@IsMongoId()
|
||||
_id: string;
|
||||
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsMongoId()
|
||||
categoryId: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
attributes?: Record<string, any>;
|
||||
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => ArticleVariantUpsertDto)
|
||||
variants: ArticleVariantUpsertDto[];
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { Document, Types } from 'mongoose';
|
||||
|
||||
@Schema()
|
||||
export class ArticleVariant extends Document {
|
||||
@Prop({ type: Types.ObjectId, ref: 'Article', required: true })
|
||||
articleId: Types.ObjectId;
|
||||
|
||||
@Prop({ type: Types.ObjectId, ref: 'Manufacturer', required: true })
|
||||
manufacturerId: Types.ObjectId;
|
||||
|
||||
@Prop()
|
||||
supplierArticleNumber?: string;
|
||||
|
||||
@Prop()
|
||||
packagingUnit?: string; // z. B. "Karton 12 Stk"
|
||||
|
||||
@Prop({ type: Object })
|
||||
extra?: Record<string, any>;
|
||||
}
|
||||
|
||||
|
||||
export const ArticleVariantSchema = SchemaFactory.createForClass(ArticleVariant);
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { Document, Types } from 'mongoose';
|
||||
|
||||
@Schema({ timestamps: true })
|
||||
export class Article extends Document {
|
||||
@Prop({ required: true })
|
||||
name: string;
|
||||
|
||||
@Prop({ type: Types.ObjectId, ref: 'Category', required: true })
|
||||
categoryId: Types.ObjectId;
|
||||
|
||||
@Prop({ type: Map, of: String })
|
||||
attributes?: Map<string, any>;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
export const ArticleSchema = SchemaFactory.createForClass(Article);
|
||||
|
||||
ArticleSchema.virtual('varianten', {
|
||||
ref: 'ArticleVariant',
|
||||
localField: '_id',
|
||||
foreignField: 'articleId',
|
||||
});
|
||||
|
||||
ArticleSchema.set('toJSON', { virtuals: true });
|
||||
ArticleSchema.set('toObject', { virtuals: true });
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { Document, Types } from 'mongoose';
|
||||
|
||||
@Schema({ timestamps: true })
|
||||
export class Category extends Document {
|
||||
@Prop({ required: true })
|
||||
name: string;
|
||||
|
||||
@Prop()
|
||||
description?: string;
|
||||
|
||||
@Prop({ default: true })
|
||||
isConsumable: boolean;
|
||||
|
||||
@Prop({ type: Map, of: String })
|
||||
requiredAttributes?: Map<string, string>;
|
||||
|
||||
}
|
||||
|
||||
export const CategorySchema = SchemaFactory.createForClass(Category);
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { Document, Types } from 'mongoose';
|
||||
|
||||
|
||||
@Schema()
|
||||
export class Manufacturer extends Document {
|
||||
@Prop({ required: true })
|
||||
name: string;
|
||||
|
||||
}
|
||||
|
||||
export const ManufacturerSchema =
|
||||
SchemaFactory.createForClass(Manufacturer);
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,44 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { ArticlesService } from './articles.service';
|
||||
|
||||
|
||||
import { ArticleCreateDto, ArticleUpdateDto } from '../_dto/article.dto';
|
||||
import { ArticleUpsertDto } from '../_dto/upsert-article.dto';
|
||||
|
||||
@Controller('articles')
|
||||
export class ArticlesController {
|
||||
constructor(
|
||||
private readonly articlesService: ArticlesService,
|
||||
) { }
|
||||
|
||||
@Post()
|
||||
create(@Body() dto: ArticleCreateDto) {
|
||||
console.log('Create Artikel')
|
||||
return this.articlesService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.articlesService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
console.log('Find one')
|
||||
return this.articlesService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateArticleDto: ArticleUpsertDto) {
|
||||
return this.articlesService.update(id, updateArticleDto);
|
||||
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.articlesService.remove(id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ArticlesService } from './articles.service';
|
||||
import { ArticlesController } from './articles.controller';
|
||||
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Article, ArticleSchema } from '../_entities/article.entity';
|
||||
import { ArticleVariantModule } from '../article-variant/article-variant.module';
|
||||
import { ArticleRepository } from './repository/article.repository';
|
||||
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([
|
||||
{ name: Article.name, schema: ArticleSchema },
|
||||
]),
|
||||
ArticleVariantModule
|
||||
],
|
||||
controllers: [ArticlesController],
|
||||
providers: [ArticlesService,ArticleRepository],
|
||||
exports:[ArticlesService]
|
||||
})
|
||||
export class ArticlesModule { }
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
|
||||
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
import { Article } from '../_entities/article.entity';
|
||||
import { ArticleRepository } from './repository/article.repository';
|
||||
import { ArticleAndVariantsDto, ArticleDto, ArticleUpdateDto,ArticleCreateDto } from '../_dto/article.dto';
|
||||
|
||||
import { ArticleUpsertDto } from '../_dto/upsert-article.dto';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class ArticlesService {
|
||||
constructor(
|
||||
@InjectModel(Article.name) private articleModel: Model<Article>,
|
||||
private readonly articleRepository: ArticleRepository
|
||||
) { }
|
||||
|
||||
create(dto: ArticleCreateDto) {
|
||||
return this.articleRepository.creatArticle(dto);
|
||||
|
||||
}
|
||||
|
||||
async findAll(): Promise<ArticleDto[]> {
|
||||
return this.articleRepository.findAll();
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<ArticleDto> {
|
||||
const article = await this.articleRepository.findOne(id);
|
||||
|
||||
|
||||
|
||||
|
||||
return article;
|
||||
|
||||
}
|
||||
|
||||
update(id: string, dto: ArticleUpsertDto) {
|
||||
console.log('update service')
|
||||
return this.articleRepository.updateArticle(id,dto);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
console.log('all variants deleted')
|
||||
return this.articleRepository.deleteArticle(id);
|
||||
|
||||
|
||||
//return this.articleModel.findByIdAndDelete(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
import { Types } from "mongoose";
|
||||
|
||||
export const articleGetQuery = [
|
||||
{
|
||||
$lookup: {
|
||||
from: 'articlevariants',
|
||||
localField: '_id',
|
||||
foreignField: 'articleId',
|
||||
as: 'varianten',
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'manufacturers',
|
||||
localField: 'varianten.manufacturerId',
|
||||
foreignField: '_id',
|
||||
as: 'manufacturer',
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'categories',
|
||||
localField: 'categoryId',
|
||||
foreignField: '_id',
|
||||
as: 'category',
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$category',
|
||||
preserveNullAndEmptyArrays: true
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
name: 1,
|
||||
category: {
|
||||
_id: '$category._id',
|
||||
name: '$category.name',
|
||||
isConsumable: '$category.isConsumable'
|
||||
},
|
||||
|
||||
variants: {
|
||||
$map: {
|
||||
input: '$varianten',
|
||||
as: 'v',
|
||||
in: {
|
||||
_id: '$$v._id',
|
||||
packagingUnit: '$$v.packagingUnit',
|
||||
manufacturer: {
|
||||
$arrayElemAt: [
|
||||
{
|
||||
$filter: {
|
||||
input: '$manufacturer',
|
||||
as: 'm',
|
||||
cond: {
|
||||
$eq: [
|
||||
'$$m._id',
|
||||
{ $toObjectId: '$$v.manufacturerId' }
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
0
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
];
|
||||
|
||||
export const articleGetQuery2 = [
|
||||
{
|
||||
$lookup: {
|
||||
from: 'articlevariants',
|
||||
localField: '_id',
|
||||
foreignField: 'articleId',
|
||||
as: 'varianten',
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'manufacturers',
|
||||
localField: 'varianten.manufacturerId',
|
||||
foreignField: '_id',
|
||||
as: 'manufacturer',
|
||||
},
|
||||
},
|
||||
{ $unwind: '$manufacturer' },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'categories',
|
||||
localField: 'categoryId',
|
||||
foreignField: '_id',
|
||||
as: 'category',
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$category',
|
||||
preserveNullAndEmptyArrays: true
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
$project: {
|
||||
name: 1,
|
||||
category: {
|
||||
_id: '$category._id',
|
||||
name: '$category.name',
|
||||
isConsumable: 1,
|
||||
},
|
||||
varianten: {
|
||||
_id: 1,
|
||||
manufacturer: {
|
||||
_id: '$manufacturer._id',
|
||||
name: '$manufacturer.name',
|
||||
},
|
||||
packagingUnit: 1
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
];
|
||||
@@ -0,0 +1,113 @@
|
||||
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model, Types } from 'mongoose';
|
||||
|
||||
import { ArticleCreateDto, ArticleDto } from 'src/catalog/_dto/article.dto';
|
||||
import { Article } from 'src/catalog/_entities/article.entity';
|
||||
import { ArticleVariantRepository } from 'src/catalog/article-variant/repository/article-variant.repository';
|
||||
import { articleGetQuery } from '../querys/article-get.query';
|
||||
|
||||
import { ArticleUpsertDto, ArticleVariantUpsertDto } from 'src/catalog/_dto/upsert-article.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ArticleRepository {
|
||||
|
||||
constructor(
|
||||
@InjectModel(Article.name)
|
||||
private articleModel: Model<Article>,
|
||||
private readonly articleVariantRepository: ArticleVariantRepository
|
||||
|
||||
) { }
|
||||
|
||||
async findAll(): Promise<ArticleDto[]> {
|
||||
const res = await this.articleModel.aggregate<ArticleDto>(
|
||||
[...articleGetQuery]);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
async findOne(id: string): Promise<ArticleDto> {
|
||||
const res = await this.articleModel.aggregate<ArticleDto>(
|
||||
[{ $match: { _id: new Types.ObjectId(id) } }
|
||||
, ...articleGetQuery]);
|
||||
|
||||
return res[0];
|
||||
}
|
||||
|
||||
async creatArticle(dto: ArticleCreateDto) {
|
||||
const { name, variants, categoryId, attributes } = dto;
|
||||
|
||||
|
||||
const isSameName = await this.articleModel.findOne({ name: name });
|
||||
if (isSameName) throw new ConflictException("Name ist arlready exsist!");
|
||||
|
||||
const newArticle = await this.articleModel.create({ name, categoryId: new Types.ObjectId(categoryId), attributes })
|
||||
|
||||
|
||||
const variantsWithNewArticleId = variants.map((variant) => (
|
||||
{
|
||||
packagingUnit: variant.packagingUnit,
|
||||
manufacturerId: new Types.ObjectId(variant.manufacturerId),
|
||||
articleId: newArticle._id
|
||||
}
|
||||
));
|
||||
|
||||
await this.articleVariantRepository.createMany(variantsWithNewArticleId)
|
||||
|
||||
return this.findOne(newArticle.id);
|
||||
|
||||
}
|
||||
|
||||
async updateArticle(id: string, dto: ArticleUpsertDto) {
|
||||
const { name, variants, categoryId, attributes } = dto;
|
||||
|
||||
|
||||
const article = await this.articleModel.findOne({ _id: id });
|
||||
|
||||
if (!article) throw new NotFoundException("Article nicht gefunden");
|
||||
|
||||
if (dto.name !== undefined) article.name = dto.name;
|
||||
if (dto.categoryId !== undefined) article.categoryId = new Types.ObjectId(dto.categoryId);
|
||||
if (dto.attributes !== undefined) { article.attributes = new Map(Object.entries(dto.attributes));}
|
||||
|
||||
article.updateOne({
|
||||
$set:{
|
||||
name,categoryId,attributes
|
||||
}
|
||||
});
|
||||
article.save();
|
||||
|
||||
|
||||
await this.articleVariantRepository.upsertVariants(id,variants)
|
||||
|
||||
return this.findOne(id);
|
||||
|
||||
}
|
||||
|
||||
async deleteArticle(id: string) {
|
||||
|
||||
const article = await this.findOne(id);
|
||||
|
||||
if (true) throw new NotFoundException("deleteArticle Not Supportet jet");
|
||||
if (!article) throw new NotFoundException("Article nicht gefunden");
|
||||
|
||||
/*
|
||||
for (const item of article.variants) {
|
||||
const res = await this.articleVariantRepository.remove(item._id.toString())
|
||||
}
|
||||
|
||||
const deleteIt = await this.articleModel.deleteOne(article._id);
|
||||
|
||||
console.log(deleteIt)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return {_id:id};
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { CategoriesModule } from './categories/categories.module';
|
||||
import { ArticlesModule } from './articles/articles.module';
|
||||
import { ArticleVariantModule } from './article-variant/article-variant.module';
|
||||
import { ManufacturerModule } from './manufacturer/manufacturer.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ArticlesModule,
|
||||
ArticleVariantModule,
|
||||
CategoriesModule,
|
||||
ManufacturerModule,
|
||||
],
|
||||
exports: [ArticlesModule, ArticleVariantModule]
|
||||
})
|
||||
export class CatalogModule { }
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { CategoriesService } from './categories.service';
|
||||
import { CategoryCreateDto,CategoryUpdateDto } from '../_dto/category.dto';
|
||||
|
||||
@Controller('categories')
|
||||
export class CategoriesController {
|
||||
constructor(private readonly categoriesService: CategoriesService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createCategoryDto: CategoryCreateDto) {
|
||||
return this.categoriesService.create(createCategoryDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.categoriesService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.categoriesService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateCategoryDto: CategoryUpdateDto) {
|
||||
return this.categoriesService.update(id, updateCategoryDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.categoriesService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Category, CategorySchema } from '../_entities/category.entity';
|
||||
import { CategoriesService } from './categories.service';
|
||||
import { CategoriesController } from './categories.controller';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([
|
||||
{ name: Category.name, schema: CategorySchema },
|
||||
])
|
||||
],
|
||||
controllers: [CategoriesController],
|
||||
providers: [CategoriesService],
|
||||
})
|
||||
export class CategoriesModule {}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { Injectable,NotFoundException } from '@nestjs/common';
|
||||
import { CategoryCreateDto,CategoryUpdateDto } from '../_dto/category.dto';
|
||||
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model } from 'mongoose';
|
||||
import { Category } from '../_entities/category.entity';
|
||||
|
||||
@Injectable()
|
||||
export class CategoriesService {
|
||||
constructor(
|
||||
@InjectModel(Category.name)
|
||||
private categoryModel: Model<Category>,
|
||||
) { }
|
||||
|
||||
create(dto: CategoryCreateDto) {
|
||||
return this.categoryModel.create(dto);
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return this.categoryModel
|
||||
.find();
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const category = await this.categoryModel.findById(id);
|
||||
if (!category) throw new NotFoundException('Category nicht gefunden');
|
||||
return category;
|
||||
}
|
||||
|
||||
update(id: string, dto: CategoryUpdateDto) {
|
||||
|
||||
|
||||
return this.categoryModel.findByIdAndUpdate(id, dto, { new: true });
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
return this.categoryModel.findByIdAndDelete(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { ManufacturerService } from './manufacturer.service';
|
||||
import { ManufacturerCreateDto, ManufacturerUpdateDto } from '../_dto/manufacturer.dto';
|
||||
|
||||
|
||||
@Controller('manufacturer')
|
||||
export class ManufacturerController {
|
||||
constructor(private readonly manufacturerService: ManufacturerService) {}
|
||||
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.manufacturerService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.manufacturerService.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() createManufacturerDto: ManufacturerCreateDto) {
|
||||
return this.manufacturerService.create(createManufacturerDto);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateManufacturerDto: ManufacturerUpdateDto) {
|
||||
console.log(updateManufacturerDto)
|
||||
return this.manufacturerService.update(id, updateManufacturerDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.manufacturerService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ManufacturerService } from './manufacturer.service';
|
||||
import { ManufacturerController } from './manufacturer.controller';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
import { Manufacturer, ManufacturerSchema } from '../_entities/manufacturer.entity';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forFeature([
|
||||
{ name: Manufacturer.name, schema: ManufacturerSchema },
|
||||
])
|
||||
],
|
||||
controllers: [ManufacturerController],
|
||||
providers: [ManufacturerService],
|
||||
})
|
||||
export class ManufacturerModule {}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Injectable,NotFoundException } from '@nestjs/common';
|
||||
import { ManufacturerCreateDto, ManufacturerUpdateDto } from '../_dto/manufacturer.dto';
|
||||
import { InjectModel } from '@nestjs/mongoose';
|
||||
import { Model } from 'mongoose';
|
||||
import { Manufacturer } from '../_entities/manufacturer.entity';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class ManufacturerService {
|
||||
constructor(
|
||||
@InjectModel(Manufacturer.name)
|
||||
private manufacturerModel: Model<Manufacturer>,
|
||||
) { }
|
||||
|
||||
create(dto: ManufacturerCreateDto) {
|
||||
return this.manufacturerModel.create(dto);
|
||||
}
|
||||
|
||||
findAll() {
|
||||
return this.manufacturerModel
|
||||
.find();
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const article = await this.manufacturerModel.findById(id);
|
||||
if (!article) throw new NotFoundException('Artikel nicht gefunden');
|
||||
return article;
|
||||
}
|
||||
|
||||
update(id: string, dto: ManufacturerUpdateDto) {
|
||||
return this.manufacturerModel.findByIdAndUpdate(id, dto, { new: true });
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
return this.manufacturerModel.findByIdAndDelete(id);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user