Skip to content

Commit

Permalink
Feat: 공지사항 조회 API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
pingowl committed Dec 2, 2023
1 parent 5fc0b56 commit 3ffb730
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/main/generated/igoMoney/BE/domain/QNews.java
Original file line number Diff line number Diff line change
@@ -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<News> {

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<java.time.LocalDateTime> createdDate = _super.createdDate;

public final NumberPath<Long> id = createNumber("id", Long.class);

//inherited
public final DateTimePath<java.time.LocalDateTime> modifiedDate = _super.modifiedDate;

public final StringPath title = createString("title");

public QNews(String variable) {
super(News.class, forVariable(variable));
}

public QNews(Path<? extends News> path) {
super(path.getType(), path.getMetadata());
}

public QNews(PathMetadata metadata) {
super(News.class, metadata);
}

}

32 changes: 32 additions & 0 deletions src/main/java/igoMoney/BE/controller/NewsController.java
Original file line number Diff line number Diff line change
@@ -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<List<NewsResponse>> getNews(
@RequestParam(value="lastId", required = false, defaultValue = "-1") Long lastId ) {

List<NewsResponse> response = newsService.getNews(lastId);

return ResponseEntity.status(HttpStatus.OK).body(response);
}
}
23 changes: 23 additions & 0 deletions src/main/java/igoMoney/BE/domain/News.java
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions src/main/java/igoMoney/BE/dto/response/NewsResponse.java
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions src/main/java/igoMoney/BE/repository/NewsRepository.java
Original file line number Diff line number Diff line change
@@ -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<News, Long> {
List<News> findAllByIdGreaterThan(Long lastId);
}
42 changes: 42 additions & 0 deletions src/main/java/igoMoney/BE/service/NewsService.java
Original file line number Diff line number Diff line change
@@ -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<NewsResponse> getNews(Long lastId) {

List<NewsResponse> responseList = new ArrayList<>();
List<News> 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();
}
}

0 comments on commit 3ffb730

Please sign in to comment.