package edu.wisc.my.rssToJson.service;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;

import edu.wisc.my.rssToJson.dao.RssToJsonDao;


@Service
public class RsstoJsonServiceImpl implements RssToJsonService {
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    
    private RssToJsonDao rssToJsonDao;
        
    @Autowired
    void setRssToJsonDao(RssToJsonDao rssToJsonDao){
        this.rssToJsonDao = rssToJsonDao;
    }

    @Override
    public JSONObject getJsonFromURL(String endpoint) {
        SyndFeed feed = rssToJsonDao.getRssFeed(endpoint);
        if(feed == null){
            logger.warn("No feed returned for endpoint: {}", endpoint);
            return null;
        }
        JSONObject jsonToReturn = new JSONObject();
        
        JSONObject feedInfo = new JSONObject();
        feedInfo.put("title", feed.getTitle());
        feedInfo.put("link", feed.getLink());
        feedInfo.put("description", feed.getDescription());
        jsonToReturn.put("feed", feedInfo);
        JSONArray entries = new JSONArray();
        for(SyndEntry entry : feed.getEntries()){
            JSONObject feedItem = new JSONObject();
            feedItem.put("title", entry.getTitle());
            feedItem.put("link", entry.getLink());
            feedItem.put("description", entry.getDescription().getValue());
            entries.put(feedItem);
        }
        jsonToReturn.put("items", entries);
        jsonToReturn.put("status", "ok");
        return jsonToReturn;
    }
}

@@ -589,21 +589,17 @@
 ass());%0A
-    
 %0A
+
     priv
@@ -629,25 +629,17 @@
 sonDao;%0A
-        
 %0A
+
     @Aut
@@ -1065,17 +1065,9 @@
 ();%0A
-        
 %0A
+
     
@@ -1260,24 +1260,82 @@
 ription());%0A
+        feedInfo.put(%22pubDate%22, feed.getPublishedDate());%0A
         json
@@ -1609,32 +1609,32 @@
 try.getLink());%0A
-
             feed
@@ -1689,24 +1689,87 @@
 etValue());%0A
+            feedItem.put(%22pubDate%22, entry.getPublishedDate());%0A
             

Add pubDate to feed