Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions demo/src/main/java/org/sopt/demo/DemoApplication.java
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 demo/src/main/java/org/sopt/demo/controller/MemberController.java
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());
}

}
43 changes: 43 additions & 0 deletions demo/src/main/java/org/sopt/demo/domain/Member.java
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();
}
}
5 changes: 5 additions & 0 deletions demo/src/main/java/org/sopt/demo/domain/Part.java
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
}
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

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가 있어서 굳이 적지 않아도 됩니다!

public interface MemberRepository extends JpaRepository<Member, Long> {
}
48 changes: 48 additions & 0 deletions demo/src/main/java/org/sopt/demo/service/MemberService.java
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

Choose a reason for hiding this comment

The 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 demo/src/main/java/org/sopt/demo/service/dto/MemberCreateDto.java
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 demo/src/main/java/org/sopt/demo/service/dto/MemberFindDto.java
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());
}
}

1 change: 1 addition & 0 deletions demo/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=demo
13 changes: 13 additions & 0 deletions demo/src/test/java/org/sopt/demo/DemoApplicationTests.java
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 demo/src/test/java/org/sopt/demo/controller/TestController.java
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");
// }
}



1 change: 1 addition & 0 deletions demo/src/test/java/org/sopt/demo/settings/Apitest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/**/