From 3ffb730b058fb149a11f58c027280a96505e4c3f Mon Sep 17 00:00:00 2001 From: BK Date: Sat, 2 Dec 2023 16:54:22 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EA=B3=B5=EC=A7=80=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20API=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generated/igoMoney/BE/domain/QNews.java | 49 +++++++++++++++++++ .../BE/controller/NewsController.java | 32 ++++++++++++ src/main/java/igoMoney/BE/domain/News.java | 23 +++++++++ .../BE/dto/response/NewsResponse.java | 19 +++++++ .../BE/repository/NewsRepository.java | 10 ++++ .../java/igoMoney/BE/service/NewsService.java | 42 ++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100644 src/main/generated/igoMoney/BE/domain/QNews.java create mode 100644 src/main/java/igoMoney/BE/controller/NewsController.java create mode 100644 src/main/java/igoMoney/BE/domain/News.java create mode 100644 src/main/java/igoMoney/BE/dto/response/NewsResponse.java create mode 100644 src/main/java/igoMoney/BE/repository/NewsRepository.java create mode 100644 src/main/java/igoMoney/BE/service/NewsService.java diff --git a/src/main/generated/igoMoney/BE/domain/QNews.java b/src/main/generated/igoMoney/BE/domain/QNews.java new file mode 100644 index 0000000..2c7c551 --- /dev/null +++ b/src/main/generated/igoMoney/BE/domain/QNews.java @@ -0,0 +1,49 @@ +package igoMoney.BE.domain; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; + + +/** + * QNews is a Querydsl query type for News + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QNews extends EntityPathBase { + + private static final long serialVersionUID = -738777007L; + + public static final QNews news = new QNews("news"); + + public final igoMoney.BE.common.entity.QBaseEntity _super = new igoMoney.BE.common.entity.QBaseEntity(this); + + public final StringPath content = createString("content"); + + //inherited + public final DateTimePath createdDate = _super.createdDate; + + public final NumberPath id = createNumber("id", Long.class); + + //inherited + public final DateTimePath modifiedDate = _super.modifiedDate; + + public final StringPath title = createString("title"); + + public QNews(String variable) { + super(News.class, forVariable(variable)); + } + + public QNews(Path path) { + super(path.getType(), path.getMetadata()); + } + + public QNews(PathMetadata metadata) { + super(News.class, metadata); + } + +} + diff --git a/src/main/java/igoMoney/BE/controller/NewsController.java b/src/main/java/igoMoney/BE/controller/NewsController.java new file mode 100644 index 0000000..8a8cefd --- /dev/null +++ b/src/main/java/igoMoney/BE/controller/NewsController.java @@ -0,0 +1,32 @@ +package igoMoney.BE.controller; + +import igoMoney.BE.dto.response.NewsResponse; +import igoMoney.BE.service.NewsService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@Slf4j +@RequestMapping("news") +public class NewsController { + + private final NewsService newsService; + + @GetMapping("") + public ResponseEntity> getNews( + @RequestParam(value="lastId", required = false, defaultValue = "-1") Long lastId ) { + + List response = newsService.getNews(lastId); + + return ResponseEntity.status(HttpStatus.OK).body(response); + } +} diff --git a/src/main/java/igoMoney/BE/domain/News.java b/src/main/java/igoMoney/BE/domain/News.java new file mode 100644 index 0000000..9bf2a59 --- /dev/null +++ b/src/main/java/igoMoney/BE/domain/News.java @@ -0,0 +1,23 @@ +package igoMoney.BE.domain; + +import igoMoney.BE.common.entity.BaseEntity; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class News extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "news_id") + private Long id; + private String title; + private String content; +} diff --git a/src/main/java/igoMoney/BE/dto/response/NewsResponse.java b/src/main/java/igoMoney/BE/dto/response/NewsResponse.java new file mode 100644 index 0000000..adf25cf --- /dev/null +++ b/src/main/java/igoMoney/BE/dto/response/NewsResponse.java @@ -0,0 +1,19 @@ +package igoMoney.BE.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class NewsResponse { + private Long newsId; + private String title; + private String content; + private LocalDate date; +} diff --git a/src/main/java/igoMoney/BE/repository/NewsRepository.java b/src/main/java/igoMoney/BE/repository/NewsRepository.java new file mode 100644 index 0000000..926f2b4 --- /dev/null +++ b/src/main/java/igoMoney/BE/repository/NewsRepository.java @@ -0,0 +1,10 @@ +package igoMoney.BE.repository; + +import igoMoney.BE.domain.News; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface NewsRepository extends JpaRepository { + List findAllByIdGreaterThan(Long lastId); +} diff --git a/src/main/java/igoMoney/BE/service/NewsService.java b/src/main/java/igoMoney/BE/service/NewsService.java new file mode 100644 index 0000000..41e40fc --- /dev/null +++ b/src/main/java/igoMoney/BE/service/NewsService.java @@ -0,0 +1,42 @@ +package igoMoney.BE.service; + +import igoMoney.BE.domain.News; +import igoMoney.BE.dto.response.NewsResponse; +import igoMoney.BE.repository.NewsRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +@Slf4j +@Transactional +public class NewsService { + + private final NewsRepository newsRepository; + + public List getNews(Long lastId) { + + List responseList = new ArrayList<>(); + List latestNews; + latestNews = newsRepository.findAllByIdGreaterThan(lastId); + for (News news: latestNews){ + responseList.add(newsToNewsResponse(news)); + } + return responseList; + } + + private NewsResponse newsToNewsResponse(News news) { + return NewsResponse.builder() + .newsId(news.getId()) + .title(news.getTitle()) + .content(news.getContent()) + .date(LocalDate.from(news.getCreatedDate())) + .build(); + } +}