Skip to content

Commit

Permalink
Merge pull request #20 from Team3-NEWNEEK-Clone-Coding/isHeart
Browse files Browse the repository at this point in the history
IsNewsHeart
  • Loading branch information
dpwls8364 committed Jul 27, 2023
2 parents 392f4f0 + 742d261 commit 56e81c3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setCharacterEncoding("UTF-8");

// JSON 응답 생성
String json = "{\"msg\": \"로그인이 완료 되었습니다.\"}";
String json = String.format("{\"msg\": \"로그인이 완료 되었습니다.\",\"emoji\": \"%s\"}",emoji);

// JSON 응답 전송
PrintWriter writer = response.getWriter();
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/example/newnique/global/Scheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.newnique.news.entity.News;
import com.example.newnique.news.repository.NewsRepository;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
Expand Down Expand Up @@ -40,10 +41,10 @@ public String getLink() {
}
}

// @PostConstruct
// public void init() throws InterruptedException {
// updateNews(); // 프로그램 시작 시에 한 번 실행(테스트 용도)
// }
// @PostConstruct
// public void init() throws InterruptedException {
// updateNews(); // 프로그램 시작 시에 한 번 실행(테스트 용도)
// }

@Scheduled(cron = "0 0 3 * * ?")
public void updateNews() throws InterruptedException {
Expand Down Expand Up @@ -130,6 +131,7 @@ public void updateNews() throws InterruptedException {
}
}

Collections.shuffle(todaysNewsLinkList);

//오늘 올라온 기사 상세정보 저장
for(Category newsDetailsLinkPair : todaysNewsLinkList){
Expand Down Expand Up @@ -285,4 +287,4 @@ public int countOccurrences(String text, String word) {
}
return count;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.example.newnique.news.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

@Getter
public class NewsHeartResponseDto {
private int NewsHeart;

public NewsHeartResponseDto(int heartCount) {
this.NewsHeart = heartCount;
private int newsHeart;
private Boolean isNewsHeart ;

public NewsHeartResponseDto(int heartCount, boolean isNewsHeart) {
this.newsHeart = heartCount;
this.isNewsHeart = isNewsHeart;
}
}
15 changes: 10 additions & 5 deletions src/main/java/com/example/newnique/news/service/NewsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Map<String, Object> getNews(int page, int size,

// 페이징 처리
Sort.Direction direction = isAsc ? Sort.Direction.ASC : Sort.Direction.DESC;
Sort sort = Sort.by(direction, sortBy);
Sort sort = Sort.by(direction, sortBy,"title");
Pageable pageable = PageRequest.of(page, size, sort);

Page<News> newsList = newsRepository.findAll(pageable);
Expand Down Expand Up @@ -70,7 +70,7 @@ public Map<String, Object> getNewsByCategory(String category, int page, int size

// 페이징 처리
Sort.Direction direction = isAsc ? Sort.Direction.ASC : Sort.Direction.DESC;
Sort sort = Sort.by(direction, sortBy);
Sort sort = Sort.by(direction, sortBy,"title");
Pageable pageable = PageRequest.of(page, size, sort);

Page<News> newsListByCategory = newsRepository.findAllByCategory(category, pageable);
Expand Down Expand Up @@ -106,7 +106,7 @@ public Map<String, Object> SearchNews(String keyword, int page,
Map<String, Object> response = new HashMap<>();
List<NewsResponseDto> newsResponseDtoList = newsListByCategory.stream().map(NewsResponseDto::new).collect(Collectors.toList());

int totalNewsCount = newsRepository.countSearchNewsByKeyWordNativeVer("+"+keyword);
int totalNewsCount = newsRepository.countSearchNewsByKeyWordNativeVer("+"+keyword+"*");
int totalPages = (int) Math.ceil((double) totalNewsCount / size);
response.put("totalPages", totalPages);

Expand Down Expand Up @@ -144,15 +144,20 @@ public NewsHeartResponseDto getNewsHeart(Long newsId, String userEmail) {
);

NewsHeart existHeart = newsHeartRepository.findByHeartUserAndHeartNews(loginUser, news);
boolean isNewsHeart;
if (existHeart == null) {
NewsHeart newsHeart = new NewsHeart(loginUser, news);
news.increaseHeartCount();
newsHeartRepository.save(newsHeart);
isNewsHeart = true;
} else {
news.decreaseHeartCount();
newsHeartRepository.delete(existHeart);
isNewsHeart = false;
}
return new NewsHeartResponseDto(news.getHeartCount());

return new NewsHeartResponseDto(news.getHeartCount(), isNewsHeart);
}

}

}

0 comments on commit 56e81c3

Please sign in to comment.