Skip to content

Commit

Permalink
merge #232: HttpService 사용 & 게시글 수정/좋아요 반환 값 수정 & swagger 수정
Browse files Browse the repository at this point in the history
[chore] HttpService 사용 & 게시글 수정/좋아요 반환 값 수정 & swagger 수정
  • Loading branch information
yaongmeow authored Dec 4, 2023
2 parents b222c00 + e4cd208 commit f2b4ab3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
12 changes: 2 additions & 10 deletions BE/src/postings/posting.swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ export const search_OK = [

export const create_OK = { id: 'c89f207a-f528-4d53-8ac2-1356fa22eb21' };

export const update_OK = {
generatedMaps: [],
raw: [],
affected: 1,
};
export const update_OK = { id: '4d365e7c-3e82-472d-bf87-7faf65d9377d' };

export const remove_OK = {
title: 'bread lover',
Expand Down Expand Up @@ -167,11 +163,7 @@ export const remove_OK = {
reports: [],
};

export const like_OK = {
posting: 'c89f207a-f528-4d53-8ac2-1356fa22eb21',
user: '123456789012345678901234567890123456',
isDeleted: false,
};
export const like_OK = { isLiked: false };

export const report_OK = {
posting: 'c0265845-4991-4f04-a5c3-4cf12207d675',
Expand Down
35 changes: 31 additions & 4 deletions BE/src/postings/postings.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiConflictResponse,
ApiConsumes,
ApiCreatedResponse,
ApiForbiddenResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
Expand Down Expand Up @@ -140,6 +140,15 @@ export class PostingsController {
description: 'id 값에 해당되는 게시글을 반환합니다.',
})
@ApiOkResponse({ schema: { example: findOne_OK } })
@ApiForbiddenResponse({
schema: {
example: {
message: '차단된 게시글입니다.',
error: 'Forbidden',
statusCode: 403,
},
},
})
async findOne(@Req() request, @Param('id', ParseUUIDPipe) id: string) {
const userId = request['user'].id;
const posting = await this.postingsService.findOne(id);
Expand All @@ -159,15 +168,24 @@ export class PostingsController {
summary: '게시글 수정',
description: 'id 값에 해당되는 게시글을 수정합니다.',
})
@ApiConsumes('application/x-www-form-urlencoded')
@ApiOkResponse({ schema: { example: update_OK } })
@ApiForbiddenResponse({
schema: {
example: {
message: '본인이 작성한 게시글만 수정할 수 있습니다.',
error: 'Forbidden',
statusCode: 403,
},
},
})
async update(
@Req() request,
@Param('id', ParseUUIDPipe) id: string,
@Body() updatePostingDto: UpdatePostingDto
) {
const userId = request['user'].id;
return this.postingsService.update(id, userId, updatePostingDto);
await this.postingsService.update(id, userId, updatePostingDto);
return { id };
}

@Delete(':id')
Expand All @@ -176,6 +194,15 @@ export class PostingsController {
description: 'id 값에 해당되는 게시글을 삭제합니다.',
})
@ApiOkResponse({ schema: { example: remove_OK } })
@ApiForbiddenResponse({
schema: {
example: {
message: '본인이 작성한 게시글만 삭제할 수 있습니다.',
error: 'Forbidden',
statusCode: 403,
},
},
})
async remove(
@Req() request,
@Param('id', ParseUUIDPipe) id: string
Expand All @@ -189,7 +216,7 @@ export class PostingsController {
summary: '게시글 좋아요 토글',
description: 'id 값에 해당하는 게시글에 좋아요가 추가되거나 삭제됩니다.',
})
@ApiOkResponse({ schema: { examples: [update_OK, like_OK] } })
@ApiOkResponse({ schema: { example: like_OK } })
async toggleLike(@Req() request, @Param('id', ParseUUIDPipe) id: string) {
const userId = request['user'].id;
return this.postingsService.toggleLike(id, userId);
Expand Down
6 changes: 4 additions & 2 deletions BE/src/postings/postings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,15 @@ export class PostingsService {
const liked = await this.likedsRepository.findOne(postingId, userId);

if (liked) {
return this.likedsRepository.toggle(postingId, userId, liked.isDeleted);
await this.likedsRepository.toggle(postingId, userId, liked.isDeleted);
return { isLiked: liked.isDeleted };
}

const newLiked = new Liked();
newLiked.posting = postingId;
newLiked.user = userId;
return this.likedsRepository.save(newLiked);
await this.likedsRepository.save(newLiked);
return { isLiked: true };
}

async report(postingId: string, userId: string) {
Expand Down
9 changes: 8 additions & 1 deletion BE/src/timelines/timelines.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { TimelinesService } from './timelines.service';
import { TimelinesController } from './timelines.controller';
import { timelinesProviders } from './timelines.providers';
Expand All @@ -14,7 +15,13 @@ import { postingsProviders } from '../postings/postings.providers';
import { StorageModule } from '../storage/storage.module';

@Module({
imports: [DatabaseModule, PostingsModule, UsersModule, StorageModule],
imports: [
DatabaseModule,
PostingsModule,
UsersModule,
StorageModule,
HttpModule,
],
controllers: [TimelinesController],
providers: [
...timelinesProviders,
Expand Down
14 changes: 9 additions & 5 deletions BE/src/timelines/timelines.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
ForbiddenException,
NotFoundException,
} from '@nestjs/common';
import axios from 'axios';
import { firstValueFrom } from 'rxjs';
import { HttpService } from '@nestjs/axios';
import { CreateTimelineDto } from './dto/create-timeline.dto';
import { UpdateTimelineDto } from './dto/update-timeline.dto';
import { TimelinesRepository } from './timelines.repository';
Expand All @@ -19,7 +20,8 @@ export class TimelinesService {
private readonly timelinesRepository: TimelinesRepository,
private readonly postingsRepository: PostingsRepository,
private readonly postingsService: PostingsService,
private readonly storageService: StorageService
private readonly storageService: StorageService,
private readonly httpService: HttpService
) {}

async create(
Expand Down Expand Up @@ -151,9 +153,11 @@ export class TimelinesService {
const url = `${KAKAO_KEYWORD_SEARCH}?query=${place}&page=${offset}&size=${limit}`;
const {
data: { documents },
} = await axios.get(url, {
headers: { Authorization: `KakaoAK ${process.env.KAKAO_REST_API_KEY}` },
});
} = await firstValueFrom(
this.httpService.get(url, {
headers: { Authorization: `KakaoAK ${process.env.KAKAO_REST_API_KEY}` },
})
);

return documents;
}
Expand Down

0 comments on commit f2b4ab3

Please sign in to comment.