feat: 完善喂养计划编辑
This commit is contained in:
1 parent
32f8aca49e
commit
73ac0f8049
12 files changed
+99
-18
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { Body, Controller, Post } from '@nestjs/common';
|
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common';
|
||||||
import { CreateFeedingPlanRequest } from '@/pet-feeder/dto/create-feeding-plan.request';
|
import { CreateFeedingPlanRequest } from '@/pet-feeder/dto/create-feeding-plan.request';
|
||||||
import { FeedingPlanService } from '@/pet-feeder/service/feeding-plan.service';
|
import { FeedingPlanService } from '@/pet-feeder/service/feeding-plan.service';
|
||||||
import { FeedingPlanVo } from '@/pet-feeder/vo/feeding-plan.vo';
|
import { FeedingPlanVo } from '@/pet-feeder/vo/feeding-plan.vo';
|
||||||
@@ -19,4 +19,19 @@ export class FeedingPlanController {
|
|||||||
await this.feedingPlanService.create(createFeedingPlanRequest),
|
await this.feedingPlanService.create(createFeedingPlanRequest),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Put(':id')
|
||||||
|
async update(
|
||||||
|
@Param('id') id: string,
|
||||||
|
@Body() updateFeedingRequest: CreateFeedingPlanRequest,
|
||||||
|
) {
|
||||||
|
return this.feedingPlanMapper.toVo(
|
||||||
|
await this.feedingPlanService.update(id, updateFeedingRequest),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get(':id')
|
||||||
|
async get(@Param('id') id: string): Promise<FeedingPlanVo> {
|
||||||
|
return this.feedingPlanMapper.toVo(await this.feedingPlanService.get(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -25,5 +25,5 @@ export class CreateFeedingPlanRequest {
|
|||||||
message: '产品 ID 不能为空',
|
message: '产品 ID 不能为空',
|
||||||
})
|
})
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
products: { id: string; dailyUsageWeight: number }[];
|
products: { id: string; dailyUsageWeight: number; percentage: number }[];
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,9 @@ export class FeedingPlanProduct extends BaseEntity {
|
|||||||
@Column()
|
@Column()
|
||||||
dailyUsageWeight: number;
|
dailyUsageWeight: number;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
percentage: number;
|
||||||
|
|
||||||
@ManyToOne(
|
@ManyToOne(
|
||||||
() => FeedingPlan,
|
() => FeedingPlan,
|
||||||
(feedingPlan) => feedingPlan.feedingPlanProducts,
|
(feedingPlan) => feedingPlan.feedingPlanProducts,
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ export class FeedingPlan extends BaseEntity {
|
|||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => FeedingPlanProduct,
|
() => FeedingPlanProduct,
|
||||||
(feedingPlanProduct) => feedingPlanProduct.product,
|
(feedingPlanProduct) => feedingPlanProduct.feedingPlan,
|
||||||
|
{ eager: true },
|
||||||
)
|
)
|
||||||
feedingPlanProducts: FeedingPlanProduct[];
|
feedingPlanProducts: FeedingPlanProduct[];
|
||||||
}
|
}
|
||||||
@@ -16,7 +16,8 @@ export class FeedingPlanProductMapper extends BaseMapper<
|
|||||||
return {
|
return {
|
||||||
...super.toVo(entity),
|
...super.toVo(entity),
|
||||||
dailyUsageWeight: entity.dailyUsageWeight,
|
dailyUsageWeight: entity.dailyUsageWeight,
|
||||||
product: this.productMapper.toVo(entity.product),
|
percentage: entity.percentage,
|
||||||
|
product: entity.product ? this.productMapper.toVo(entity.product) : null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,15 +2,12 @@ import { BaseMapper } from '@/core/mapper/base.mapper';
|
|||||||
import { FeedingPlan } from '@/pet-feeder/entity/feeding-plan.entity';
|
import { FeedingPlan } from '@/pet-feeder/entity/feeding-plan.entity';
|
||||||
import { FeedingPlanVo } from '@/pet-feeder/vo/feeding-plan.vo';
|
import { FeedingPlanVo } from '@/pet-feeder/vo/feeding-plan.vo';
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { PetMapper } from '@/pet-feeder/mapper/pet.mapper';
|
|
||||||
import { ProductMapper } from '@/pet-feeder/mapper/product.mapper';
|
import { ProductMapper } from '@/pet-feeder/mapper/product.mapper';
|
||||||
|
import { FeedingPlanProductMapper } from '@/pet-feeder/mapper/feeding-plan-product.mapper';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FeedingPlanMapper extends BaseMapper<FeedingPlan, FeedingPlanVo> {
|
export class FeedingPlanMapper extends BaseMapper<FeedingPlan, FeedingPlanVo> {
|
||||||
constructor(
|
constructor(private feedingPlanProductMapper: FeedingPlanProductMapper) {
|
||||||
private petMapper: PetMapper,
|
|
||||||
private productMapper: ProductMapper,
|
|
||||||
) {
|
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
toVo(entity: FeedingPlan): FeedingPlanVo {
|
toVo(entity: FeedingPlan): FeedingPlanVo {
|
||||||
@@ -18,7 +15,9 @@ export class FeedingPlanMapper extends BaseMapper<FeedingPlan, FeedingPlanVo> {
|
|||||||
...super.toVo(entity),
|
...super.toVo(entity),
|
||||||
targetWeight: entity.targetWeight,
|
targetWeight: entity.targetWeight,
|
||||||
feedingGoal: entity.feedingGoal,
|
feedingGoal: entity.feedingGoal,
|
||||||
pet: this.petMapper.toVo(entity.pet),
|
feedingPlanProducts: this.feedingPlanProductMapper.toVos(
|
||||||
|
entity.feedingPlanProducts,
|
||||||
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,14 @@ import { Pet } from '@/pet-feeder/entity/pet.entity';
|
|||||||
import { PetVo } from '@/pet-feeder/vo/pet.vo';
|
import { PetVo } from '@/pet-feeder/vo/pet.vo';
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { DateUtil } from '@/core/util/date.util';
|
import { DateUtil } from '@/core/util/date.util';
|
||||||
|
import { FeedingPlanMapper } from '@/pet-feeder/mapper/feeding-plan.mapper';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PetMapper extends BaseMapper<Pet, PetVo> {
|
export class PetMapper extends BaseMapper<Pet, PetVo> {
|
||||||
|
constructor(private readonly feedingPlanMapper: FeedingPlanMapper) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
toVo(entity: Pet): PetVo {
|
toVo(entity: Pet): PetVo {
|
||||||
return {
|
return {
|
||||||
...super.toVo(entity),
|
...super.toVo(entity),
|
||||||
@@ -20,6 +25,9 @@ export class PetMapper extends BaseMapper<Pet, PetVo> {
|
|||||||
isSterilization: entity.isSterilization,
|
isSterilization: entity.isSterilization,
|
||||||
isLactation: entity.isLactation,
|
isLactation: entity.isLactation,
|
||||||
bodyType: entity.bodyType,
|
bodyType: entity.bodyType,
|
||||||
|
feedingPlan: entity.feedingPlan
|
||||||
|
? this.feedingPlanMapper.toVo(entity.feedingPlan)
|
||||||
|
: null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,11 +38,61 @@ export class FeedingPlanService {
|
|||||||
const feedingPlanProduct = this.feedingPlanProductRepository.create({
|
const feedingPlanProduct = this.feedingPlanProductRepository.create({
|
||||||
product: existProduct,
|
product: existProduct,
|
||||||
dailyUsageWeight: product.dailyUsageWeight,
|
dailyUsageWeight: product.dailyUsageWeight,
|
||||||
|
percentage: product.percentage,
|
||||||
});
|
});
|
||||||
feedingPlanProducts.push(feedingPlanProduct);
|
feedingPlanProducts.push(
|
||||||
|
await this.feedingPlanProductRepository.save(feedingPlanProduct),
|
||||||
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
feedingPlan.feedingPlanProducts = feedingPlanProducts;
|
feedingPlan.feedingPlanProducts = feedingPlanProducts;
|
||||||
return this.feedingPlanRepository.save(feedingPlan);
|
return this.feedingPlanRepository.save(feedingPlan);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async update(
|
||||||
|
id: string,
|
||||||
|
updateFeedingPlanRequest: CreateFeedingPlanRequest,
|
||||||
|
): Promise<FeedingPlan> {
|
||||||
|
const feedingPlan = await this.get(id);
|
||||||
|
feedingPlan.feedingGoal = updateFeedingPlanRequest.feedingGoal;
|
||||||
|
feedingPlan.targetWeight = updateFeedingPlanRequest.targetWeight;
|
||||||
|
const feedingPlanProducts: FeedingPlanProduct[] = [];
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
updateFeedingPlanRequest.products.map(async (product) => {
|
||||||
|
const existProduct = await this.productService.get(product.id);
|
||||||
|
const exitFeedingPlanProduct =
|
||||||
|
await this.feedingPlanProductRepository.findOneBy({
|
||||||
|
product: existProduct,
|
||||||
|
});
|
||||||
|
if (exitFeedingPlanProduct) {
|
||||||
|
exitFeedingPlanProduct.dailyUsageWeight = product.dailyUsageWeight;
|
||||||
|
exitFeedingPlanProduct.percentage = product.percentage;
|
||||||
|
feedingPlanProducts.push(
|
||||||
|
await this.feedingPlanProductRepository.save(
|
||||||
|
exitFeedingPlanProduct,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const feedingPlanProduct = this.feedingPlanProductRepository.create({
|
||||||
|
product: existProduct,
|
||||||
|
dailyUsageWeight: product.dailyUsageWeight,
|
||||||
|
percentage: product.percentage,
|
||||||
|
});
|
||||||
|
feedingPlanProducts.push(
|
||||||
|
await this.feedingPlanProductRepository.save(feedingPlanProduct),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return this.feedingPlanRepository.save(feedingPlan);
|
||||||
|
}
|
||||||
|
|
||||||
|
async get(id: string): Promise<FeedingPlan> {
|
||||||
|
const feedingPlan = await this.feedingPlanRepository.findOneBy({ id });
|
||||||
|
if (!feedingPlan) {
|
||||||
|
throw new Error('Feeding plan not found');
|
||||||
|
}
|
||||||
|
return feedingPlan;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -41,8 +41,9 @@ export class PetService {
|
|||||||
|
|
||||||
async ownPetList(): Promise<Pet[]> {
|
async ownPetList(): Promise<Pet[]> {
|
||||||
const id = RequestContext.currentContext.req.user.id;
|
const id = RequestContext.currentContext.req.user.id;
|
||||||
return this.petRepository.findBy({
|
return this.petRepository.find({
|
||||||
createBy: { id },
|
where: { createBy: { id } },
|
||||||
|
relations: ['feedingPlan'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,9 @@ export class FeedingPlanProductVo extends BaseVo {
|
|||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
dailyUsageWeight: number;
|
dailyUsageWeight: number;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
percentage: number;
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
product: ProductVo;
|
product: ProductVo;
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
import { BaseVo } from '@/core/vo/base.vo';
|
import { BaseVo } from '@/core/vo/base.vo';
|
||||||
import { ApiProperty, ApiSchema } from '@nestjs/swagger';
|
import { ApiProperty, ApiSchema } from '@nestjs/swagger';
|
||||||
import { FeedingGoal } from '@/pet-feeder/enum/feeding-goal';
|
import { FeedingGoal } from '@/pet-feeder/enum/feeding-goal';
|
||||||
import { PetVo } from '@/pet-feeder/vo/pet.vo';
|
|
||||||
import { FeedingPlanProduct } from '@/pet-feeder/entity/feeding-plan-product.entity';
|
|
||||||
import { FeedingPlanProductVo } from '@/pet-feeder/vo/feeding-plan-product.vo';
|
import { FeedingPlanProductVo } from '@/pet-feeder/vo/feeding-plan-product.vo';
|
||||||
|
|
||||||
@ApiSchema({
|
@ApiSchema({
|
||||||
@@ -15,9 +13,6 @@ export class FeedingPlanVo extends BaseVo {
|
|||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
feedingGoal: FeedingGoal;
|
feedingGoal: FeedingGoal;
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
pet: PetVo;
|
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
feedingPlanProducts: FeedingPlanProductVo[];
|
feedingPlanProducts: FeedingPlanProductVo[];
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,8 @@ import { ApiProperty, ApiSchema } from '@nestjs/swagger';
|
|||||||
import { PetType } from '@/pet-feeder/enum/pet-type';
|
import { PetType } from '@/pet-feeder/enum/pet-type';
|
||||||
import { Gender } from '@/core/enum/Gender';
|
import { Gender } from '@/core/enum/Gender';
|
||||||
import { PetBodyType } from '@/pet-feeder/enum/pet-body-type';
|
import { PetBodyType } from '@/pet-feeder/enum/pet-body-type';
|
||||||
|
import { FeedingPlan } from '@/pet-feeder/entity/feeding-plan.entity';
|
||||||
|
import { FeedingPlanVo } from '@/pet-feeder/vo/feeding-plan.vo';
|
||||||
|
|
||||||
@ApiSchema({
|
@ApiSchema({
|
||||||
name: 'Pet',
|
name: 'Pet',
|
||||||
@@ -32,4 +34,7 @@ export class PetVo extends TraceableVo {
|
|||||||
isLactation: boolean;
|
isLactation: boolean;
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
bodyType: PetBodyType;
|
bodyType: PetBodyType;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
feedingPlan: FeedingPlanVo | null;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user