Skip to content

Commit

Permalink
Merge pull request #82 from Arios67/feat/#81
Browse files Browse the repository at this point in the history
[#81]feat: 경매 참여 중인 작품 구현
  • Loading branch information
Arios67 committed Mar 30, 2022
2 parents 2ccf93a + f122662 commit df0d3a7
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 42 deletions.
8 changes: 2 additions & 6 deletions ars/src/apis/art/entities/art.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export class Art {

@CreateDateColumn()
@Field(() => Date)
createdAt: string;
createdAt: Date;

@DeleteDateColumn()
@Field(() => Date)
deletedAt: string;
deletedAt: Date;

@Column()
@Field(() => Date)
Expand All @@ -58,10 +58,6 @@ export class Art {
@Field(() => Boolean)
is_soldout: boolean;

@DeleteDateColumn()
@Field(() => Date)
deletedAt: Date;

@ManyToOne(() => User, { eager: true })
@Field(() => User)
user: User;
Expand Down
17 changes: 17 additions & 0 deletions ars/src/apis/engage/entities/engage.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { Art } from 'src/apis/art/entities/art.entity';
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
@ObjectType()
export class Engage {
@PrimaryGeneratedColumn()
id: string;

@Column()
userId: string;

@ManyToOne(() => Art, { eager: true })
@Field(() => Art)
art: Art;
}
7 changes: 6 additions & 1 deletion ars/src/apis/likeArt/likeArt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export class LikeArtService {

async like(artId, userId) {
try {
const prevLike = await this.likeArtRepository.findOne({ art: artId });
const prevLike = await this.likeArtRepository.findOne({
where: {
userId: userId,
art: artId,
},
});
if (!prevLike) {
await this.likeArtRepository.save({
userId: userId,
Expand Down
2 changes: 2 additions & 0 deletions ars/src/apis/payment/payment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { ArtService } from '../art/art.service';
import { Art } from '../art/entities/art.entity';
import { ArtImage } from '../artImage/entities/artImage.entity';
import { Engage } from '../engage/entities/engage.entity';
import { User } from '../user/entities/user.entity';
import { UserService } from '../user/user.service';
import { Payment } from './entities/payment.entity';
Expand All @@ -16,6 +17,7 @@ import { PaymentServie } from './payment.service';
ArtImage,
User,
Payment,
Engage,
]),
],
providers: [
Expand Down
20 changes: 19 additions & 1 deletion ars/src/apis/payment/payment.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { Cache } from 'cache-manager';
import { GqlAuthAccessGuard } from 'src/common/auth/gql-auth.guard';
import { CurrentUser, ICurrentUser } from 'src/common/auth/gql-user.param';
import { Engage } from '../engage/entities/engage.entity';
import { UserService } from '../user/user.service';
import { Payment } from './entities/payment.entity';
import { PaymentServie } from './payment.service';
Expand All @@ -23,11 +24,16 @@ export class PaymentResolver {
return await this.paymentService.find(currentUser.id);
}

@UseGuards(GqlAuthAccessGuard)
@Query(() => [Engage])
async fetchEngaging(@CurrentUser() currentUser: ICurrentUser) {
return await this.paymentService.findEngage(currentUser.id);
}

@Mutation(() => String)
async checkTimedoutAndProcess() {
try {
const arts = await this.paymentService.checkTimeout();
console.log(arts, '작품들');
arts.map(async (e) => {
const bidInfo = await this.cacheManager.get(e.id);
const price = bidInfo[0];
Expand Down Expand Up @@ -63,6 +69,7 @@ export class PaymentResolver {
return 'artId';
}

// 입찰 API(임시)
@UseGuards(GqlAuthAccessGuard)
@Mutation(() => [String])
async Bid(
Expand All @@ -72,4 +79,15 @@ export class PaymentResolver {
) {
return await this.paymentService.call(artId, bid_price, currentUser.email);
}

@UseGuards(GqlAuthAccessGuard)
@Mutation(() => String)
async saveBid(
@Args('artId') artId: string,
@CurrentUser() currentUser: ICurrentUser,
) {
console.log(currentUser.id);
await this.paymentService.save(artId, currentUser.id);
return 'ok';
}
}
37 changes: 36 additions & 1 deletion ars/src/apis/payment/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Cache } from 'cache-manager';
import { Connection, LessThan, Repository } from 'typeorm';
import { Art } from '../art/entities/art.entity';
import { Engage } from '../engage/entities/engage.entity';
import { History } from '../history/entities/history.entity';
import { User } from '../user/entities/user.entity';
import { Payment } from './entities/payment.entity';
Expand All @@ -16,6 +17,9 @@ export class PaymentServie {
@InjectRepository(Payment)
private readonly paymentRepository: Repository<Payment>,

@InjectRepository(Engage)
private readonly engageRepository: Repository<Engage>,

@Inject(CACHE_MANAGER)
private readonly cacheManager: Cache,

Expand Down Expand Up @@ -53,6 +57,7 @@ export class PaymentServie {
is_soldout: true,
});
await queryRunner.manager.softDelete(Art, { id: artId });
await queryRunner.manager.delete(Engage, { art: artId });

// 낙찰 테이블 저장
const payment = await queryRunner.manager.save(Payment, {
Expand Down Expand Up @@ -99,7 +104,6 @@ export class PaymentServie {
async call(artId, bid_price, email) {
const art = await this.artRepository.findOne(artId);
const time = Number(art.deadline) - Number(new Date());
console.log(time);
if (time > 0) {
await this.cacheManager.set(artId, [bid_price, email], {
ttl: time + 60000,
Expand All @@ -108,8 +112,39 @@ export class PaymentServie {
return [bid_price, email];
}

async save(artId, userId) {
const queryRunner = this.connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
const prevEngage = await queryRunner.manager.findOne(Engage, {
where: {
userId: userId,
art: artId,
},
});

if (!prevEngage)
await queryRunner.manager.save(Engage, {
userId: userId,
art: artId,
});
await queryRunner.commitTransaction();
} catch (error) {
await queryRunner.rollbackTransaction();
throw error + 'saveBid';
} finally {
await queryRunner.release();
}
}

// 구매한 작품 목록 조회
async find(userId) {
return await this.paymentRepository.find({ user: userId });
}

// 경매 참여 중인 작품 조회
async findEngage(userId) {
return await this.engageRepository.find({ userId: userId });
}
}
21 changes: 0 additions & 21 deletions ars/src/apis/user/entities/phoneToken.entity.ts

This file was deleted.

9 changes: 1 addition & 8 deletions ars/src/apis/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import {
Column,
Entity,
JoinColumn,
OneToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Column, Entity, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Field, Int, ObjectType } from '@nestjs/graphql';
import { Profile } from 'src/apis/profile/entities/profile.entity';
import { History } from 'src/apis/history/entities/history.entity';

@Entity()
@ObjectType()
Expand Down
2 changes: 0 additions & 2 deletions ars/src/apis/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Profile } from '../profile/entities/profile.entity';
import { PhoneToken } from './entities/phoneToken.entity';
import { User } from './entities/user.entity';
import { UserResolver } from './user.resolver';
import { UserService } from './user.service';
Expand All @@ -11,7 +10,6 @@ import { UserService } from './user.service';
TypeOrmModule.forFeature([
User, //
Profile,
PhoneToken,
]),
],
providers: [
Expand Down
2 changes: 1 addition & 1 deletion ars/src/apis/user/user.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Args, Context, Mutation, Query, Resolver } from '@nestjs/graphql';
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { CreateUserInput } from './dto/createUserInput';
import { User } from './entities/user.entity';
import { UserService } from './user.service';
Expand Down
8 changes: 7 additions & 1 deletion ars/src/common/graphql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ type Art {
deletedAt: DateTime!
deadline: DateTime!
is_soldout: Boolean!
deletedAt: DateTime!
user: User!
payment: Payment!
tag1: String!
Expand Down Expand Up @@ -115,6 +114,10 @@ type Comment {
user: User!
}

type Engage {
art: Art!
}

type Query {
fetchArts(tags: [String!]!, createdAt: String = "1970-2-10"): [Art!]!
fetchArt(artId: String!): Art!
Expand All @@ -132,7 +135,9 @@ type Query {
fetchHistory(page: Float!): [History!]!
fetchProfile: Profile!
fetchUser: User!
findUserEmail(phoneNum: String!): String!
fetchPurchaseList: Payment!
fetchEngaging: [Engage!]!
fetchPointTransactions: [PointTransaction!]!
}

Expand Down Expand Up @@ -160,6 +165,7 @@ type Mutation {
checkTimedoutAndProcess: String!
instantBid(artId: String!, price: Float!, artistEmail: String!): String!
Bid(artId: String!, bid_price: Float!): [String!]!
saveBid(artId: String!): String!
createPointTransaction(impUid: String!, charge_amount: Float!): PointTransaction!
cancelPointTransaction(impUid: String!): PointTransaction!
}
Expand Down

0 comments on commit df0d3a7

Please sign in to comment.