Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: create plant water #35

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/objects/response-message.object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export const RESPONSE_MESSAGE: {
// plants
READ_PLANT_INFORMATION_SUCCESS: '식물 단계 쑰회 성곡',
READ_PLANT_WATER_LOG_SUCCESS: '식물 λ¬Όμ£ΌκΈ° 기둝 쑰회 성곡',
CREATE_PLANT_WATER_LOG_SUCCESS: '식물 λ¬Όμ£ΌκΈ° 생성 성곡',
};
13 changes: 11 additions & 2 deletions src/constants/swagger/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { SIGNIN_DESCRIPTION } from './auth';
import { PLANT_INFORMATION, PLANT_WATER_LOG } from './plants';
import {
PLANT_INFORMATION,
GET_PLANT_WATER_LOG,
CREATE_PLANT_WATER,
} from './plants';

export const ERROR_DESCRIPTION = {
INTERNAL_SERVER_ERROR: 'Internal Server Error',
};

export { SIGNIN_DESCRIPTION, PLANT_INFORMATION, PLANT_WATER_LOG };
export {
SIGNIN_DESCRIPTION,
PLANT_INFORMATION,
GET_PLANT_WATER_LOG,
CREATE_PLANT_WATER,
};
22 changes: 21 additions & 1 deletion src/constants/swagger/plants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const PLANT_INFORMATION = {
},
};

export const PLANT_WATER_LOG = {
export const GET_PLANT_WATER_LOG = {
API_OPERATION: {
SUMMARY: '식물 λ¬Όμ£ΌκΈ° 쑰회 API',
DESCRIPTION: '식물별 λ¬Όμ£ΌκΈ° 기둝을 μ‘°νšŒν•©λ‹ˆλ‹€.',
Expand All @@ -49,3 +49,23 @@ export const PLANT_WATER_LOG = {
BAD_REQUEST: 'Bad Request - μš”μ²­ id κ°€ μ—†κ±°λ‚˜, numberκ°€ μ•„λ‹Œ 경우 λ“±',
},
};

export const CREATE_PLANT_WATER = {
API_OPERATION: {
SUMMARY: '식물 λ¬Όμ£ΌκΈ° 생성 API',
DESCRIPTION: '식물별 λ¬Όμ£ΌκΈ°λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.',
},
API_PARAM: {
NAME: 'id',
DESCRIPTION: 'user_plant_id',
},
DTO_DESCRIPTION: {
RESPONSE: {
ID: 'water id',
else: '더 μž‘μ„±ν•΄μ•Ό 함',
},
},
ERROR_DESCRIPTION: {
BAD_REQUEST: 'Bad Request - ν•΄λ‹Ή λ‚ μ§œμ— 이미 λ¬Ό μ€€ 경우',
},
};
52 changes: 52 additions & 0 deletions src/plants/dto/plantwaterlog.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
import { ResponseSuccessDto } from 'src/common/dto/response-success.dto';
import { GET_PLANT_WATER_LOG } from 'src/constants/swagger';

const DTO_RESPONSE_DESCRIPTION = GET_PLANT_WATER_LOG.DTO_DESCRIPTION.RESPONSE;

export class PlantWaterLogData {
@ApiProperty({ description: DTO_RESPONSE_DESCRIPTION.REVIEWS.ID })
id: number;

@ApiProperty({
description: DTO_RESPONSE_DESCRIPTION.REVIEWS.CREATEDAT,
})
wateringDate: string;

@ApiProperty({ description: DTO_RESPONSE_DESCRIPTION.REVIEWS.REVIEW })
review: string;

@ApiProperty({ description: DTO_RESPONSE_DESCRIPTION.REVIEWS.REVIEW })
keywords?: string[];
}

export class ResponseGetPlantWaterLogData {
@ApiProperty({ isArray: true, type: PlantWaterLogData })
reviews: PlantWaterLogData[];
}

export class ResponseGetPlantWaterLogDto extends ResponseSuccessDto {
@ApiProperty()
data: ResponseGetPlantWaterLogData;
}

export class CreatePlantWaterLogDto {
@ApiProperty()
keywords: string[];

@ApiProperty()
@IsString()
review: string;
}

export class ResponseCreatePlantWaterDto {
@ApiProperty()
id: number;

@ApiProperty()
userPlantId: number;

@ApiProperty()
wateringDate: Date;
}
28 changes: 0 additions & 28 deletions src/plants/dto/response-plantwaterlog.dto.ts

This file was deleted.

52 changes: 42 additions & 10 deletions src/plants/plants.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, HttpStatus, Param } from '@nestjs/common';
import { Body, Controller, Get, HttpStatus, Param, Post } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiInternalServerErrorResponse,
Expand All @@ -15,11 +15,16 @@ import { CommonParamsDto } from 'src/common/dto/common-params.dto';
import {
ERROR_DESCRIPTION,
PLANT_INFORMATION,
PLANT_WATER_LOG,
GET_PLANT_WATER_LOG,
CREATE_PLANT_WATER,
} from 'src/constants/swagger';
import { wrapSuccess } from 'src/utils/success';
import { RESPONSE_MESSAGE } from 'src/common/objects';
import { ResponsePlantWaterLogDto } from './dto/response-plantwaterlog.dto';
import {
ResponseCreatePlantWaterDto,
ResponseGetPlantWaterLogDto,
} from './dto/plantwaterlog.dto';
import { badRequest } from 'src/utils/error';

@Controller('plants')
@ApiTags('Plants')
Expand Down Expand Up @@ -61,22 +66,22 @@ export class PlantsController {

@Get(':id/water')
@ApiOperation({
summary: PLANT_WATER_LOG.API_OPERATION.SUMMARY,
description: PLANT_WATER_LOG.API_OPERATION.DESCRIPTION,
summary: GET_PLANT_WATER_LOG.API_OPERATION.SUMMARY,
description: GET_PLANT_WATER_LOG.API_OPERATION.DESCRIPTION,
})
@ApiParam({
type: Number,
name: PLANT_WATER_LOG.API_PARAM.NAME,
name: GET_PLANT_WATER_LOG.API_PARAM.NAME,
required: true,
description: PLANT_WATER_LOG.API_PARAM.DESCRIPTION,
description: GET_PLANT_WATER_LOG.API_PARAM.DESCRIPTION,
})
@ApiOkResponse({ type: ResponsePlantWaterLogDto })
@ApiOkResponse({ type: ResponseGetPlantWaterLogDto })
@ApiBadRequestResponse({
description: PLANT_WATER_LOG.ERROR_DESCRIPTION.BAD_REQUEST,
description: GET_PLANT_WATER_LOG.ERROR_DESCRIPTION.BAD_REQUEST,
})
async getPlantWaterLog(
@Param() { id }: CommonParamsDto,
): Promise<ResponsePlantWaterLogDto> {
): Promise<ResponseGetPlantWaterLogDto> {
const data = await this.plantsService.getPlantWaterLog(id);

return wrapSuccess(
Expand All @@ -85,4 +90,31 @@ export class PlantsController {
data,
);
}

@Post(':id/water')
@ApiOperation({
summary: CREATE_PLANT_WATER.API_OPERATION.SUMMARY,
description: CREATE_PLANT_WATER.API_OPERATION.DESCRIPTION,
})
@ApiParam({
type: Number,
name: CREATE_PLANT_WATER.API_PARAM.NAME,
required: true,
description: CREATE_PLANT_WATER.API_PARAM.DESCRIPTION,
})
@ApiOkResponse({ type: ResponseCreatePlantWaterDto })
@ApiBadRequestResponse({
description: CREATE_PLANT_WATER.ERROR_DESCRIPTION.BAD_REQUEST,
})
async postPlantWaterLog(
@Param() { id }: CommonParamsDto,
): Promise<ResponseGetPlantWaterLogDto> {
const data = await this.plantsService.createPlantWater(id);

return wrapSuccess(
HttpStatus.OK,
RESPONSE_MESSAGE.CREATE_PLANT_WATER_LOG_SUCCESS,
data,
);
}
}
62 changes: 59 additions & 3 deletions src/plants/plants.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Injectable } from '@nestjs/common';

import { PrismaService } from 'src/prisma.service';
import { notFound } from 'src/utils/error';
import { notFound, badRequest } from 'src/utils/error';
import { renameObjectKey } from 'src/utils/object';
import { ResponsePlantInformationData } from './dto/response-plantInformation.dto';
import { ResponsePlantWaterLogData } from './dto/response-plantwaterlog.dto';
import {
ResponseGetPlantWaterLogData,
ResponseCreatePlantWaterDto,
} from './dto/plantwaterlog.dto';
import * as dayjs from 'dayjs';
import { getCurrentSeoulTime } from 'src/utils/date';

@Injectable()
export class PlantsService {
Expand Down Expand Up @@ -41,7 +45,7 @@ export class PlantsService {
);
}

async getPlantWaterLog(id: number): Promise<ResponsePlantWaterLogData> {
async getPlantWaterLog(id: number): Promise<ResponseGetPlantWaterLogData> {
const reviews = await this.prisma.water.findMany({
where: { userPlantId: id, isDeleted: false },
select: {
Expand All @@ -65,4 +69,56 @@ export class PlantsService {

return { reviews: result };
}

async createPlantWater(id: number): Promise<ResponseCreatePlantWaterDto> {
const today: Date = getCurrentSeoulTime();

const startOfDay = new Date(today);
startOfDay.setUTCHours(0, 0, 0, 0);

const endOfDay = new Date(startOfDay);
endOfDay.setDate(endOfDay.getDate() + 1);

const checkTodayWatering = await this.prisma.water.findFirst({
where: {
userPlantId: id,
wateringDate: {
gte: startOfDay,
lt: endOfDay,
},
},
});

if (checkTodayWatering) {
throw badRequest();
}
const waterData = {
userPlantId: id,
wateringDate: today,
};

const water = await this.prisma.water.create({
data: waterData,
});

const updateUserPlant = await this.prisma.userPlant.update({
where: { id },
data: {
loveGauge: {
increment: 0.5,
},
waterCount: {
increment: 1,
},
},
});

const result = {
id: water.id,
userPlantId: water.userPlantId,
wateringDate: water.wateringDate,
};

return result;
}
}
11 changes: 11 additions & 0 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function getCurrentSeoulTime(): Date {
const seoulTimeZone = 'Asia/Seoul';
const currentTime: Date = new Date();

const seoulTime: Date = new Date(
currentTime.toLocaleString('en-US', { timeZone: seoulTimeZone }),
);

seoulTime.setHours(seoulTime.getHours() + 9);
return seoulTime;
}
1 change: 1 addition & 0 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Logger } from '@nestjs/common';
import { CreateSigninDto } from 'src/auth/dto/create-signin.dto';
import { CustomException } from 'src/exceptions';
import { badRequest } from './error';
import { CreatePlantWaterLogDto } from 'src/plants/dto/plantwaterlog.dto';

export class Validation {
private readonly logger = new Logger(Validation.name);
Expand Down