Skip to content

Commit

Permalink
feat: user info
Browse files Browse the repository at this point in the history
  • Loading branch information
GanghyeonSeo committed Jun 3, 2024
1 parent 9f94b77 commit cea7650
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/ai/ai.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { HttpModule } from '@nestjs/axios';
import { FileModule } from 'src/file/file.module';
import { AiService } from './ai.service';
import { AiRepository } from './ai.repository';
import { UserModule } from 'src/user/user.module';

@Module({
imports: [ConfigModule, HttpModule, FileModule],
imports: [ConfigModule, HttpModule, FileModule, UserModule],
controllers: [AiController],
providers: [AiService, AiRepository],
})
Expand Down
17 changes: 12 additions & 5 deletions src/ai/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ConfigService } from '@nestjs/config';
import { catchError, firstValueFrom } from 'rxjs';
import { FileService } from 'src/file/file.service';
import { AiRepository } from './ai.repository';
import { UserRepository } from 'src/user/user.repository';

@Injectable()
export class AiService {
Expand All @@ -20,6 +21,7 @@ export class AiService {
private readonly httpService: HttpService,
private readonly fileService: FileService,
private readonly aiRepository: AiRepository,
private readonly userRepository: UserRepository,
) {}

async getLookBookById(id: number) {
Expand All @@ -31,8 +33,6 @@ export class AiService {
}

async createLookBook(
gender: string,
ageRange: string,
{
province,
city,
Expand All @@ -41,14 +41,21 @@ export class AiService {
TPO: [string],
userUuid: string,
) {
this.logger.log('createLookBook called');
const userInfo = await this.userRepository.getUserInfo(userUuid);

if (!userInfo.gender || !userInfo.ageRange) {
throw new HttpException(
'UserInfo does not exist. Update User info first',
HttpStatus.NOT_FOUND,
);
}

try {
const response = await firstValueFrom(
this.httpService
.post(this.configService.get<string>('AI_URL')!, {
gender,
ageRange,
gender: userInfo.gender,
ageRange: userInfo.ageRange,
area: {
province,
city,
Expand Down
41 changes: 41 additions & 0 deletions src/user/dto/req/UpdateUserInfo.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';

enum GenderEnum {
female = '여자',
male = '남자',
none = '논바이너리',
}

enum AgeRangeEnum {
early_twenties = '20대 초반',
late_twenties = '20대 후반',
early_thirties = '30대 초반',
late_thirties = '30대 후반',
early_forties = '40대 초반',
late_forties = '40대 후반',
early_fifties = '50대 초반',
late_fifties = '50대 후반',
}

export class UpdateUserInfoDto {
@ApiProperty({
example: '남자',
description: '성별',
required: true,
enum: GenderEnum,
})
@IsString()
@IsNotEmpty()
gender: string;

@ApiProperty({
example: '20대 초반',
description: '나이',
required: true,
enum: AgeRangeEnum,
})
@IsString()
@IsNotEmpty()
ageRange: string;
}
41 changes: 41 additions & 0 deletions src/user/dto/res/GetUserInfo.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';

enum GenderEnum {
female = '여자',
male = '남자',
none = '논바이너리',
}

enum AgeRangeEnum {
early_twenties = '20대 초반',
late_twenties = '20대 후반',
early_thirties = '30대 초반',
late_thirties = '30대 후반',
early_forties = '40대 초반',
late_forties = '40대 후반',
early_fifties = '50대 초반',
late_fifties = '50대 후반',
}

export class GetUserInfoDto {
@ApiProperty({
example: '남자',
description: '성별',
required: true,
enum: GenderEnum,
})
@IsString()
@IsNotEmpty()
gender: string;

@ApiProperty({
example: '20대 초반',
description: '나이',
required: true,
enum: AgeRangeEnum,
})
@IsString()
@IsNotEmpty()
ageRange: string;
}
51 changes: 51 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Body, Controller, Get, Patch, UseGuards } from '@nestjs/common';
import {
ApiBearerAuth,
ApiOkResponse,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/auth/guard/jwt-auth.guard';
import { GetUser } from './decorator/get-user.decorator';
import { UserService } from './user.service';
import { UpdateUserInfoDto } from './dto/req/UpdateUserInfo.dto';
import { GetUserInfoDto } from './dto/res/GetUserInfo.dto';

@Controller('user')
@ApiTags('user')
export class UserController {
constructor(private readonly userService: UserService) {}

@ApiOperation({
summary: 'Get user info(gender, ageRange)',
description: 'Get user info(gender, ageRange)',
})
@ApiOkResponse({
type: GetUserInfoDto,
description: 'Return user info(gender, ageRange)',
})
@ApiBearerAuth()
@Get()
@UseGuards(JwtAuthGuard)
getUserInfo(@GetUser() userUuid: string) {
return this.userService.getUserInfo(userUuid);
}

@ApiOperation({
summary: 'Update user info(gender, ageRange)',
description: 'Update user info(gender, ageRange)',
})
@ApiOkResponse({
type: GetUserInfoDto,
description: 'Return updated user info(gender, ageRange)',
})
@ApiBearerAuth()
@Patch()
@UseGuards(JwtAuthGuard)
updateUserInfo(
@GetUser() userUuid: string,
@Body() { gender, ageRange }: UpdateUserInfoDto,
) {
return this.userService.updateUserInfo(gender, ageRange, userUuid);
}
}
5 changes: 4 additions & 1 deletion src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { Module } from '@nestjs/common';
import { PrismaModule } from 'src/prisma/prisma.module';
import { ConfigModule } from '@nestjs/config';
import { UserRepository } from './user.repository';
import { UserController } from './user.controller';
import { UserService } from './user.service';

@Module({
imports: [PrismaModule, ConfigModule],
providers: [UserRepository],
controllers: [UserController],
providers: [UserRepository, UserService],
exports: [UserRepository],
})
export class UserModule {}
46 changes: 45 additions & 1 deletion src/user/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Injectable, Logger } from '@nestjs/common';
import {
Injectable,
InternalServerErrorException,
Logger,
} from '@nestjs/common';
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
Expand All @@ -14,4 +19,43 @@ export class UserRepository {
},
});
}

async updateUserInfo(gender: string, ageRange: string, userUuid: string) {
this.logger.log('updateUserInfo called');
return this.prismaService.user
.update({
where: { uuid: userUuid },
data: {
gender,
ageRange,
},
select: {
gender: true,
ageRange: true,
},
})
.catch((error) => {
if (error instanceof PrismaClientKnownRequestError) {
this.logger.error('updateUserInfo error');
this.logger.debug(error);
throw new InternalServerErrorException('Database Error');
}
this.logger.error('updateUserInfo Unknown Error');
this.logger.debug(error);
throw new InternalServerErrorException('Unknown Error');
});
}

async getUserInfo(userUuid: string) {
this.logger.log('getUserInfo');
return this.prismaService.user.findUniqueOrThrow({
where: {
uuid: userUuid,
},
select: {
gender: true,
ageRange: true,
},
});
}
}
15 changes: 15 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Injectable } from '@nestjs/common';
import { UserRepository } from './user.repository';

@Injectable()
export class UserService {
constructor(private readonly userRepository: UserRepository) {}

async getUserInfo(userUuid: string) {
return await this.userRepository.getUserInfo(userUuid);
}

async updateUserInfo(gender: string, ageRange: string, userUuid: string) {
return await this.userRepository.updateUserInfo(gender, ageRange, userUuid);
}
}

0 comments on commit cea7650

Please sign in to comment.