-
Notifications
You must be signed in to change notification settings - Fork 6
과제 1-1 jeongjongyun 과제제출 #7
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?
과제 1-1 jeongjongyun 과제제출 #7
Conversation
public @interface NotBlank { | ||
String message(); | ||
} |
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.
해당 어노테이션을 정의하신 이유가 무엇인가요?
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.
제가 @notblank 어노테이션을 쓰다가 인텔리제이에서 자동으로 추가해준 것을 못 보고 넣은거 같습니다.. 수정하겠습니다
import lombok.Getter; | ||
|
||
@Entity | ||
@Table(name = "task.1-1") |
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.
테이블 명으로 이러한 명칭은 적절하지 않을 것 같습니다.
실제 리소스를 상징할 수 있는 명칭으로 해주세요
public void update(String title, String content) { | ||
if(title != null) | ||
{ | ||
this.title = title; | ||
} | ||
if(content != null){ | ||
this.content = content; | ||
} | ||
} |
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.
update 메서드에서 이러한 null 검사를 하기보단 애초에 서비스 로직에서 null검사를 하는것이 더 깔끔하지 않을까요?
public ArticlesEntity() {} | ||
public ArticlesEntity( String title, String content) { | ||
this.title = title; | ||
this.content = content; | ||
} |
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.
생성자를 직접 정의하기 보단 Lombok에서 제공하는 생성자 생성 어노테이션들을 적용해주세요
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) |
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.
각 메서드에 트랜젝션을 별도로 선언하거나 하였다면 클래스단에서 선언하기 보단 메서드마다 알맞게 선언해주는게 좋을 것 같습니다
@RestController | ||
@RequestMapping("/articles") | ||
@RequiredArgsConstructor | ||
public class ArticlesController { | ||
private final ArticlesService articlesService; | ||
private final Articlesrepository articlesrepository; | ||
|
||
@GetMapping | ||
public ResponseEntity<List<ArticlesResponseDto>> getAriticles() { | ||
List<ArticlesResponseDto> articles = articlesService.getAll(); | ||
return ResponseEntity.ok(articles); | ||
} | ||
@GetMapping("/{id}") | ||
public ResponseEntity<ArticlesResponseDto> getArticles(@PathVariable Long id) { | ||
if(!articlesService.existsById(id)) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
ArticlesResponseDto articles = articlesService.getOne(id); | ||
return ResponseEntity.ok(articles); | ||
} | ||
@PostMapping | ||
public ResponseEntity<ArticlesResponseDto> createArticles(@Valid @RequestBody ArticlesRequestDto articles) { | ||
if (!articlesService.checkRequest(articles)) | ||
//유효성 검사에 실패 했을때 (checkRequest) | ||
return ResponseEntity.badRequest().build(); | ||
ArticlesResponseDto article = articlesService.saveArticle(articles); | ||
if (article == null) {// DB에 저장이 실패할때 | ||
return ResponseEntity.internalServerError().build(); | ||
} | ||
return ResponseEntity.status(201).body(article); | ||
} | ||
@PatchMapping("/{id}") | ||
public ResponseEntity<ArticlesResponseDto> update( | ||
@PathVariable Long id, @Valid @RequestBody ArticlesPatchRequestDto request) { | ||
if(!articlesService.existsById(id)) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
|
||
if(!articlesService.hasValidPatchData(request)) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
ArticlesResponseDto update = articlesService.updateArticle(id, request); | ||
return ResponseEntity.ok(update); | ||
} | ||
|
||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteArticles(@PathVariable Long id) { | ||
if(!articlesService.existsById(id)){ | ||
return ResponseEntity.notFound().build(); | ||
} | ||
articlesService.deleteById(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
|
||
|
||
} |
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.
이런 유효성 검사,존재 확인 및 레포지터리(영속계층) 사용은 비즈니스로직에 해당하는 것 같습니다.책임분리에 대해 고민해보시고 이러한 로직은 서비스로 옮기는게 좋을 것 같습니다
@GetMapping | ||
public ResponseEntity<List<ArticlesResponseDto>> getAriticles() { | ||
List<ArticlesResponseDto> articles = articlesService.getAll(); | ||
return ResponseEntity.ok(articles); | ||
} | ||
@GetMapping("/{id}") | ||
public ResponseEntity<ArticlesResponseDto> getArticles(@PathVariable Long id) { | ||
if(!articlesService.existsById(id)) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
ArticlesResponseDto articles = articlesService.getOne(id); | ||
return ResponseEntity.ok(articles); | ||
} | ||
@PostMapping | ||
public ResponseEntity<ArticlesResponseDto> createArticles(@Valid @RequestBody ArticlesRequestDto articles) { | ||
ArticlesResponseDto article = articlesService.saveArticle(articles); | ||
return ResponseEntity.status(201).body(article); | ||
} | ||
@PatchMapping("/{id}") | ||
public ResponseEntity<ArticlesResponseDto> update( | ||
@PathVariable Long id, @Valid @RequestBody ArticlesPatchRequestDto request) { | ||
ArticlesResponseDto update = articlesService.updateArticle(id, request); | ||
return ResponseEntity.ok(update); | ||
} | ||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> deleteArticles(@PathVariable Long id) { | ||
articlesService.deleteById(id); | ||
return ResponseEntity.noContent().build(); | ||
} |
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.
각 메서드 사이에 개행해주세요
.orElseThrow(() -> new IllegalArgumentException("해당글은 존재하지않습니다")); | ||
|
||
if(!hasValidPatchData(request)){ | ||
throw new IllegalArgumentException("수정할 데이터가 없습니다."); | ||
|
||
} | ||
Optional.ofNullable(request.getTitle()).ifPresent(entity::setTitle); | ||
Optional.ofNullable(request.getContent()).ifPresent(entity::setContent); | ||
ArticlesEntity savedEntity = articlesrepository.save(entity); | ||
return convertToResponseDto(savedEntity); | ||
} | ||
|
||
public void deleteById(Long id) { | ||
ArticlesEntity entity = articlesrepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("삭제할 글이 존재하지 않습니다.")); |
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.
IllegalArgumentException
을 발행하는 것이 아니라 커스텀 예외 클래스를 구현 후 GlobalExceptionHandler
를 구현하여 이를 캐치하는 구조로 만들면 좋지 않을까요?
No description provided.