Skip to content

Commit

Permalink
feature: add article to recommender system
Browse files Browse the repository at this point in the history
  • Loading branch information
mushroom1324 committed Nov 12, 2024
1 parent 509da93 commit e9ab422
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ public GetArticles postArticle(String requestBody) throws ParseException {

articleRepository.save(article);

try {
recommenderUtils.registerArticle(article);

}
catch (JsonProcessingException e) {
e.printStackTrace();
System.out.println("Failed to register article to recommender system.");
}

return GetArticles.of(article);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.newsfit.global.util;

import com.example.newsfit.domain.article.entity.Article;
import com.example.newsfit.domain.article.entity.Category;
import com.example.newsfit.domain.article.entity.Press;
import com.example.newsfit.domain.member.entity.Member;
Expand All @@ -8,6 +9,7 @@
import com.fasterxml.jackson.databind.ObjectWriter;
import lombok.RequiredArgsConstructor;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
Expand All @@ -25,6 +27,23 @@ public class RecommenderUtils {
@Value("${recommender.endpoint}")
private String recommenderEndpoint;

public void registerArticle(Article article) throws JsonProcessingException {

JSONObject request = new JSONObject();
request.put("news_id", article.getArticleId());
request.put("category", article.getCategory().toString());
request.put("publisher", article.getPress().toString());
request.put("title", article.getTitle());
request.put("headLine", article.getTitle());
request.put("thumbnail", article.getImages().get(0));
request.put("publishDate", article.getPublishDate().toString());

String requestBody = request.toJSONString();

requestRecommender(requestBody, "/new-news", HttpMethod.POST);

}

public void registerMember(Long memberId) throws JsonProcessingException {
String requestBody = String.format("{ \"user_id\": %d }", memberId);
requestRecommender(requestBody, "/new-user", HttpMethod.POST);
Expand Down

0 comments on commit e9ab422

Please sign in to comment.