diff --git a/src/main/java/com/lessthread/twtnews/converter/NewsConverter.java b/src/main/java/com/lessthread/twtnews/converter/NewsConverter.java new file mode 100644 index 0000000..5c81efb --- /dev/null +++ b/src/main/java/com/lessthread/twtnews/converter/NewsConverter.java @@ -0,0 +1,21 @@ +package com.lessthread.twtnews.converter; + +import com.lessthread.twtnews.dao.News; +import com.lessthread.twtnews.dto.NewsDTO; + +public class NewsConverter { + public static NewsDTO convertNews(News news) { + NewsDTO newsDTO = new NewsDTO(); + newsDTO.setId(news.getId()); + newsDTO.setTitle(news.getTitle()); + newsDTO.setContent(news.getContent()); + return newsDTO; + } + + public static News convertNews(NewsDTO newsDTO) { + News news = new News(); + news.setTitle(newsDTO.getTitle()); + news.setContent(newsDTO.getContent()); + return news; + } +} diff --git a/src/main/java/com/lessthread/twtnews/dao/NewsRepository.java b/src/main/java/com/lessthread/twtnews/dao/NewsRepository.java index 5412537..ed50e05 100644 --- a/src/main/java/com/lessthread/twtnews/dao/NewsRepository.java +++ b/src/main/java/com/lessthread/twtnews/dao/NewsRepository.java @@ -11,7 +11,6 @@ import java.util.List; @Repository public interface NewsRepository extends JpaRepository, JpaSpecificationExecutor { // find支持的关键字, And , Between, In, StaringWith - List findById(long Id); List findByTitle(String title); diff --git a/src/main/java/com/lessthread/twtnews/dto/NewsDTO.java b/src/main/java/com/lessthread/twtnews/dto/NewsDTO.java new file mode 100644 index 0000000..86cc3ba --- /dev/null +++ b/src/main/java/com/lessthread/twtnews/dto/NewsDTO.java @@ -0,0 +1,38 @@ +package com.lessthread.twtnews.dto; + + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.Instant; + +@Builder +@Data +@NoArgsConstructor +@AllArgsConstructor +public class NewsDTO { + + private long id; + private String title; + private String content; + private Instant time; + private int author; + + public String getTitle(){ + return title; + } + + public void setTitle(String title){ + this.title = title; + } + + public String getContent(){ + return content; + } + + public void setConent(String content){ + this.content = content; + } +} diff --git a/src/main/java/com/lessthread/twtnews/service/NewsService.java b/src/main/java/com/lessthread/twtnews/service/NewsService.java new file mode 100644 index 0000000..18a8c8f --- /dev/null +++ b/src/main/java/com/lessthread/twtnews/service/NewsService.java @@ -0,0 +1,18 @@ +package com.lessthread.twtnews.service; + +import com.lessthread.twtnews.dao.News; +import com.lessthread.twtnews.dto.NewsDTO; + +import java.util.List; + +public interface NewsService { + NewsDTO getNewsById(long id); + + Long addNews(NewsDTO newsDTO); + + void deleteNewsById(long id); + + NewsDTO updateNewsById(long id,String title,String content); + + List getAllNews(); +} diff --git a/src/main/java/com/lessthread/twtnews/service/NewsServiceImpl.java b/src/main/java/com/lessthread/twtnews/service/NewsServiceImpl.java new file mode 100644 index 0000000..d2daf80 --- /dev/null +++ b/src/main/java/com/lessthread/twtnews/service/NewsServiceImpl.java @@ -0,0 +1,50 @@ +package com.lessthread.twtnews.service; + +import com.lessthread.twtnews.dao.News; +import com.lessthread.twtnews.dao.NewsRepository; +import com.lessthread.twtnews.dto.NewsDTO; +import com.lessthread.twtnews.converter.NewsConverter; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + + +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Service +public class NewsServiceImpl implements NewsService{ + + @Autowired + private NewsRepository newsRepository; + + @Override + public NewsDTO getNewsById(long id){ + News news = newsRepository.findById(id).orElseThrow(RuntimeException::new); + return NewsConverter.convertNews(news); + } + + @Override + public Long addNews(NewsDTO newsDTO) { + return null; + } + + @Override + public void deleteNewsById(long id) { + + } + + @Override + public NewsDTO updateNewsById(long id, String title, String content) { + return null; + } + + @Override + public List getAllNews() { + return null; + } +} diff --git a/src/main/java/com/lessthread/twtnews/utils/Response.java b/src/main/java/com/lessthread/twtnews/utils/Response.java new file mode 100644 index 0000000..fff5d10 --- /dev/null +++ b/src/main/java/com/lessthread/twtnews/utils/Response.java @@ -0,0 +1,46 @@ +package com.lessthread.twtnews.utils; + +public class Response { + + private T data; + private boolean success; + private String errorMsg; + + public static Response newSuccess(K data) { + Response response = new Response<>(); + response.setData(data); + response.setSuccess(true); + return response; + } + + public static Response newFail(String errorMsg) { + Response response = new Response<>(); + response.setErrorMsg(errorMsg); + response.setSuccess(false); + return response; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public String getErrorMsg() { + return errorMsg; + } + + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } +}