From 894fb6a3483dc4b62e93cf17de0b744f4c742e04 Mon Sep 17 00:00:00 2001 From: Rudy Marchandise Date: Fri, 24 Jun 2022 22:42:19 +0200 Subject: [PATCH] feat(plant): remove endpoint & publish status --- .../plants/models/mapper.profiles.ts | 4 +++ .../plants/models/plant.response.body.ts | 4 +++ .../controllers/plants/plants.controller.ts | 29 ++++++++++++------- .../varieties/models/mapper.profiles.ts | 4 +++ .../varieties/models/variety.response.body.ts | 4 +++ .../varieties/varieties.controller.ts | 2 ++ core/src/entities/base.published.entity.ts | 13 +++++++++ .../entities/plants/models/plant.entity.ts | 4 +-- core/src/entities/plants/plants.service.ts | 8 +++++ .../varieties/models/variety.entity.ts | 4 +-- 10 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 core/src/entities/base.published.entity.ts diff --git a/core/src/controllers/plants/models/mapper.profiles.ts b/core/src/controllers/plants/models/mapper.profiles.ts index 74f2909..54052df 100644 --- a/core/src/controllers/plants/models/mapper.profiles.ts +++ b/core/src/controllers/plants/models/mapper.profiles.ts @@ -66,6 +66,10 @@ export class PlantMapperProfiles extends AutomapperProfile { (d) => d.classification, mapFrom((s) => mapper.map(s.classification, PlantClassification, PlantClassificationResponseBody)), ), + forMember( + (d) => d.status, + mapFrom((s) => s.status), + ), forMember( (d) => d.createdBy, mapFrom((s) => s.createdBy.toString()), diff --git a/core/src/controllers/plants/models/plant.response.body.ts b/core/src/controllers/plants/models/plant.response.body.ts index 0a9bf46..68a6786 100644 --- a/core/src/controllers/plants/models/plant.response.body.ts +++ b/core/src/controllers/plants/models/plant.response.body.ts @@ -1,4 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; +import { PublishedState } from '../../../entities/base.published.entity'; export class PlantClassificationResponseBody { @ApiProperty() @@ -36,6 +37,9 @@ export class PlantResponseBody { @ApiProperty({ type: PlantClassificationResponseBody }) classification: PlantClassificationResponseBody; + @ApiProperty({ enum: PublishedState }) + status: PublishedState; + @ApiProperty() createdBy: string; diff --git a/core/src/controllers/plants/plants.controller.ts b/core/src/controllers/plants/plants.controller.ts index 99342a6..5771533 100644 --- a/core/src/controllers/plants/plants.controller.ts +++ b/core/src/controllers/plants/plants.controller.ts @@ -8,6 +8,7 @@ import { Param, Post, Query, + Delete, } from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { Mapper } from '@automapper/core'; @@ -22,6 +23,7 @@ import { ErrorsRequestBody } from '../models/errors.response.body'; import { Response as Res } from 'express'; import { Roles } from '../../auth/roles/roles.decorator'; import { Role } from '../../auth/roles/role.enum'; +import { PublishedState } from '../../entities/base.published.entity'; @ApiBearerAuth() @ApiTags('Plants') @@ -35,15 +37,12 @@ export class PlantsController { @Roles(Role.ADMIN) @ApiResponse({ status: 403, description: 'Forbidden' }) @ApiResponse({ status: 201, type: PlantResponseBody }) - @ApiResponse({ - status: 400, - description: 'Bad Request', - type: ErrorsRequestBody, - }) + @ApiResponse({ status: 400, description: 'Bad Request', type: ErrorsRequestBody }) async createPlant(@Request() req, @Body() createPlantRequestBody: CreatePlantRequestBody) { const createPlant: Plant = { ...createPlantRequestBody, _id: null, + status: PublishedState.ONLINE, createdAt: new Date(), updatedAt: new Date(), createdBy: req.user._id, @@ -52,13 +51,23 @@ export class PlantsController { return this.mapper.map(plant, Plant, PlantResponseBody); } + @Delete(':plantId') + @Roles(Role.ADMIN) + @ApiResponse({ status: 403, description: 'Forbidden', type: ErrorsRequestBody }) + @ApiResponse({ status: 404, description: 'Not Found', type: ErrorsRequestBody }) + @ApiResponse({ status: 200, type: PlantResponseBody }) + async deletePlant(@Request() req, @Param('plantId') plantId: string) { + const plant = await this.plantsService.deletePlant(plantId); + if (!plant) { + throw new NotFoundException(); + } + + return this.mapper.map(plant, Plant, PlantResponseBody); + } + @Get(':plantId') @ApiResponse({ status: 200, type: PlantResponseBody }) - @ApiResponse({ - status: 404, - description: 'Not Found', - type: ErrorsRequestBody, - }) + @ApiResponse({ status: 404, description: 'Not Found', type: ErrorsRequestBody }) async getPlantById(@Param('plantId') plantId: string) { const plant = await this.plantsService.findOneById(plantId); if (!plant) { diff --git a/core/src/controllers/varieties/models/mapper.profiles.ts b/core/src/controllers/varieties/models/mapper.profiles.ts index 9195d3c..80d4e08 100644 --- a/core/src/controllers/varieties/models/mapper.profiles.ts +++ b/core/src/controllers/varieties/models/mapper.profiles.ts @@ -142,6 +142,10 @@ export class VarietyMapperProfiles extends AutomapperProfile { (d) => d.culture, mapFrom((s) => mapper.map(s.culture, VarietyCulture, VarietyCultureResponseBody)), ), + forMember( + (d) => d.status, + mapFrom((s) => s.status), + ), forMember( (d) => d.createdBy, mapFrom((s) => s.createdBy.toString()), diff --git a/core/src/controllers/varieties/models/variety.response.body.ts b/core/src/controllers/varieties/models/variety.response.body.ts index fa563e4..e9d7028 100644 --- a/core/src/controllers/varieties/models/variety.response.body.ts +++ b/core/src/controllers/varieties/models/variety.response.body.ts @@ -6,6 +6,7 @@ import { import { Country } from '../../../entities/countries/models/countries.entity'; import { VarietyPrecocity } from '../../../entities/varieties/models/variety.entity'; import { VarietyCultureType } from '../../../entities/varieties/models/culture.entity'; +import { PublishedState } from '../../../entities/base.published.entity'; export class VarietyRequirementWaterResponseBody { @ApiProperty({ enum: VarietyRequirementWaterNeed }) @@ -79,6 +80,9 @@ export class VarietyResponseBody { @ApiProperty({ type: VarietyCultureResponseBody }) culture: VarietyCultureResponseBody; + @ApiProperty({ enum: PublishedState }) + status: PublishedState; + @ApiProperty() createdBy: string; diff --git a/core/src/controllers/varieties/varieties.controller.ts b/core/src/controllers/varieties/varieties.controller.ts index 24c9f07..7072713 100644 --- a/core/src/controllers/varieties/varieties.controller.ts +++ b/core/src/controllers/varieties/varieties.controller.ts @@ -12,6 +12,7 @@ import mongoose from 'mongoose'; import { ErrorsRequestBody } from '../models/errors.response.body'; import { Roles } from '../../auth/roles/roles.decorator'; import { Role } from '../../auth/roles/role.enum'; +import { PublishedState } from '../../entities/base.published.entity'; @ApiBearerAuth() @ApiTags('Varieties') @@ -34,6 +35,7 @@ export class VarietiesController { const createVariety: Variety = { ...createVarietyRequestBody, _id: null, + status: PublishedState.ONLINE, plant: new mongoose.Types.ObjectId(createVarietyRequestBody.plant), requirement: { ...createVarietyRequestBody.requirement, diff --git a/core/src/entities/base.published.entity.ts b/core/src/entities/base.published.entity.ts new file mode 100644 index 0000000..1c9f28b --- /dev/null +++ b/core/src/entities/base.published.entity.ts @@ -0,0 +1,13 @@ +import { Prop } from '@nestjs/mongoose'; +import { BaseEntity } from './base.entity'; + +export enum PublishedState { + ONLINE = 'ONLINE', + DRAFT = 'DRAFT', + PENDING = 'PENDING', +} + +export class BasePublishedEntity extends BaseEntity { + @Prop({ required: true, enum: PublishedState }) + status: PublishedState; +} diff --git a/core/src/entities/plants/models/plant.entity.ts b/core/src/entities/plants/models/plant.entity.ts index d38bfc2..4ba7cef 100644 --- a/core/src/entities/plants/models/plant.entity.ts +++ b/core/src/entities/plants/models/plant.entity.ts @@ -1,12 +1,12 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; -import { BaseEntity } from '../../base.entity'; +import { BasePublishedEntity } from '../../../entities/base.published.entity'; import { PlantClassification } from './plant.classification'; export type PlantDocument = Plant & Document; @Schema() -export class Plant extends BaseEntity { +export class Plant extends BasePublishedEntity { @Prop({ required: true }) name: string; diff --git a/core/src/entities/plants/plants.service.ts b/core/src/entities/plants/plants.service.ts index cbfc694..ff00d9e 100644 --- a/core/src/entities/plants/plants.service.ts +++ b/core/src/entities/plants/plants.service.ts @@ -34,6 +34,14 @@ export class PlantsService extends BaseEntityService { } } + async deletePlant(plantId: string): Promise { + try { + return await this.PlantModel.findByIdAndDelete(plantId); + } catch { + return null; + } + } + async findOneById(id: string): Promise { return await this.PlantModel.findById(id); } diff --git a/core/src/entities/varieties/models/variety.entity.ts b/core/src/entities/varieties/models/variety.entity.ts index a433ef2..32c0e25 100644 --- a/core/src/entities/varieties/models/variety.entity.ts +++ b/core/src/entities/varieties/models/variety.entity.ts @@ -1,7 +1,7 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import mongoose, { Document } from 'mongoose'; import { Plant } from '../../plants/models/plant.entity'; -import { BaseEntity } from '../../base.entity'; +import { BasePublishedEntity } from '../../base.published.entity'; import { VarietyRequirement } from './requirement.entity'; import { VarietyCulture } from './culture.entity'; @@ -14,7 +14,7 @@ export enum VarietyPrecocity { } @Schema() -export class Variety extends BaseEntity { +export class Variety extends BasePublishedEntity { @Prop({ required: true, type: [{ type: mongoose.Schema.Types.ObjectId, ref: Plant.name }] }) plant: mongoose.Types.ObjectId;