-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
8 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 |
---|---|---|
|
@@ -85,4 +85,4 @@ | |
"coverageDirectory": "../coverage", | ||
"testEnvironment": "node" | ||
} | ||
} | ||
} |
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,4 +1,24 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { Controller, Get, Post, Query } from '@nestjs/common'; | ||
import { FootprintService } from './footprint.service'; | ||
|
||
@Controller('footprint') | ||
export class FootprintController {} | ||
export class FootprintController { | ||
constructor( | ||
private footprintService: FootprintService | ||
) { } | ||
|
||
// 발자국 수 추가 | ||
@Post() | ||
async updateFootPrint(@Query() query) { | ||
return await this.footprintService.addFootprint( | ||
query.user_id, | ||
query.post_id, | ||
); | ||
} | ||
|
||
// 발자국 수 조회 | ||
@Get() | ||
async getFootprints(@Query() query) { | ||
return await this.footprintService.getFootprints(query.post_id); | ||
} | ||
} |
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,4 +1,80 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { BadRequestException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Footprint } from './footprint.entity'; | ||
import { User } from 'src/user/user.entity'; | ||
import { Repository } from 'typeorm'; | ||
import { Posting } from 'src/posting/posting.entity'; | ||
|
||
@Injectable() | ||
export class FootprintService {} | ||
export class FootprintService { | ||
constructor( | ||
@InjectRepository(Footprint) | ||
private footprintRepository: Repository<Footprint>, | ||
|
||
@InjectRepository(User) | ||
private userRepository: Repository<User>, | ||
|
||
@InjectRepository(Posting) | ||
private postingRepository: Repository<Posting> | ||
) { } | ||
|
||
// 발자국 수 추가 | ||
async addFootprint(user_id: string, post_id: string) { | ||
const user = await this.userRepository.findOneBy({ | ||
user_id: user_id, | ||
}); | ||
const post = await this.postingRepository.findOneBy({ | ||
post_id: post_id, | ||
}); | ||
|
||
if (user == null) { | ||
throw new BadRequestException({ | ||
status: HttpStatus.BAD_REQUEST, | ||
message: '유저가 존재하지 않습니다.', | ||
}); | ||
} | ||
if (post == null) { | ||
throw new BadRequestException({ | ||
status: HttpStatus.BAD_REQUEST, | ||
message: '게시글이 존재하지 않습니다.', | ||
}); | ||
} | ||
|
||
const isRead = await this.footprintRepository | ||
.createQueryBuilder('footprint') | ||
.where('footprint.user_id = :user_id', { user_id }) | ||
.andWhere('footprint.post_id = :post_id', { post_id }) | ||
.getOne(); | ||
|
||
if (isRead == null) { | ||
return await this.footprintRepository | ||
.createQueryBuilder('footprint') | ||
.insert() | ||
.into(Footprint) | ||
.values([ | ||
{ user_id: user }, | ||
{ post_id: post }, | ||
]) | ||
.execute(); | ||
} | ||
} | ||
|
||
// 발자국 수 조회 | ||
async getFootprints(post_id: string) { | ||
const post = await this.footprintRepository | ||
.createQueryBuilder('footprint') | ||
.where('footprint.post_id = :post_id', { post_id }) | ||
.getOne(); | ||
|
||
if (post == null) | ||
throw new BadRequestException({ | ||
status: HttpStatus.BAD_REQUEST, | ||
message: '게시글이 존재하지 않습니다.', | ||
}); | ||
|
||
return await this.footprintRepository | ||
.createQueryBuilder('footprint') | ||
.select('footprint.user_id') | ||
.getCount(); | ||
} | ||
} |