-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |