Browse Source

精简代码

dongbw 8 years ago
parent
commit
416bef235d

+ 4 - 28
src/main/java/com/uas/cloud/mall/shop/news/api/NewsController.java

@@ -5,19 +5,14 @@ import com.uas.cloud.mall.shop.news.model.News;
 import com.uas.cloud.mall.shop.news.service.NewsService;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
-import org.json.JSONML;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cloud.client.ServiceInstance;
 import org.springframework.cloud.client.discovery.DiscoveryClient;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.*;
-import org.springframework.web.util.UriComponentsBuilder;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
 
-import java.lang.reflect.Parameter;
-import java.net.URI;
-import java.util.Date;
 import java.util.List;
 
 /**
@@ -79,23 +74,4 @@ public class NewsController {
         return newsList;
     }
 
-
-    @RequestMapping(method = RequestMethod.POST, consumes = "application/json")
-    @ResponseStatus(HttpStatus.CREATED)
-    public ResponseEntity<News> save(@RequestBody News news, UriComponentsBuilder ucb) {
-        ServiceInstance instance = client.getLocalServiceInstance();
-        logger.info("/news, method: post, host: " + instance.getHost() + ", serviceId: " + instance.getServiceId());
-
-        News saved = newsService.save(news);
-
-        HttpHeaders headers = new HttpHeaders();
-        URI locationUri = ucb.path("/news")
-                .path(String.valueOf(news.getId()))
-                .build()
-                .toUri();
-        headers.setLocation(locationUri);
-
-        ResponseEntity<News> responseEntity = new ResponseEntity<>(saved, headers, HttpStatus.CREATED);
-        return responseEntity;
-    }
 }

+ 2 - 11
src/main/java/com/uas/cloud/mall/shop/news/data/NewsRepository.java

@@ -2,22 +2,13 @@ package com.uas.cloud.mall.shop.news.data;
 
 import com.uas.cloud.mall.shop.news.model.News;
 import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.stereotype.Repository;
 
-import java.util.List;
-
 /**
  * Created by dongbw on 201722517:56:50.
  */
 @Repository
-public interface NewsRepository extends JpaRepository<News, Long> {
-
-    @Query(value = "select n from News n where n.module = 'news' order by n.modified DESC")
-    List<News> findByCreated();
-
-    @Query(value = "select n from News n where n.module = 'news' order by n.viewCount DESC")
-    List<News> findByViewCount();
+public interface NewsRepository extends JpaSpecificationExecutor<News>, JpaRepository<News, Long> {
 
-//    List<News> findAllOrderByCreatedDesc(Date date);
 }

+ 23 - 32
src/main/java/com/uas/cloud/mall/shop/news/model/News.java

@@ -1,7 +1,12 @@
 package com.uas.cloud.mall.shop.news.model;
 
-import javax.persistence.*;
-import java.util.*;
+import com.fasterxml.jackson.annotation.JsonInclude;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import java.util.Date;
 
 /**
  *  资讯
@@ -11,8 +16,11 @@ import java.util.*;
 
 @Entity
 @Table(name = "jpress_content")
+@JsonInclude(JsonInclude.Include.NON_EMPTY)
 public class News{
 
+//    @Value("${application.indexUrl}")
+//    private String INDEX_USOFTCHINA;
     private static final String INDEX_USOFTCHINA = "10.10.100.88:9090/";
 
     private static final String NEWS_TEXT = "c/";
@@ -27,17 +35,17 @@ public class News{
     @Column(name = "title")
     private String title;
 
-    /**
-     *  摘要
-     */
-    @Column(name = "summary")
-    private String summary;
-
-    /**
-     *  内容
-     */
-    @Column(name = "text")
-    private String content;
+//    /**
+//     *  摘要
+//     */
+//    @Column(name = "summary")
+//    private String summary;
+//
+//    /**
+//     *  内容
+//     */
+//    @Column(name = "text")
+//    private String content;
 
     /**
      *  缩略图
@@ -81,10 +89,8 @@ public class News{
 
     public News() {}
 
-    public News(String title, String summary, String content, String thumbnail, String hrefUrl, Long viewCount, Date created, Date modified , String url_suffix) {
+    public News(String title, String thumbnail, String hrefUrl, Long viewCount, Date created, Date modified , String url_suffix) {
         this.title = title;
-        this.summary = summary;
-        this.content = content;
         this.thumbnail = thumbnail;
         this.hrefUrl = hrefUrl;
         this.viewCount = viewCount;
@@ -109,22 +115,6 @@ public class News{
         this.title = title;
     }
 
-    public String getSummary() {
-        return summary;
-    }
-
-    public void setSummary(String summary) {
-        this.summary = summary;
-    }
-
-    public String getContent() {
-        return content;
-    }
-
-    public void setContent(String content) {
-        this.content = content;
-    }
-
     public String getThumbnail() {
         return thumbnail;
     }
@@ -135,6 +125,7 @@ public class News{
 
     // 跳转到文章详情的URL
     public String getHrefUrl() {
+        System.out.println(INDEX_USOFTCHINA);
         StringBuffer linkTo = new StringBuffer();
         linkTo.append(INDEX_USOFTCHINA);
         linkTo.append(NEWS_TEXT);

+ 0 - 10
src/main/java/com/uas/cloud/mall/shop/news/service/NewsService.java

@@ -2,7 +2,6 @@ package com.uas.cloud.mall.shop.news.service;
 
 import com.uas.cloud.mall.shop.news.model.News;
 
-import java.util.Date;
 import java.util.List;
 
 /**
@@ -19,13 +18,6 @@ public interface NewsService {
      */
     List<News> findBycreated();
 
-    /**
-     *  根据传入数量获取创建时间最新的多条新闻
-     *
-     * @param created
-     * @return
-     */
-    List<News> findBycreatedDesc(Date created);
 
     /**
      *  根据传入数量获取点击量最多的多条新闻
@@ -33,6 +25,4 @@ public interface NewsService {
      * @return
      */
     List<News> findByViewCount();
-
-    News save(News news);
 }

+ 26 - 13
src/main/java/com/uas/cloud/mall/shop/news/service/impl/NewsServiceImpl.java

@@ -3,11 +3,16 @@ package com.uas.cloud.mall.shop.news.service.impl;
 import com.uas.cloud.mall.shop.news.data.NewsRepository;
 import com.uas.cloud.mall.shop.news.model.News;
 import com.uas.cloud.mall.shop.news.service.NewsService;
+import org.apache.commons.collections.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Service;
 
+import javax.persistence.criteria.Predicate;
 import java.util.Collections;
-import java.util.Date;
 import java.util.List;
 
 /**
@@ -26,22 +31,30 @@ public class NewsServiceImpl implements NewsService {
 
     @Override
     public List<News> findBycreated() {
-        return newsRepository.findByCreated();
-    }
-
-    @Override
-    public List<News> findBycreatedDesc(Date created) {
-        /*return newsRepository.findAllOrderByCreatedDesc(created);*/
+        Pageable pageable = new PageRequest(0 ,8, new Sort(Sort.Direction.DESC, "modified"));
+        Page<News> page = newsRepository.findAll((root, query, builder) -> {
+            Predicate predicate = builder.equal(root.get("module"), "news");
+            query.where(predicate);
+            return null;
+        }, pageable);
+        if (page != null && !CollectionUtils.isEmpty(page.getContent())) {
+            return page.getContent();
+        }
         return Collections.emptyList();
     }
 
-    @Override
-    public List<News> findByViewCount() {
-        return newsRepository.findByViewCount();
-    }
 
     @Override
-    public News save(News news) {
-        return newsRepository.save(news);
+    public List<News> findByViewCount() {
+        Pageable pageable = new PageRequest(0 ,8, new Sort(Sort.Direction.DESC, "viewCount"));
+        Page<News> page = newsRepository.findAll((root, query, builder) -> {
+            Predicate predicate = builder.equal(root.get("module"), "news");
+            query.where(predicate);
+            return null;
+        }, pageable);
+        if (page != null && !CollectionUtils.isEmpty(page.getContent())) {
+            return page.getContent();
+        }
+        return Collections.emptyList();
     }
 }

+ 1 - 1
src/main/resources/application-dev.yml

@@ -24,4 +24,4 @@ server:
 eureka:
   client:
     serviceUrl:
-      defaultZone: http://localhost:1111/eureka/
+      defaultZone: http://localhost:1111/eureka/