-
Notifications
You must be signed in to change notification settings - Fork 0
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
2차 과제 코드 #2
Open
eunseo5343
wants to merge
1
commit into
main
Choose a base branch
from
seminar/2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
2차 과제 코드 #2
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 @@ | ||
package org.sopt.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class DemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DemoApplication.class, args); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
demo/src/main/java/org/sopt/demo/controller/MemberController.java
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,52 @@ | ||
package org.sopt.demo.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.sopt.demo.service.MemberService; | ||
import org.sopt.demo.service.dto.MemberCreateDto; | ||
import org.sopt.demo.service.dto.MemberFindDto; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/member") | ||
public class MemberController { | ||
|
||
private final MemberService memberService; | ||
|
||
@PostMapping | ||
public ResponseEntity createMember( | ||
@RequestBody MemberCreateDto memberCreateDto | ||
) { | ||
return ResponseEntity.created(URI.create(memberService.createMember(memberCreateDto))) | ||
.build(); | ||
} | ||
|
||
@GetMapping("/{memberId}") | ||
public ResponseEntity<MemberFindDto> findMemberById(@PathVariable Long memberId) { | ||
return ResponseEntity.ok(memberService.findMemberById(memberId)); | ||
} | ||
|
||
@DeleteMapping("/{memberId}") | ||
public ResponseEntity deleteMemberById(@PathVariable Long memberId){ | ||
memberService.deleteMemberById(memberId); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List<MemberFindDto>> getAllMembers() { | ||
return ResponseEntity.ok(memberService.getAllMembers()); | ||
} | ||
|
||
} |
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,43 @@ | ||
package org.sopt.demo.domain; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor | ||
public class Member { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private Part part; | ||
|
||
private int age; | ||
|
||
@Builder | ||
private Member(String name, Part part, int age) { | ||
this.name = name; | ||
this.part = part; | ||
this.age = age; | ||
} | ||
|
||
public static Member create(String name, Part part, int age) { | ||
return Member.builder() | ||
.name(name) | ||
.age(age) | ||
.part(part) | ||
.build(); | ||
} | ||
} |
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,5 @@ | ||
package org.sopt.demo.domain; | ||
|
||
public enum Part { | ||
SERVER, IOS, ANDROID, WEB, DESIGN, PLAN | ||
} |
9 changes: 9 additions & 0 deletions
9
demo/src/main/java/org/sopt/demo/repository/MemberRepository.java
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,9 @@ | ||
package org.sopt.demo.repository; | ||
|
||
import org.sopt.demo.domain.Member; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
} |
48 changes: 48 additions & 0 deletions
48
demo/src/main/java/org/sopt/demo/service/MemberService.java
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,48 @@ | ||
package org.sopt.demo.service; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.demo.domain.Member; | ||
import org.sopt.demo.service.dto.MemberCreateDto; | ||
import org.sopt.demo.service.dto.MemberFindDto; | ||
import org.sopt.demo.repository.MemberRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MemberService { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
@Transactional | ||
public String createMember( | ||
MemberCreateDto memberCreateDto | ||
) { | ||
Member member = Member.create(memberCreateDto.name(), memberCreateDto.part(), memberCreateDto.age()); | ||
memberRepository.save(member); | ||
return member.getId().toString(); | ||
} | ||
|
||
public MemberFindDto findMemberById(Long memberId) { | ||
return MemberFindDto.of(memberRepository.findById(memberId).orElseThrow( | ||
() -> new EntityNotFoundException("ID에 해당하는 사용자가 존재하지 않습니다.") | ||
)); | ||
Comment on lines
+31
to
+33
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. memberRepository.findById(memberId).orElseThrow(
() -> new EntityNotFoundException("ID에 해당하는 사용자가 존재하지 않습니다.")
) 멤버 id로 멤버를 가져올 때 멤버가 존재하는지 확인하고 없으면 예외를 터뜨리는 로직은 재사용될 가능성이 높습니다. 그래서 MemberRepository 인터페이스에 default 메소드를 정의하거나 MemberService내에 해당 부분을 메소드로 따로 분리하는 것도 좋을 것 같습니당. |
||
} | ||
|
||
@Transactional | ||
public void deleteMemberById(Long memberId) { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new EntityNotFoundException("ID에 해당하는 사용자가 존재하지 않습니다.")); | ||
memberRepository.delete(member); | ||
} | ||
|
||
public List<MemberFindDto> getAllMembers() { | ||
return memberRepository.findAll().stream() | ||
.map(MemberFindDto::of) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
demo/src/main/java/org/sopt/demo/service/dto/MemberCreateDto.java
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 org.sopt.demo.service.dto; | ||
|
||
import org.sopt.demo.domain.Part; | ||
|
||
public record MemberCreateDto( | ||
String name, | ||
Part part, | ||
int age | ||
) { | ||
} |
15 changes: 15 additions & 0 deletions
15
demo/src/main/java/org/sopt/demo/service/dto/MemberFindDto.java
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,15 @@ | ||
package org.sopt.demo.service.dto; | ||
|
||
import org.sopt.demo.domain.Member; | ||
import org.sopt.demo.domain.Part; | ||
|
||
public record MemberFindDto( | ||
String name, | ||
Part part, | ||
int age | ||
) { | ||
public static MemberFindDto of(Member member) { | ||
return new MemberFindDto(member.getName(), member.getPart(), member.getAge()); | ||
} | ||
} | ||
|
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 @@ | ||
spring.application.name=demo |
13 changes: 13 additions & 0 deletions
13
demo/src/test/java/org/sopt/demo/DemoApplicationTests.java
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,13 @@ | ||
package org.sopt.demo; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@SpringBootTest | ||
class DemoApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
demo/src/test/java/org/sopt/demo/controller/TestController.java
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,21 @@ | ||
package org.sopt.demo.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/test") | ||
public class TestController { | ||
@GetMapping | ||
public String test(){ | ||
return "test Api"; | ||
} | ||
// @GetMapping("/test/json") | ||
// public ApiResponse testJson() { | ||
// return ApiResponse.create("1차 세미나 테스트 API - JSON"); | ||
// } | ||
} | ||
|
||
|
||
|
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 @@ | ||
/**/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
저도 이번 과제를 졸면서 했는지 별 생각 없이 @repository 어노테이션을 달아줬는데 저희가 MemberRepository에서 상속받는 JpaRepository의 구현체인 SimpleJpaRepository에 이미 @repository가 있어서 굳이 적지 않아도 됩니다!