Skip to content
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
273 changes: 268 additions & 5 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
"@nestjs/platform-express": "^8.0.0",
"@nestjs/typeorm": "^8.0.3",
"@types/jsonwebtoken": "^8.5.8",
"aws-sdk": "^2.1093.0",
"axios": "^0.26.1",
"class-validator": "^0.13.2",
"jsonwebtoken": "^8.5.1",
"md5": "^2.3.0",
"multer": "^1.4.4",
"multer-s3": "^2.10.0",
"mysql2": "^2.3.3",
"passport": "^0.5.2",
"passport-local": "^1.0.0",
Expand All @@ -51,6 +54,7 @@
"@types/express": "^4.17.13",
"@types/jest": "27.4.1",
"@types/md5": "^2.3.2",
"@types/multer": "^1.4.7",
"@types/node": "^16.0.0",
"@types/passport-local": "^1.0.34",
"@types/supertest": "^2.0.11",
Expand Down
1 change: 0 additions & 1 deletion src/socialLogin/dto/completeFirstLogin.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
IsEmail,
IsString,
IsNumber,
IsBoolean,
MaxLength,
MinLength,
} from 'class-validator';
Expand Down
1 change: 0 additions & 1 deletion src/user/dto/create-user.dto.ts

This file was deleted.

17 changes: 17 additions & 0 deletions src/user/dto/createUserProfile.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IsString, MaxLength, MinLength } from 'class-validator';

export class CreateUserProfileDTO {
@MaxLength(13)
@MinLength(4)
@IsString()
nickname: string;

@IsString()
technologyStack: string;

@IsString()
selfIntroduction: string;

@IsString()
portfolioUrl: string;
}
4 changes: 0 additions & 4 deletions src/user/dto/update-user.dto.ts

This file was deleted.

54 changes: 35 additions & 19 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
UseInterceptors,
UploadedFile,
Req,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { CreateUserProfileDTO } from './dto/createUserProfile.dto';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';

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

@Post()
create(@Body() createUserDto: CreateUserDto) {
return this.userService.create(createUserDto);
@UseInterceptors(FileInterceptor('profileImg'))
create(
@Req() user,
@UploadedFile() file: Express.Multer.File,
@Body() createUserProfileDto: CreateUserProfileDTO,
) {
return this.userService.createUserProfile(
'profileImg',
user.userId,
file,
createUserProfileDto,
);
}

@Get()
findAll() {
return this.userService.findAll();
@Get('/profile')
async getProfile(@Req() user) {
return this.userService.getMyProfile(user.userId);
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.userService.findOne(+id);
@Get('/love')
getLovePosts(@Req() user) {
return this.userService.getLovePosts(user.userId);
}

@Patch(':id')
update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
return this.userService.update(+id, updateUserDto);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.userService.remove(+id);
@Get('/keep')
getKeepPosts(@Req() user) {
return this.userService.getKeepPosts(user.userId);
}
}
9 changes: 8 additions & 1 deletion src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { UserService } from './user.service';
import { UserController } from './user.controller';
import { Notification } from './entities/Notification';
import { UserReputation } from './entities/UserReputation';
import { ConfigModule } from '@nestjs/config';
import { Users } from 'src/socialLogin/entity/Users';

@Module({
imports: [TypeOrmModule.forFeature([Notification, UserReputation])],
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
TypeOrmModule.forFeature([Notification, UserReputation, Users]),
],
controllers: [UserController],
providers: [UserService],
})
Expand Down
131 changes: 116 additions & 15 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,127 @@
import { Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';

import { BadRequestException, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { Users } from 'src/socialLogin/entity/Users';
import { Repository } from 'typeorm';
import { CreateUserProfileDTO } from './dto/createUserProfile.dto';
import { UserReputation } from './entities/UserReputation';
import * as AWS from 'aws-sdk';
import path from 'path';
@Injectable()
export class UserService {
create(createUserDto: CreateUserDto) {
return 'This action adds a new user';
private readonly awsS3: AWS.S3;
public readonly S3_BUCKET_NAME: string;
constructor(
private readonly configService: ConfigService,
@InjectRepository(Users) private readonly userRepository: Repository<Users>,
@InjectRepository(UserReputation)
private readonly userReputation: Repository<UserReputation>,
) {
this.awsS3 = new AWS.S3({
accessKeyId: this.configService.get('AWS_ACCESS_KEY_ID'),
secretAccessKey: this.configService.get('AWS_SECRET_ACCESS_KEY'),
region: this.configService.get('AWS_REGION'),
});
this.S3_BUCKET_NAME = this.configService.get('AWS_S3_BUCKET_NAME');
}

findAll() {
return `This action returns all user`;
}
async createUserProfile(
folder: string,
userId: string,
file: Express.Multer.File,
createUserProfileDto: CreateUserProfileDTO,
) {
// 사진을 s3에 넣는 로직.. 먼저 DB profileImg 컬럼에 변수 key값을 넣는다.
try {
const key = `${folder}/${Date.now()}_${path.basename(
file.originalname,
)}`.replace(/ /g, '');
if (key) {
await this.userRepository
.createQueryBuilder('user')
.update(Users)
.set({
nickname: createUserProfileDto.nickname,
portfolioUrl: createUserProfileDto.portfolioUrl,
profileImgUrl: key,
selfIntroduction: createUserProfileDto.selfIntroduction,
technologyStack: createUserProfileDto.technologyStack,
})
.where('userId=:userId', { userId })
.execute();

findOne(id: number) {
return `This action returns a #${id} user`;
await this.awsS3
.putObject({
Bucket: this.S3_BUCKET_NAME,
Key: key,
Body: file.buffer,
ACL: 'public-read',
ContentType: file.mimetype,
})
.promise();
}
} catch (e) {
throw new BadRequestException(`File upload failed : ${e}`);
}
}

update(id: number, updateUserDto: UpdateUserDto) {
return `This action updates a #${id} user`;
async getLovePosts(userId: string) {
const getUser = await this.userRepository.createQueryBuilder('user');
const lovePosts = getUser.leftJoinAndSelect(
'user.informationLoves',
'informationLoves',
);

const totalLovePosts = [];

const informationPostIdOfLoves = getUser
.select(['informationLoves.informationPostId'])
.leftJoinAndSelect(
'informationLoves.informationPosts',
'informationPosts',
)
.where('user.userId=:userId', { userId })
.getRawMany();

for (const id in informationPostIdOfLoves) {
totalLovePosts.push(
lovePosts
.select(['informationPosts.title', 'informationPosts.createdAt'])
.where('informationPosts.informationPostId=:informationPostId', {
inforationPostId: id,
}),
);
}
return totalLovePosts;
}

remove(id: number) {
return `This action removes a #${id} user`;
async getKeepPosts(userId: string) {
// 먼저 recruitKeeps 테이블의 recruitPostId를 뽑아낸다. 그리고 그 값들을 반복문을 통해 where문 조건값에 넣는다.
const getUser = await this.userRepository.createQueryBuilder('user');
const keepPosts = getUser.leftJoinAndSelect(
'user.recruitKeeps',
'recruitKeeps',
);
const totalKeepPosts = [];

const recruitPostIdOfKeeps = getUser
.select(['recruitKeep.recruitPostId'])
.leftJoinAndSelect('recruitKeeps.recruitPosts', 'recruitPosts')
.where('user.userId=:userId', { userId })
.getRawMany();

for (const id in recruitPostIdOfKeeps) {
totalKeepPosts.push(
keepPosts
.select(['recruitPosts.title', 'recruitPosts.createdAt'])
.where('recruitPosts.recruitPostId=:recruitPostId', {
recruitPostId: id,
}),
);
}

return totalKeepPosts;
}

async getMyProfile(userId: string) {}
}