Skip to content

Commit

Permalink
Merge pull request #120 from DEV-dsm/modifyUpdateProfile
Browse files Browse the repository at this point in the history
(#119) πŸ‘€ :: chore :: οΏ½modify update profile
  • Loading branch information
soyeonkim0227 authored Dec 14, 2023
2 parents d3cbe79 + 61840d4 commit 3fdbd89
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsString } from "class-validator";

export class searchProfileDto {
export class SearchProfileDto {
@ApiProperty({
example: "name or number",
description: "학생, ꡐ사 쀑 μ„ νƒν•΄μ„œ 검색"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsOptional, IsString } from "class-validator";

export class StudentProfileDto {
export class UpdateStudentProfileDto {
@ApiProperty({
example: "",
description: "μ»€μŠ€ν…€ 아이디",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,70 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsString } from "class-validator";
import { IsOptional, IsString } from "class-validator";

export class TeacherProfileDto {
export class UpdateTeacherProfileDto {

@ApiProperty({
example: "osj",
description: "μ»€μŠ€ν…€ 아이디"
})
@IsString()
identify: string;
@IsOptional()
identify?: string;

@ApiProperty({
example: "μ˜€μƒμ§„",
description: "이름"
})
@IsString()
@IsOptional()
name?: string;

@ApiProperty({
example: "[email protected]",
description: "이메일"
})
@IsString()
email: string;
@IsOptional()
email?: string;

@ApiProperty({
example: "asdfasdf",
description: "ν”„λ‘œν•„ 사진 파일 경둜"
})
@IsString()
profile: string;
@IsOptional()
profile?: string;

@ApiProperty({
example: "asdfasdf",
description: "λ°°κ²½ 사진 파일 경둜"
})
@IsString()
background: string;
@IsOptional()
background?: string;

@ApiProperty({
example: "2μΈ΅ λ³ΈλΆ€ ꡐ무싀",
description: "ꡐ무싀 μœ„μΉ˜"
})
@IsString()
location: string;
@IsOptional()
location?: string;

@ApiProperty({
example: "κ³Όν•™",
description: "λ‹΄λ‹Ή κ³Όλͺ©"
})
@IsString()
subject: string;
@IsOptional()
subject?: string;

@ApiProperty({
example: "λ§ˆμ΄μŠ€ν„° λΆ€μž₯",
description: "λ‹΄λ‹Ή 직무"
})
@IsString()
duty: string;
@IsOptional()
duty?: string;

}
34 changes: 16 additions & 18 deletions src/profile/profile.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Body, Controller, Get, Headers, Param, Patch, Post, UseFilters } from '@nestjs/common';
import { ApiBadRequestResponse, ApiBody, ApiConflictResponse, ApiHeader, ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiParam, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { ApiBadRequestResponse, ApiBody, ApiConflictResponse, ApiForbiddenResponse, ApiHeader, ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiParam, ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { HttpExceptionFilter } from 'src/filter/httpException.filter';
import { searchProfileDto } from 'src/user/dto/searchProfile.dto';
import { StudentProfileDto } from 'src/user/dto/studentProfile.dto';
import { TeacherProfileDto } from 'src/user/dto/teacherProfile.dto';
import { SearchProfileDto } from 'src/profile/dto/searchProfile.dto';
import { UpdateStudentProfileDto } from './dto/updateStudentProfile.dto';
import { UpdateTeacherProfileDto } from './dto/updateTeacherProfile.dto';
import { ProfileService } from './profile.service';

@ApiTags('ν”„λ‘œν•„ API')
Expand All @@ -21,7 +21,7 @@ export class ProfileController {
description: "학생 ν”„λ‘œν•„ μˆ˜μ •ν•˜κΈ°"
})
@ApiHeader({ name: 'authorization', required: true })
@ApiBody({ type: StudentProfileDto })
@ApiBody({ type: UpdateStudentProfileDto })
@ApiOkResponse({
status: 200,
description: "학생 ν”„λ‘œν•„ μˆ˜μ • μ™„λ£Œ"
Expand All @@ -30,9 +30,9 @@ export class ProfileController {
status: 401,
description: "μ•‘μ„ΈμŠ€ 토큰 검증 μ‹€νŒ¨"
})
@ApiConflictResponse({
status: 409,
description: "학생 μ „μš© API에 μ„ μƒλ‹˜μ΄ μš”μ²­μ„ 보냄"
@ApiForbiddenResponse({
status: 403,
description: "학생 μ „μš© API에 ꡐ사가 μš”μ²­μ„ 보냄"
})
@ApiConflictResponse({
status: 409,
Expand All @@ -41,12 +41,11 @@ export class ProfileController {
@Patch('student')
async updateStudentProfile(
@Headers('authorization') accesstoken: string,
@Body() StudentProfileDto: StudentProfileDto
@Body() studentProfileDto: UpdateStudentProfileDto
): Promise<object> {
const data = await this.profileService.patchStudentProfile(accesstoken, StudentProfileDto);
await this.profileService.patchStudentProfile(accesstoken, studentProfileDto);

return {
data,
statusCode: 200,
statusMsg: "ν”„λ‘œν•„ μˆ˜μ •μ— μ„±κ³΅ν–ˆμŠ΅λ‹ˆλ‹€."
};
Expand Down Expand Up @@ -101,7 +100,7 @@ export class ProfileController {

@ApiOperation({ summary: "ꡐ사 ν”„λ‘œν•„ μˆ˜μ • API", description: "ꡐ사 ν”„λ‘œν•„ μˆ˜μ •" })
@ApiHeader({ name: "authorization", required: true })
@ApiBody({ type: TeacherProfileDto })
@ApiBody({ type: UpdateTeacherProfileDto })
@ApiOkResponse({
status: 200,
description: "ꡐ사 ν”„λ‘œν•„ μˆ˜μ • μ™„λ£Œ"
Expand All @@ -110,8 +109,8 @@ export class ProfileController {
status: 401,
description: "μ•‘μ„ΈμŠ€ 토큰 검증 μ‹€νŒ¨"
})
@ApiConflictResponse({
status: 409,
@ApiForbiddenResponse({
status: 403,
description: "ꡐ사 μ „μš© API에 학생이 μš”μ²­μ„ 보냄"
})
@ApiConflictResponse({
Expand All @@ -121,12 +120,11 @@ export class ProfileController {
@Patch('teacher')
async updateTeacherProfile(
@Headers('authorization') accesstoken: string,
@Body() teacherProfile: TeacherProfileDto
@Body() teacherProfile: UpdateTeacherProfileDto
): Promise<object> {
const data = await this.profileService.patchTeacherProfile(accesstoken, teacherProfile);
await this.profileService.patchTeacherProfile(accesstoken, teacherProfile);

return {
data,
statusCode: 200,
statusMsg: "ꡐ사 개인 ν”„λ‘œν•„ μˆ˜μ •μ΄ μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€."
};
Expand Down Expand Up @@ -202,7 +200,7 @@ export class ProfileController {
async searchProfileList(
@Headers('authorization') accesstoken: string,
@Param('isStudent') isStudent: boolean,
@Body() searchProfile: searchProfileDto
@Body() searchProfile: SearchProfileDto
): Promise<object> {
const data = await this.profileService.searchProfileList(accesstoken, isStudent, searchProfile);

Expand Down
55 changes: 27 additions & 28 deletions src/profile/profile.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import { BadRequestException, ConflictException, Injectable, NotFoundException, UseFilters } from '@nestjs/common';
import { BadRequestException, ConflictException, ForbiddenException, Injectable, NotFoundException, UseFilters } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Redis } from 'ioredis';
import { HttpExceptionFilter } from 'src/filter/httpException.filter';
import { searchProfileDto } from 'src/user/dto/searchProfile.dto';
import { StudentProfileDto } from 'src/user/dto/studentProfile.dto';
import { TeacherProfileDto } from 'src/user/dto/teacherProfile.dto';
import { SearchProfileDto } from 'src/profile/dto/searchProfile.dto';
import { Student } from 'src/user/entities/student.entity';
import { Teacher } from 'src/user/entities/teacher.entity';
import { User } from 'src/user/entities/user.entity';
import { UserService } from 'src/user/user.service';
import { Repository } from 'typeorm';
import { UpdateStudentProfileDto } from './dto/updateStudentProfile.dto';
import { UpdateTeacherProfileDto } from './dto/updateTeacherProfile.dto';

@UseFilters(new HttpExceptionFilter())
@Injectable()
Expand Down Expand Up @@ -68,18 +68,18 @@ export class ProfileService {
/**
*
* @param accesstoken
* @param studentProfileDto
* @param studentProfileDto
* @returns
*
* 학생 ν”„λ‘œν•„ μˆ˜μ •
*/
async patchStudentProfile(accesstoken: string, studentProfileDto: StudentProfileDto): Promise<object> {
async patchStudentProfile(accesstoken: string, studentProfileDto: UpdateStudentProfileDto) {
const { userID } = await this.userService.validateAccess(accesstoken);

const thisUser = await this.userEntity.findOneBy({ userID });
const thisStudent = await this.studentEntity.findOneBy({ userID });

if(!thisUser.isStudent) throw new ConflictException('이 APIλŠ” 학생 μ „μš©μž…λ‹ˆλ‹€.')
if(!thisUser.isStudent) throw new ForbiddenException('이 APIλŠ” 학생 μ „μš©μž…λ‹ˆλ‹€.')

const identify = studentProfileDto.identify ?? thisUser.identify;
const name = studentProfileDto.name ?? thisUser.name;
Expand All @@ -93,7 +93,7 @@ export class ProfileService {
if (await this.userEntity.findOneBy({ identify }) && (identify != thisUser.identify)) throw new ConflictException('아이디 쀑볡');
if (await this.userEntity.findOneBy({ email }) && (email != thisUser.email)) throw new ConflictException('이메일 쀑볡');

const updatedUser = await this.userEntity.update({
await this.userEntity.update({
userID
}, {
identify,
Expand All @@ -103,18 +103,13 @@ export class ProfileService {
background
})

const updatedStudent = await this.studentEntity.update({
await this.studentEntity.update({
userID
}, {
major,
github,
number
})

return {
updatedUser,
updatedStudent
}
}

/**
Expand All @@ -125,39 +120,43 @@ export class ProfileService {
*
* ꡐ사 ν”„λ‘œν•„ μˆ˜μ •
*/
async patchTeacherProfile(accesstoken: string, teacherProfileDto: TeacherProfileDto): Promise<object> {
async patchTeacherProfile(accesstoken: string, teacherProfileDto: UpdateTeacherProfileDto) {
const { userID } = await this.userService.validateAccess(accesstoken);

const user = await this.userEntity.findOneBy({ userID });
const thisUser = await this.userEntity.findOneBy({ userID });
const thisTeacher = await this.teacherEntity.findOneBy({ userID });

if (user.isStudent) throw new ConflictException('이 APIλŠ” ꡐ사 μ „μš©μž…λ‹ˆλ‹€.');
if (thisUser.isStudent) throw new ForbiddenException('이 APIλŠ” ꡐ사 μ „μš©μž…λ‹ˆλ‹€.');

const { identify, email, profile, background, location, subject, duty } = teacherProfileDto;
const identify = teacherProfileDto.identify ?? thisUser.identify;
const name = teacherProfileDto.name ?? thisUser.name;
const email = teacherProfileDto.email ?? thisUser.email;
const profile = teacherProfileDto.profile ?? thisUser.profile;
const background = teacherProfileDto.background ?? thisUser.background;
const location = teacherProfileDto.location ?? thisTeacher.location;
const subject = teacherProfileDto.subject ?? thisTeacher.subject;
const duty = teacherProfileDto.duty ?? thisTeacher.duty

if (await this.userEntity.findOneBy({ identify })) throw new ConflictException('아이디 쀑볡');
if (await this.userEntity.findOneBy({ email })) throw new ConflictException('이메일 쀑볡');
if (await this.userEntity.findOneBy({ identify }) && (identify != thisUser.identify)) throw new ConflictException('아이디 쀑볡');
if (await this.userEntity.findOneBy({ email }) && (email != thisUser.email)) throw new ConflictException('이메일 쀑볡');

const updateUser = await this.userEntity.update({
await this.userEntity.update({
userID
}, {
identify,
name,
email,
profile,
background
});

const updateTeacher = await this.teacherEntity.update({
await this.teacherEntity.update({
userID
}, {
location,
subject,
duty
});

return {
updateUser,
updateTeacher
}
}

/**
Expand Down Expand Up @@ -212,7 +211,7 @@ export class ProfileService {
*
* μœ μ € 검색
*/
async searchProfileList(accesstoken: string, isStudent: boolean, searchProfileDto: searchProfileDto): Promise<object> {
async searchProfileList(accesstoken: string, isStudent: boolean, searchProfileDto: SearchProfileDto): Promise<object> {
const { standard, keyword } = searchProfileDto;
if(standard != 'number' && standard != 'name') throw new BadRequestException('잘λͺ»λœ μš”μ²­');

Expand Down

0 comments on commit 3fdbd89

Please sign in to comment.