Skip to content

Commit

Permalink
feat:#35 발자국 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
lywg committed Dec 1, 2022
1 parent 7cad263 commit e54f104
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
24 changes: 22 additions & 2 deletions src/footprint/footprint.controller.ts
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);
}
}
12 changes: 9 additions & 3 deletions src/footprint/footprint.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { Module } from '@nestjs/common';
import { FootprintController } from './footprint.controller';
import { FootprintService } from './footprint.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import {Footprint} from './footprint.entity';
import { Footprint } from './footprint.entity';
import { User } from 'src/user/user.entity';
import { Posting } from 'src/posting/posting.entity';
@Module({
imports: [TypeOrmModule.forFeature([Footprint])],
imports: [
TypeOrmModule.forFeature([Footprint]),
TypeOrmModule.forFeature([User]),
TypeOrmModule.forFeature([Posting]),
],
controllers: [FootprintController],
providers: [FootprintService]
})
export class FootprintModule {}
export class FootprintModule { }


80 changes: 78 additions & 2 deletions src/footprint/footprint.service.ts
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();
}
}

0 comments on commit e54f104

Please sign in to comment.