Skip to content

Commit

Permalink
Merge pull request #8 from Deumjubukeo/origin/feature/profile
Browse files Browse the repository at this point in the history
Origin/feature/profile + img upload
  • Loading branch information
tomaho1756 authored Nov 25, 2024
2 parents 4865904 + 6dc13c3 commit 31dc5c1
Show file tree
Hide file tree
Showing 15 changed files with 2,183 additions and 1,994 deletions.
3,897 changes: 1,929 additions & 1,968 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.699.0",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.3.0",
"@nestjs/core": "^10.0.0",
Expand All @@ -34,6 +35,8 @@
"@types/cookie-parser": "^1.4.7",
"@types/passport-jwt": "^4.0.1",
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"cookie-parser": "^1.4.7",
"express": "^4.21.1",
"joi": "^17.13.3",
Expand All @@ -45,14 +48,16 @@
"rxjs": "^7.8.1",
"socket.io": "^4.8.1",
"swagger-ui-express": "^5.0.1",
"typeorm": "^0.3.20"
"typeorm": "^0.3.20",
"uuid": "^11.0.3"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/multer": "^1.4.12",
"@types/node": "^20.3.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
Expand Down
10 changes: 10 additions & 0 deletions src/aws/aws.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { AwsService } from './aws.service';
import { ConfigModule } from '@nestjs/config';

@Module({
imports: [ConfigModule],
providers: [AwsService],
exports: [AwsService],
})
export class AwsModule {}
31 changes: 31 additions & 0 deletions src/aws/aws.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';

@Injectable()
export class AwsService {
s3Client: S3Client;

constructor(private configService: ConfigService) {
this.s3Client = new S3Client({
region: this.configService.get('AWS_REGION'),
});
}

async imageUploadToS3(
fileName: string,
file: Express.Multer.File,
ext: string,
) {
const command = new PutObjectCommand({
Bucket: this.configService.get('AWS_S3_BUCKET_NAME'),
Key: fileName,
Body: file.buffer,
ACL: 'public-read',
ContentType: `image/${ext}`,
});

await this.s3Client.send(command);
return `https://s3.${process.env.AWS_REGION}.amazonaws.com/${process.env.AWS_S3_BUCKET_NAME}/${fileName}`;
}
}
18 changes: 11 additions & 7 deletions src/goods/dto/createGoods.dto.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { ApiProperty } from '@nestjs/swagger';
// @ts-ignore
import { IsString, IsNumber, IsOptional } from 'class-validator';

export class CreateGoodsDto {
@ApiProperty({ description: '제목' })
@IsString()
title: string;

@ApiProperty({ description: '내용' })
@IsString()
content: string;

@ApiProperty({ description: '가격' })
pize: number;
@IsNumber()
price: number;

@ApiProperty({ description: '이미지' })
imageUrl: string;
@IsOptional()
imageUrl?: string;

@IsNumber()
purchaseCount: number;
}
3 changes: 3 additions & 0 deletions src/goods/goods.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Delete,
HttpCode,
Param,
UploadedFile,
} from '@nestjs/common';
import { GoodsService } from './goods.service';
import { CreateGoodsDto } from './dto/createGoods.dto';
Expand All @@ -32,10 +33,12 @@ export class GoodsController {
@Body() createGoodsDto: CreateGoodsDto,
@Req() request,
@Res() response: Response,
@UploadedFile() file: Express.Multer.File,
) {
const newGoods = await this.goodsService.createGoods(
createGoodsDto,
request.user,
file,
);
return response.status(201).json(newGoods);
}
Expand Down
2 changes: 1 addition & 1 deletion src/goods/goods.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Goods {
content: string;

@Column({ default: 0 })
prize: number;
price: number;

@Column()
imageUrl: string;
Expand Down
2 changes: 2 additions & 0 deletions src/goods/goods.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { Goods } from './goods.entity';
import { GoodsController } from './goods.controller';
import { AuthModule } from '../auth/auth.module';
import { UsersModule } from '../users/users.module';
import { AwsModule } from '../aws/aws.module';

@Module({
imports: [
TypeOrmModule.forFeature([Goods]),
forwardRef(() => AuthModule),
forwardRef(() => UsersModule),
forwardRef(() => AwsModule),
],
providers: [GoodsService],
exports: [GoodsService],
Expand Down
26 changes: 22 additions & 4 deletions src/goods/goods.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Repository } from 'typeorm';
import { Goods } from './goods.entity';
import { CreateGoodsDto } from './dto/createGoods.dto';
import { User } from '../users/user.entity';
import * as path from 'node:path';
import { AwsService } from '../aws/aws.service';

@Injectable()
export class GoodsService {
Expand All @@ -18,20 +20,36 @@ export class GoodsService {
private goodsRepository: Repository<Goods>,
@InjectRepository(User)
private userRepository: Repository<User>,
private readonly awsService: AwsService,
) {}

// 상품 생성
async createGoods(
createGoodsDto: CreateGoodsDto,
user: User,
file: Express.Multer.File, // 파일 추가
): Promise<Goods> {
try {
//어드민인지 아닌지 확인하는 로직 아직 어드민 카테고리 없어서 임시로 !user해둠
// 관리자 권한 확인
if (!user) {
throw new UnauthorizedException('관리자만 상품을 생성할 수 있습니다.');
}

const goods = this.goodsRepository.create(createGoodsDto);
// 이미지 업로드
const ext = path.extname(file.originalname).slice(1); // 확장자 추출
const fileName = `${Date.now()}_${file.originalname}`; // 파일 이름 생성
const imageUrl = await this.awsService.imageUploadToS3(
fileName,
file,
ext,
); // S3에 이미지 업로드

// 상품 생성
const goods = this.goodsRepository.create({
...createGoodsDto,
imageUrl, // S3에서 받은 URL 저장
});

return this.goodsRepository.save(goods);
} catch (error) {
console.log(error);
Expand All @@ -51,11 +69,11 @@ export class GoodsService {
if (!user) {
throw new NotFoundException('사용자를 찾을 수 없습니다.');
}
if (user.point < goods.prize) {
if (user.point < goods.price) {
throw new UnauthorizedException('포인트가 부족합니다.');
}

user.point -= goods.prize;
user.point -= goods.price;
goods.purchaseCount += 1;

await this.userRepository.save(user);
Expand Down
18 changes: 16 additions & 2 deletions src/lostStuff/lostStuff.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { Controller, Get, Post, Body, Param, Patch, Delete, UseGuards, Req, Query } from "@nestjs/common";
import {
Controller,
Get,
Post,
Body,
Param,
Patch,
Delete,
UseGuards,
Req,
Query,
} from '@nestjs/common';
import { LostStuffService } from './lostStuff.service';
import { CreateLostStuffDto } from './dto/createLostStuff.dto';
import { UpdateLostStuffDto } from './dto/updateLostStuff.dto';
Expand Down Expand Up @@ -30,7 +41,10 @@ export class LostStuffController {

@UseGuards(AuthGuard)
@Patch(':id')
async update(@Param('id') id: number, @Body() updateLostStuffDto: UpdateLostStuffDto) {
async update(
@Param('id') id: number,
@Body() updateLostStuffDto: UpdateLostStuffDto,
) {
return this.lostStuffService.update(id, updateLostStuffDto);
}

Expand Down
1 change: 1 addition & 0 deletions src/users/dto/updateUser.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import RegisterDto from '../../auth/dto/register.dto';
export class UpdateUserDto extends PartialType(RegisterDto) {
point: number;
temperature: number;
imageUrl?: string
}
2 changes: 1 addition & 1 deletion src/users/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
import { Column, Entity, PrimaryColumn, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User {
Expand Down
Loading

0 comments on commit 31dc5c1

Please sign in to comment.