Skip to content

Commit

Permalink
Merge pull request #121 from Arios67/feat/#120
Browse files Browse the repository at this point in the history
[#120]feat: 찜 기능 수정
  • Loading branch information
Daseul1 committed Apr 3, 2022
2 parents 535380f + bbecb9d commit 394c3c7
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 25 deletions.
2 changes: 1 addition & 1 deletion ars/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ services:
cap_add:
- SYS_NICE
ports:
- 3307:3306
- 3306:3306

my_redis:
image: redis:6.2.6
Expand Down
15 changes: 12 additions & 3 deletions ars/src/apis/art/entities/likeArt.entity.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { ObjectType } from '@nestjs/graphql';
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';

import { Field, ObjectType } from '@nestjs/graphql';
import { User } from 'src/apis/user/entities/user.entity';
import {
Column,
Entity,
ManyToOne,
PrimaryColumn,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Art } from './art.entity';

@Entity()
@ObjectType()
export class LikeArt {
@PrimaryGeneratedColumn()
@Field(() => String)
id: string;

@Column()
userId: string;

@ManyToOne(() => Art, { eager: true })
@ManyToOne(() => Art)
art: Art;
}
3 changes: 2 additions & 1 deletion ars/src/apis/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export class AuthService {
async loginOAuth(req, res) {
let email = req.user.email;
let user = await this.userService.findOne(email);
if (!user) {

if (!user || !user.phoneNum) {
const { password, ...rest } = req.user;
const hashedPassword = await bcrypt.hash(String(password), 1);
const createUser = { ...rest, password: hashedPassword };
Expand Down
59 changes: 42 additions & 17 deletions ars/src/apis/likeArt/likeArt.service.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,74 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Connection, Like, Repository } from 'typeorm';
import { Art } from '../art/entities/art.entity';
import { LikeArt } from '../art/entities/likeArt.entity';

@Injectable()
export class LikeArtService {
constructor(
@InjectRepository(LikeArt)
private readonly likeArtRepository: Repository<LikeArt>,

private readonly connection: Connection,
) {}

async find(userId, page) {
let arts = [];
if (page) {
arts = await this.likeArtRepository.find({
take: 10,
skip: 10 * (page - 1),
where: { userId: userId },
});
} else {
arts = await this.likeArtRepository.find({ userId });
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
let arts = [];
if (page) {
arts = await queryRunner.manager.find(Art, {
take: 10,
skip: 10 * (page - 1),
where: { userId: userId },
});
} else {
arts = await queryRunner.manager.find(LikeArt, {
where: { userId },
relations: ['art'],
});
}
return arts.map((ele) => ele.art);
} catch (error) {
await queryRunner.rollbackTransaction();
throw error + 'like art!!!';
} finally {
await queryRunner.release();
}

return arts.map((ele) => ele.art);
}

async like(artId, userId) {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const prevLike = await this.likeArtRepository.findOne({
const art = await queryRunner.manager.findOne(Art, { id: artId });
const prevLike = await queryRunner.manager.findOne(LikeArt, {
where: {
userId: userId,
art: artId,
},
});
if (!prevLike) {
await this.likeArtRepository.save({
await queryRunner.manager.save(LikeArt, {
userId: userId,
art: artId,
art: art,
});
await queryRunner.commitTransaction();
return true;
} else {
await this.likeArtRepository.delete({ art: artId });
await queryRunner.manager.delete(LikeArt, { art: artId });
await queryRunner.commitTransaction();
return false;
}
return true;
} catch (error) {
await queryRunner.rollbackTransaction();
throw error + 'like art!!!';
} finally {
await queryRunner.release();
}
}
}
6 changes: 3 additions & 3 deletions ars/src/apis/payment/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ export class PaymentService {
});

// 유저 누적 포인트 업데이트
const updated = await queryRunner.manager.save(User, {
const updatedUser = await queryRunner.manager.save(User, {
...bidder,
point: bidder.point - price,
});

// 히스토리 테이블(낙찰자) 저장
await queryRunner.manager.save(History, {
point: price,
balance: updated.point,
user: updated,
balance: updatedUser.point,
user: updatedUser,
payment: payment,
});

Expand Down
1 change: 1 addition & 0 deletions ars/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ProfileModule } from './apis/profile/profile.module';
import { HistoryModule } from './apis/history/history.module';
import { ScheduleModule } from '@nestjs/schedule';
import { PaymentModule } from './apis/payment/payment.module';
import { setgroups } from 'process';

@Module({
imports: [
Expand Down

0 comments on commit 394c3c7

Please sign in to comment.