-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[#120]feat: 찜 기능 수정
- Loading branch information
Showing
6 changed files
with
61 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ services: | |
cap_add: | ||
- SYS_NICE | ||
ports: | ||
- 3307:3306 | ||
- 3306:3306 | ||
|
||
my_redis: | ||
image: redis:6.2.6 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters