티스토리 뷰
개발 Issue #35 유저가 게시물을 확인하면 발자국이 올라가고 발자국을 볼 수 있다.
- 우연한발견에서 발자국은 조회수이다.
- 조회수는 유저당 한 번만 올릴 수 있다. (유저가 처음 게시물을 확인하면 발자국이 올라가고, 그 뒤로 중복되어 올라가지 않음)
- 발자국 조회
- footprint.controller.ts
// 발자국 수 조회
@Get()
async getFootprints(@Query() query) {
return await this.footprintService.getFootprints(query.post_id);
}
발자국 수 조회는 유저가 게시글을 누르면 게시글 정보와 함께 불러와진다. 그 때 Get으로 post_id값을 받아와서 getFootprints()에 매개변수로 넘겨준다.
- footprint.service.ts
발자국을 조회하기에 앞서, 먼저 게시글이 존재하는 게시글인지 확인한다.
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: '게시글이 존재하지 않습니다.',
});
footprint 테이블에 post_id를 검색해서 그 결과를 post 변수에 넣는다.
post가 null이면 값이 존재하지 않으므로 BadRequestException으로 게시글이 존재하지 않는다는 오류메시지를 띄운다.
post가 null이 아니면 해당 게시글의 조회수를 count하여 조회한 결과를 return 한다.
return await this.footprintRepository
.createQueryBuilder('footprint')
.select('footprint.user_id')
.where('footprint.post_id = :post_id', { post_id })
.getCount();
.getCount()를 통해 select하는 열을 count한다.
SELECT COUNT(footprint.user_id) FROM footprint WHERE footprint.post_id = {post_id} 와 같은 쿼리이다.
- 전체 발자국 조회 코드
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 {
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')
.where('footprint.post_id = :post_id', { post_id })
.getCount();
}
}
'캡스톤디자인(1) > 개발' 카테고리의 다른 글
캡스톤디자인(1) 개발 - 신고 (개발중) (0) | 2022.12.06 |
---|---|
캡스톤디자인(1) 개발 - (수정) 발자국 (0) | 2022.12.06 |
캡스톤디자인(1) 개발 - 발자국: 발자국 추가 (0) | 2022.12.01 |
캡스톤디자인(1) 개발 - 데이터 조회, 수정, 삭제 (0) | 2022.11.10 |
캡스톤디자인(1) 개발 - 개발 환경 셋팅 (0) | 2022.11.07 |