Skip to content

Commit

Permalink
feat(plant): remove endpoint & publish status
Browse files Browse the repository at this point in the history
  • Loading branch information
Ealenn committed Jun 24, 2022
1 parent 2a97cd0 commit 894fb6a
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 14 deletions.
4 changes: 4 additions & 0 deletions core/src/controllers/plants/models/mapper.profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
4 changes: 4 additions & 0 deletions core/src/controllers/plants/models/plant.response.body.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { PublishedState } from '../../../entities/base.published.entity';

export class PlantClassificationResponseBody {
@ApiProperty()
Expand Down Expand Up @@ -36,6 +37,9 @@ export class PlantResponseBody {
@ApiProperty({ type: PlantClassificationResponseBody })
classification: PlantClassificationResponseBody;

@ApiProperty({ enum: PublishedState })
status: PublishedState;

@ApiProperty()
createdBy: string;

Expand Down
29 changes: 19 additions & 10 deletions core/src/controllers/plants/plants.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Param,
Post,
Query,
Delete,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Mapper } from '@automapper/core';
Expand All @@ -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')
Expand All @@ -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,
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/controllers/varieties/models/mapper.profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -79,6 +80,9 @@ export class VarietyResponseBody {
@ApiProperty({ type: VarietyCultureResponseBody })
culture: VarietyCultureResponseBody;

@ApiProperty({ enum: PublishedState })
status: PublishedState;

@ApiProperty()
createdBy: string;

Expand Down
2 changes: 2 additions & 0 deletions core/src/controllers/varieties/varieties.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions core/src/entities/base.published.entity.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 2 additions & 2 deletions core/src/entities/plants/models/plant.entity.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
8 changes: 8 additions & 0 deletions core/src/entities/plants/plants.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export class PlantsService extends BaseEntityService {
}
}

async deletePlant(plantId: string): Promise<Plant | undefined> {
try {
return await this.PlantModel.findByIdAndDelete(plantId);
} catch {
return null;
}
}

async findOneById(id: string): Promise<Plant | undefined> {
return await this.PlantModel.findById(id);
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/entities/varieties/models/variety.entity.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;

Expand Down

0 comments on commit 894fb6a

Please sign in to comment.