-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from SJU-CapstoneDesign/feat/#40NewsCategorySe…
…rvice Feat/#40 news category service
- Loading branch information
Showing
13 changed files
with
203 additions
and
66 deletions.
There are no files selected for viewing
35 changes: 0 additions & 35 deletions
35
src/main/java/com/example/d3nserver/news/controller/NewsController.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/main/java/com/example/d3nserver/news/dto/NewsResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/example/d3nserver/news/dto/TodayHomeResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.d3nserver.news.dto; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
public class TodayHomeResponseDto { | ||
private String type; | ||
private String title; | ||
private String subtitle; | ||
private List<NewsResponseDto> newsList; | ||
|
||
public TodayHomeResponseDto(String type, List<NewsResponseDto> newsList){ | ||
|
||
if(type.equals("Recent")){ | ||
this.type = type; | ||
this.title = "최신 뉴스"; | ||
this.subtitle = "최신 뉴스를 가져왔어요"; | ||
} | ||
else if(type.equals("My")){ | ||
this.type = type; | ||
this.title = "맞춤 뉴스"; | ||
this.subtitle = "맞춤형 뉴스를 가져왔어요"; | ||
} | ||
this.newsList = newsList; | ||
} | ||
} |
12 changes: 10 additions & 2 deletions
12
src/main/java/com/example/d3nserver/news/repository/NewsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
package com.example.d3nserver.news.repository; | ||
|
||
import com.example.d3nserver.news.domain.Field; | ||
import com.example.d3nserver.news.domain.News; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface NewsRepository extends JpaRepository<News, Integer> { | ||
List<News> findTop10ByOrderByCreatedAtDesc(); | ||
public interface NewsRepository extends JpaRepository<News, Long> { | ||
|
||
@Query("SELECT n FROM News n ORDER BY n.createdAt DESC LIMIT :n") | ||
List<News> findTopNByOrderByCreatedAtDesc(@Param("n") int cnt); | ||
@Query("SELECT n FROM News n WHERE n.field = :field ORDER BY n.createdAt DESC LIMIT :n") | ||
List<News> findTopNByField(@Param("field") Field field, @Param("n") int n); | ||
Page<News> findAllNewsByOrderByCreatedAtDesc(Pageable pageable); | ||
Page<News> findAllByFieldOrderByCreatedAtDesc(Field field, Pageable pageable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/example/d3nserver/time/repository/NewsReadingTimeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
package com.example.d3nserver.time.repository; | ||
|
||
import com.example.d3nserver.news.domain.Field; | ||
import com.example.d3nserver.time.domain.NewsReadingTime; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface NewsReadingTimeRepository extends JpaRepository<NewsReadingTime, Long> { | ||
public Optional<NewsReadingTime> findByUserIdAndNewsId(String userId, Long newsId); | ||
@Query("SELECT nrt FROM NewsReadingTime nrt JOIN FETCH nrt.news WHERE nrt.userId = :userId ORDER BY nrt.createdAt DESC") | ||
List<NewsReadingTime> findTop10ByUserIdOrderByCreatedAtDesc(@Param("userId") String userId); | ||
|
||
} |
37 changes: 36 additions & 1 deletion
37
src/main/java/com/example/d3nserver/time/service/NewsReadingTimeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,56 @@ | ||
package com.example.d3nserver.time.service; | ||
|
||
import com.example.d3nserver.news.domain.Field; | ||
import com.example.d3nserver.news.domain.News; | ||
import com.example.d3nserver.news.repository.NewsRepository; | ||
import com.example.d3nserver.time.domain.NewsReadingTime; | ||
import com.example.d3nserver.time.repository.NewsReadingTimeRepository; | ||
import com.example.d3nserver.user.domain.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class NewsReadingTimeService { | ||
private final NewsReadingTimeRepository newsReadingTimeRepository; | ||
private final NewsRepository newsRepository; | ||
|
||
public void updateNewsReadingTime(User user, Long newsId, int secondTime){ | ||
NewsReadingTime readingTime = newsReadingTimeRepository.findByUserIdAndNewsId(user.getId(), newsId).orElseGet( | ||
()-> new NewsReadingTime(user.getId(), newsId) | ||
()-> { | ||
Optional<News> news = newsRepository.findById(newsId); | ||
return news.map(value -> new NewsReadingTime(user.getId(), value)).orElse(null); | ||
} | ||
); | ||
if(readingTime == null) | ||
return; | ||
readingTime.updateReadingTime(secondTime); | ||
newsReadingTimeRepository.save(readingTime); | ||
} | ||
|
||
public Field getCategoryOfMostReadingTime(User user){ | ||
Map<Field, Integer> categoryTimeMap = new HashMap<Field, Integer>(); | ||
List<NewsReadingTime> newsReadingTimeList = newsReadingTimeRepository.findTop10ByUserIdOrderByCreatedAtDesc(user.getId()); | ||
if(newsReadingTimeList.isEmpty()) | ||
return Field.DEFAULT; | ||
int mostReadingTime = 0; | ||
Field mostReadingTimeField = Field.DEFAULT; | ||
for(NewsReadingTime readingTime : newsReadingTimeList) { | ||
Field field = readingTime.getNews().getField(); | ||
if(categoryTimeMap.containsKey(field)) | ||
categoryTimeMap.put(field, categoryTimeMap.get(field) + readingTime.getSecondTime()); | ||
else | ||
categoryTimeMap.put(field, readingTime.getSecondTime()); | ||
if (categoryTimeMap.get(field) > mostReadingTime) { | ||
mostReadingTime = categoryTimeMap.get(field); | ||
mostReadingTimeField = field; | ||
} | ||
} | ||
return mostReadingTimeField; | ||
} | ||
} |
Oops, something went wrong.