This commit is contained in:
parent
d94a64b74f
commit
c4ffc20378
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,7 +11,6 @@ import java.util.List;
|
||||||
@Repository
|
@Repository
|
||||||
public interface NewsRepository extends JpaRepository<News, Long>, JpaSpecificationExecutor<News> {
|
public interface NewsRepository extends JpaRepository<News, Long>, JpaSpecificationExecutor<News> {
|
||||||
// find支持的关键字, And , Between, In, StaringWith
|
// find支持的关键字, And , Between, In, StaringWith
|
||||||
List<News> findById(long Id);
|
|
||||||
|
|
||||||
List<News> findByTitle(String title);
|
List<News> findByTitle(String title);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<NewsDTO> getAllNews();
|
||||||
|
}
|
||||||
|
|
@ -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<NewsDTO> getAllNews() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
package com.lessthread.twtnews.utils;
|
||||||
|
|
||||||
|
public class Response <T> {
|
||||||
|
|
||||||
|
private T data;
|
||||||
|
private boolean success;
|
||||||
|
private String errorMsg;
|
||||||
|
|
||||||
|
public static <K> Response<K> newSuccess(K data) {
|
||||||
|
Response<K> response = new Response<>();
|
||||||
|
response.setData(data);
|
||||||
|
response.setSuccess(true);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Response<Void> newFail(String errorMsg) {
|
||||||
|
Response<Void> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue