-
Notifications
You must be signed in to change notification settings - Fork 6
과제 1-1 hongjm0912 과제제출 #6
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,23 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.DTO; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public class ArticleDTO { | ||
private Long idx; | ||
private String title; | ||
private String content; | ||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
private LocalDateTime createdAt; | ||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") | ||
private LocalDateTime updatedAt; | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.controller; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.DTO.ArticleDTO; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.service.ArticleService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/articles") | ||
@RequiredArgsConstructor | ||
public class ArticleController { | ||
|
||
private final ArticleService articleService; | ||
|
||
@PostMapping | ||
public ResponseEntity<String> posting(@RequestBody ArticleDTO dto) { | ||
articleService.savePost(dto); | ||
return ResponseEntity.status(201).body("Article 리소스 생성에 성공함"); | ||
} | ||
|
||
|
||
@GetMapping | ||
public ResponseEntity<List<ArticleDTO>> read() { | ||
List<ArticleDTO> list = articleService.findList(); | ||
return ResponseEntity.ok(list); | ||
} | ||
|
||
@GetMapping("/{idx}") | ||
public ResponseEntity<ArticleDTO> reading(@PathVariable Long idx) { | ||
ArticleDTO post = articleService.readPost(idx); | ||
return ResponseEntity.ok(post); | ||
} | ||
|
||
@PatchMapping("/{idx}") | ||
public ResponseEntity<String> sujeong( | ||
@PathVariable Long idx, | ||
@RequestBody ArticleDTO dto) { | ||
articleService.sujeong(idx, dto); | ||
return ResponseEntity.ok("게시글이 수정되었습니다."); | ||
} | ||
|
||
@DeleteMapping("/{idx}") | ||
public ResponseEntity<String> deleteByTitle(@PathVariable Long idx) { | ||
articleService.sakjeByTitle(idx); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.entity; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.global.entity.BaseIdxEntity; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Table(name = "articles") | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public class ArticleEntity extends BaseIdxEntity { | ||
|
||
@Column(nullable = false) | ||
private String title; | ||
|
||
@Column(nullable = false) | ||
private String content; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ResponseEntity<String> handleNotFound(IllegalArgumentException ex) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage()); | ||
} | ||
} | ||
Comment on lines
+11
to
+15
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. 이렇게 핸들링하면 모든 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.repository; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.ArticleEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ArticleRepository extends JpaRepository<ArticleEntity, Long> { | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.service; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.DTO.ArticleDTO; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.ArticleEntity; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.repository.ArticleRepository; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class ArticleService { | ||
private final ArticleRepository articleRepository; | ||
|
||
public void savePost(ArticleDTO ArticleDTO) { | ||
ArticleEntity articleEntity = ArticleEntity.builder() | ||
.title(ArticleDTO.getTitle()) | ||
.content(ArticleDTO.getContent()) | ||
.build(); | ||
articleRepository.save(articleEntity); | ||
} | ||
|
||
public List<ArticleDTO> findList() { | ||
List<ArticleEntity> posts = articleRepository.findAll(); | ||
if (posts.isEmpty()) { | ||
return Collections.emptyList(); // 데이터가 없으면 200 OK + 빈 리스트([]) 반환 | ||
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. 따로 Empty를 감지하고, Collections.emptyList()를 반환한 이유가 무엇인가요? |
||
} | ||
return posts.stream() | ||
.map(post -> new ArticleDTO( | ||
post.getIdx(), | ||
post.getTitle(), | ||
post.getContent(), | ||
post.getCreatedAt(), | ||
post.getUpdatedAt() | ||
)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public ArticleDTO readPost(Long idx) { | ||
ArticleEntity post = articleRepository.findById(idx) | ||
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 게시글입니다.")); | ||
|
||
return new ArticleDTO( | ||
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. 정말 필요한 수정 사항은 아니지만, Entity to DTO를 담당하는 코드를 함수로 분리하는것도 좋은방법일것 같습니다. |
||
post.getIdx(), | ||
post.getTitle(), | ||
post.getContent(), | ||
post.getCreatedAt(), | ||
post.getUpdatedAt() | ||
); | ||
} | ||
|
||
public void sujeong(Long idx, ArticleDTO dto) { | ||
ArticleEntity post = articleRepository.findById(idx) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다.")); | ||
|
||
post.setTitle(dto.getTitle()); | ||
post.setContent(dto.getContent()); | ||
Comment on lines
+63
to
+64
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. Setter(설정자) 사용은 지양하는게 좋습니다! Entity에서의 사용은 더더욱 지양해야하고요. |
||
|
||
articleRepository.save(post); | ||
} | ||
|
||
public void sakjeByTitle(Long idx) { | ||
ArticleEntity post = articleRepository.findById(idx) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 ID의 게시글이 존재하지 않습니다.")); | ||
|
||
articleRepository.delete(post); | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
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.
메서드명 영어로 올바르게 통일해주세요