티스토리 뷰

컴퓨터공학과 5584977 이지현
캡스톤디자인(1) TEAM 우연한 인연

개발 Issue #35 유저가 게시물을 확인하면 발자국이 올라가고 발자국을 볼 수 있다.

- 우연한발견에서 발자국은 조회수이다.

- 조회수는 유저당 한 번만 올릴 수 있다. (유저가 처음 게시물을 확인하면 발자국이 올라가고, 그 뒤로 중복되어 올라가지 않음)

 

(git push하는 순서

git add .   -> 파일 반영

git commit -m [message]   -> 커밋

git remote add origin   -> 원격 저장소랑 연결

git push origin [branch]   -> branch에 push)

 

 

1. 발자국 수 추가

- footprint.controller.ts

// 발자국 수 추가
@Post()
async updateFootPrint(@Query() query) {
    return await this.footprintService.addFootprint(
        query.user_id,
        query.post_id,
    );
}

발자국 수 추가는 Post 요청으로 Query를 받아와서 그 안의 user_id 정보와 post_id 정보를 서비스의 addFootprint의 파라미터로 넘겨준다.

처음에는 update로 생각해서 함수 이름을 updateFootPrint라고 했으나

구조상 읽은 유저와 확인한 게시글 id값을 footprint 테이블에 추가해주면 되므로 create와 비슷한 코드인 듯하다.

 

footprint 테이블 -> footprint_id, user_id, post_id

 

 

- footprint.service.ts

발자국 수를 추가할 때 유저의 id와 게시물(포스트)의 id 값을 추가해야하는데,

유저와 게시물이 존재하지 않는다면 잘못된 요청이기 때문에 먼저 User 테이블과 Posting 테이블에서 해당 유저와 게시글이 존재하는지 확인해주었다.

그리고 각 값이 존재하지 않을때 잘못된 요청이라는 메시지를 띄워주도록 하였다.

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: '게시글이 존재하지 않습니다.',
    });
}

 

그리고 유저가 발자국을 한 번만 늘릴 수 있으므로 이미 게시글을 확인한 유저가 그 게시글을 확인했는지 검사해야 한다.

따라서 쿼리문으로 footprint 테이블에 동일한 user_id와 post_id 값이 존재하는지 확인한 후, 결과가 없으면 (유저가 게시글을 처음 읽었으면) footprint 테이블에 값을 넣는다.

 

- footprint 테이블에 유저, 게시글 정보가 존재하는지 검색

const isRead = await this.footprintRepository
            .createQueryBuilder('footprint')
            .where('footprint.user_id = :user_id', { user_id })
            .andWhere('footprint.post_id = :post_id', { post_id })
            .getOne();

footprint 테이블에 매개변수로 받아온 user_id와 post_id가 같은 열이 존재하는지 확인하는 쿼리이다.

 

 

- 검색결과가 없으면 footprint 테이블에 유저, 게시글 정보 추가

if (isRead == null) {
    return await this.footprintRepository
        .createQueryBuilder('footprint')
        .insert()
        .into(Footprint)
        .values([
            { user_id: user },
            { post_id: post },
        ])
        .execute();
}

footprint 테이블에 user_id와 post_id를 추가한다. pk인 footprint_id는 자동으로 추가된다. 

 

 

- 발자국 수 추가 전체 코드

// 발자국 수 추가
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();
    }
}