-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] 다솜소식 API 구현 #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
9a6709d
7382b30
38def04
f936cc3
6db6309
c173492
4bd898a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package dmu.dasom.api.domain.news.controller; | ||
|
|
||
| import dmu.dasom.api.domain.news.dto.NewsRequestDto; | ||
| import dmu.dasom.api.domain.news.dto.NewsResponseDto; | ||
| import dmu.dasom.api.domain.news.service.NewsService; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import io.swagger.v3.oas.annotations.media.Content; | ||
| import io.swagger.v3.oas.annotations.media.ExampleObject; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
| import java.util.List; | ||
|
|
||
| @Tag(name = "NEWS API", description = "다솜소식 API") | ||
| @RestController | ||
| @RequestMapping("/news") | ||
| public class NewsController { | ||
|
|
||
| private final NewsService newsService; | ||
|
|
||
| public NewsController(NewsService newsService) { | ||
| this.newsService = newsService; | ||
| } | ||
|
|
||
| @Operation(summary = "소식 조회", description = "리스트로 조회") | ||
| @ApiResponse(responseCode = "200", description = "정상 응답", | ||
| content = @Content(mediaType = "application/json")) | ||
| @GetMapping | ||
| public ResponseEntity<List<NewsResponseDto>> getAllNews() { | ||
| List<NewsResponseDto> newsList = newsService.getAllNews(); | ||
| return ResponseEntity.ok(newsList); | ||
| } | ||
|
|
||
| @Operation(summary = "소식 등록", description = "새로운 소식을 등록") | ||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "201", description = "생성 완료"), | ||
| @ApiResponse(responseCode = "400", description = "유효하지 않은 요청 데이터", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| content = @Content(mediaType = "application/json", | ||
| examples = @ExampleObject(value = "{ \"errorCode\": \"E003\", \"errorMessage\": \"유효하지 않은 입력입니다.\" }"))) | ||
| }) | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<NewsResponseDto> createNews(@RequestBody NewsRequestDto requestDto) { | ||
| NewsResponseDto responseDto = newsService.createNews(requestDto); | ||
| return ResponseEntity.status(201).body(responseDto); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package dmu.dasom.api.domain.news.dto; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @Schema(description = "뉴스 요청 DTO") | ||
| public class NewsRequestDto { | ||
|
|
||
| @Schema(description = "뉴스 제목", example = "새로운 뉴스 제목") | ||
| @NotNull | ||
| private String title; | ||
|
|
||
| @Schema(description = "뉴스 내용", example = "새로운 뉴스 내용") | ||
| @NotNull | ||
| private String content; | ||
|
|
||
| @Schema(description = "뉴스 이미지 URL", example = "http://example.com/new-image.jpg") | ||
| private String imageUrl; | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package dmu.dasom.api.domain.news.dto; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import lombok.Getter; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Getter | ||
| @Schema(description = "응답 DTO") | ||
| public class NewsResponseDto { | ||
|
|
||
| @Schema(description = "소식 ID", example = "1") | ||
| private Long id; | ||
|
|
||
| @Schema(description = "제목", example = "제목") | ||
| private String title; | ||
|
|
||
| @Schema(description = "내용", example = "내용") | ||
| private String content; | ||
|
|
||
| @Schema(description = "작성일", example = "2025-02-14T12:00:00") | ||
| private LocalDateTime createdAt; | ||
|
|
||
| @Schema(description = "이미지 URL", example = "http://example.com/image.jpg") | ||
| private String imageUrl; | ||
|
|
||
| public NewsResponseDto(Long id, String title, String content, LocalDateTime createdAt, String imageUrl) { | ||
| this.id = id; | ||
| this.title = title; | ||
| this.content = content; | ||
| this.createdAt = createdAt; | ||
| this.imageUrl = imageUrl; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package dmu.dasom.api.domain.news.entity; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Getter | ||
| @Setter | ||
|
||
| @Entity | ||
| @Schema(description = "뉴스 엔티티") | ||
| public class NewsEntity { | ||
|
||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Schema(description = "뉴스 ID", example = "1") | ||
| private Long id; | ||
|
|
||
| @Schema(description = "뉴스 제목", example = "뉴스 예제 제목") | ||
| @NotNull | ||
| private String title; | ||
|
|
||
| @Schema(description = "뉴스 내용", example = "뉴스 예제 내용") | ||
| @NotNull | ||
| @Column(columnDefinition = "TEXT") | ||
| private String content; | ||
|
|
||
| @Schema(description = "뉴스 작성일", example = "2025-02-14T12:00:00") | ||
| private LocalDateTime createdAt; | ||
|
|
||
| @Schema(description = "뉴스 이미지 URL", example = "http://example.com/image.jpg") | ||
| private String imageUrl; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이미지는 외부 DB에 저장하는 건가요? |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package dmu.dasom.api.domain.news.repository; | ||
|
|
||
| import dmu.dasom.api.domain.news.entity.NewsEntity; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface NewsRepository extends JpaRepository<NewsEntity, Long> { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package dmu.dasom.api.domain.news.service; | ||
|
|
||
| import dmu.dasom.api.domain.news.dto.NewsRequestDto; | ||
| import dmu.dasom.api.domain.news.dto.NewsResponseDto; | ||
| import dmu.dasom.api.domain.news.entity.NewsEntity; | ||
| import dmu.dasom.api.domain.news.repository.NewsRepository; | ||
| import org.springframework.stereotype.Service; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| public class NewsService { | ||
|
|
||
| private final NewsRepository newsRepository; | ||
|
|
||
| public NewsService(NewsRepository newsRepository) { | ||
| this.newsRepository = newsRepository; | ||
| } | ||
|
|
||
| public List<NewsResponseDto> getAllNews() { | ||
| return newsRepository.findAll().stream() | ||
| .map(news -> new NewsResponseDto(news.getId(), news.getTitle(), news.getContent(), news.getCreatedAt(), news.getImageUrl())) | ||
|
||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public NewsResponseDto createNews(NewsRequestDto requestDto) { | ||
| NewsEntity news = new NewsEntity(); | ||
|
||
| news.setTitle(requestDto.getTitle()); | ||
| news.setContent(requestDto.getContent()); | ||
| news.setImageUrl(requestDto.getImageUrl()); | ||
| news.setCreatedAt(LocalDateTime.now()); | ||
|
|
||
| NewsEntity savedNews = newsRepository.save(news); | ||
| return new NewsResponseDto(savedNews.getId(), savedNews.getTitle(), savedNews.getContent(), savedNews.getCreatedAt(), savedNews.getImageUrl()); | ||
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
엔드포인트는
/api로 시작하는 걸로 통일할게용(ex. /api/news)