Skip to content

Commit

Permalink
Merge pull request #6 from 7th-UMC-Hackathon-TeamV/feat/likebtn
Browse files Browse the repository at this point in the history
feat : 공감 누르기 기능
  • Loading branch information
dokyung-kang authored Jan 11, 2025
2 parents 92a2027 + 8a27d85 commit a3d5b51
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
//검증 어노테이션, Error Handler
implementation 'org.springframework.boot:spring-boot-starter-validation'
//Swagger 세팅
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0'
implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.security:spring-security-test'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public enum ErrorStatus implements BaseErrorCode {
//FoodCategory Error
FOOD_CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, "FOOD_CATEGORY4001", "음식 카테고리가 없습니다."),

// News Error
NEWS_NOT_EXIST_FOUND(HttpStatus.BAD_REQUEST, "NEWS4001", "뉴스가 없습니다."),

//Store Error
STORE_NOT_FOUND(HttpStatus.NOT_FOUND, "STORE_4001","가게가 없습니다.");



private final HttpStatus httpStatus;
private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package banban.springboot.apiPayload.exception.handler;

import banban.springboot.apiPayload.code.BaseErrorCode;
import banban.springboot.apiPayload.exception.GeneralException;

public class NewsHandler extends GeneralException {
public NewsHandler(BaseErrorCode errorCode) {
super(errorCode);
}
}
14 changes: 14 additions & 0 deletions src/main/java/banban/springboot/service/NewsService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package banban.springboot.service;

import banban.springboot.apiPayload.code.status.ErrorStatus;
import banban.springboot.apiPayload.exception.handler.NewsHandler;
import banban.springboot.domain.entity.News;
import banban.springboot.repository.NewsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -10,4 +13,15 @@ public class NewsService {
@Autowired
private NewsRepository newsRepository;

// 뉴스 공감 누르기
public News addNewsLike(Long newsId){
News existNews = newsRepository.findById(newsId)
.orElseThrow(() -> new NewsHandler(ErrorStatus.NEWS_NOT_EXIST_FOUND));

Integer likes_count = existNews.getLikes() + 1;

existNews.setLikes(likes_count);

return newsRepository.save(existNews);
}
}
38 changes: 38 additions & 0 deletions src/main/java/banban/springboot/web/controller/NewsController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package banban.springboot.web.controller;

import banban.springboot.apiPayload.ApiResponse;
import banban.springboot.domain.entity.News;
import banban.springboot.service.NewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@Validated
@RequestMapping("/api")
public class NewsController {

private final NewsService newsService;

// 뉴스 공감 누르기
@PostMapping("/news/{newsId}/likes")
@Operation(summary = "공감 누르기 API",description = "뉴스에 공감 누르는 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "newsId", description = "뉴스글의 ID, path variable 입니다!")
})
public ApiResponse<Void> updatePromotionProject(@PathVariable(name = "newsId") Long newsId){

newsService.addNewsLike(newsId);

return ApiResponse.onSuccess(null);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring:
datasource:
url: jdbc:${DB_URL}
url: ${DB_URL}
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
Expand Down

0 comments on commit a3d5b51

Please sign in to comment.