-
Notifications
You must be signed in to change notification settings - Fork 4
과제 1-1 jihoonwjj 과제제출 #3
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: task/hjh
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,50 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.controller; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.dto.AnggimoddiDTO; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.service.DeletePost; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.service.GetPost; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.service.Postpost; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.service.UpdatePost; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RestController | ||
@RequestMapping("/articles") | ||
@RequiredArgsConstructor | ||
public class AnggimoddiController { | ||
|
||
private final GetPost getPost; | ||
private final Postpost postpost; | ||
private final UpdatePost updatePost; | ||
private final DeletePost deletePost; | ||
|
||
@GetMapping("/") | ||
public ResponseEntity<List<Ang>> getShitAll() { | ||
return getPost.getAngs(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<Optional<Ang>> getThatShit(@PathVariable Long id) { | ||
return getPost.getAngById(id); | ||
} | ||
|
||
@PostMapping("/") | ||
public ResponseEntity<Ang> makeShit(Ang ang) { | ||
return postpost.postPost(ang); | ||
} | ||
|
||
@PatchMapping(("/{id}")) | ||
public ResponseEntity<Ang> editThatShit(AnggimoddiDTO anggimoddiDTO, @PathVariable Long id) { | ||
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.
|
||
return updatePost.updatePost(anggimoddiDTO, id); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Ang> removeThatShit(@PathVariable Long id) { | ||
return deletePost.deleteAng(id); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Builder | ||
public class AnggimoddiDTO { | ||
private Long id; | ||
private String title; | ||
private String content; | ||
} | ||
Comment on lines
+8
to
+16
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,22 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Ang extends BaseIdxEntity{ | ||
private String title; | ||
private String content; | ||
|
||
@Builder | ||
public Ang(Long idx, String title, String content){ | ||
this.idx = idx; | ||
this.title = title; | ||
this.content = content; | ||
} | ||
} | ||
Comment on lines
+11
to
+22
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,9 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.repository; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface AngRepository extends JpaRepository<Ang, Long> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.service; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public interface DeletePost { | ||
ResponseEntity<Ang> deleteAng(Long id); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.service; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public interface GetPost { | ||
ResponseEntity<List<Ang>> getAngs(); | ||
ResponseEntity<Optional<Ang>> getAngById(Long id); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.service; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
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. 이 녀석 인터페이스에 |
||
public interface Postpost { | ||
ResponseEntity<Ang> postPost(Ang ang); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.service; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.dto.AnggimoddiDTO; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public interface UpdatePost { | ||
ResponseEntity<Ang> updatePost(AnggimoddiDTO angimoddiDTO, Long postId); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.service.impl; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.repository.AngRepository; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.service.DeletePost; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@RequiredArgsConstructor | ||
public class DeletePostImpl implements DeletePost { | ||
private final AngRepository angRepository; | ||
|
||
public ResponseEntity<Ang> deleteAng(Long id) { | ||
angRepository.deleteById(id); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.service.impl; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.repository.AngRepository; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.service.GetPost; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RequiredArgsConstructor | ||
public class GetPostImpl implements GetPost { | ||
private final AngRepository angRepository; | ||
|
||
public ResponseEntity<List<Ang>> getAngs() { | ||
try { | ||
return ResponseEntity.ok(angRepository.findAll()); | ||
} catch (IllegalArgumentException e) { | ||
List<Ang> emptyList = Collections.emptyList(); | ||
return ResponseEntity.ok(emptyList); | ||
} | ||
} | ||
Comment on lines
+17
to
+24
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. 같이 협업하는 클라이언트 마다 다르긴 했는데 대부분의 클라이언트는 List형 API에서는 값이 없으면 그냥 Empty List를 반환해 주는 것을 선호하더라고요 |
||
|
||
public ResponseEntity<Optional<Ang>> getAngById(Long id) { | ||
try { | ||
return ResponseEntity.ok(angRepository.findById(id)); | ||
} catch (IllegalArgumentException e) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.service.impl; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.repository.AngRepository; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.service.Postpost; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.util.Optional; | ||
|
||
@RequiredArgsConstructor | ||
public class PostpostImpl implements Postpost { | ||
private final AngRepository angRepository; | ||
|
||
public ResponseEntity<Ang> postPost(Ang ang) { | ||
return ResponseEntity.ok(angRepository.save(ang)); | ||
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. 이런 식으로 설계되어 있으면 오류 발생 시 500뜨고 말거나 이상한 값이 날라갈 거 같은데 예외 처리를 해주는건 어떨까요 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain.service.impl; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.dto.AnggimoddiDTO; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.repository.AngRepository; | ||
import com.gsm._8th.class4.backed.task._1._1.domain.service.UpdatePost; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
|
||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class UpdatePostImpl implements UpdatePost { | ||
private final AngRepository angRepository; | ||
|
||
public ResponseEntity<Ang> updatePost(AnggimoddiDTO anggimoddiDTO, Long postId) { | ||
try { | ||
Optional<Ang> ang = angRepository.findById(postId); | ||
Ang upAng = ang.get(); | ||
upAng.builder() | ||
.idx(anggimoddiDTO.getId()) | ||
.title(anggimoddiDTO.getTitle()) | ||
.content(anggimoddiDTO.getContent()) | ||
.build(); | ||
angRepository.save(upAng); | ||
return ResponseEntity.ok(upAng); | ||
} catch (IllegalArgumentException e) { | ||
return ResponseEntity.notFound().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.
GET /articles
나POST /articles
와 같은 상황에서는 ("/")로 명시하는 것보다 그냥 아무것도 안남기고@PostMapping
이렇게만 하는게 관례에 더 맞을 것 같습니다