전체적인 패키지 및 파일 생성
API 설계
1. MemberController
package com.sparta.hanghae.controller;
import com.sparta.hanghae.dto.MemberRequestDto;
import com.sparta.hanghae.dto.MemberResponseDto;
import com.sparta.hanghae.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequiredArgsConstructor
public class MemberController {
private final MemberService memberService;
@PostMapping("/member")
public MemberResponseDto createMember(@RequestBody MemberRequestDto requestDto) {
return memberService.createMember(requestDto);
}
@GetMapping("/member")
public List<MemberResponseDto> getMemberList() {
return memberService.getMemberList();
}
@GetMapping("/member")
public ResponseDto getMemberList() {
try {
List<MemberDto> getMemberList() = memberService.findAllMember();
return new ManyMemberResponseDto(200, "OK!", allMember);
} catch (NullPointerException error) {
return new ResponseDto(500, error.getMessage());
}
@GetMapping("/member/{id}")
public MemberResponseDto getMemberInfo(@PathVariable Long id) {
return memberService.getMemberInfo(id);
}
@GetMapping("/{id}")
public ResponseDto getMemberInfo(@PathVariable Long id) {
try {
MemberDto member = MemberService.getMemberInfo(id);
return new MemberResponseDto(200, "OK!", member);
} catch (NullPointerException error) {
return new ResponseDto (500, error.getMessage());
}
}
}
}
2. MemberService
package com.sparta.hanghae.service;
import com.sparta.hanghae.dto.MemberRequestDto;
import com.sparta.hanghae.dto.MemberResponseDto;
import com.sparta.hanghae.entity.Member;
import com.sparta.hanghae.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service
@RequiredArgsConstructor
public class MemberService {
private final MemberRepository memberRepository;
@Transactional
public MemberResponseDto createMember(MemberRequestDto requestDto) {
Member member = new Member(requestDto);
memberRepository.save(member);
return new MemberResponseDto(member);
}
@Transactional(readOnly = true)
public List<MemberResponseDto> getMemberList() {
List<Member> memberList = memberRepository.findAllByOrderByModifiedAtDesc();
List<MemberResponseDto> memberResponseDto = new ArrayList<>();
if (memberList. isEmpty()) {
throw new NullPointerException("회원 상세 조회 실패");
}
for (Member member : memberList) {
memberResponseDto.add(new MemberResponseDto(member));
}
return memberResponseDto;
}
@Transactional(readOnly = true)
public MemberResponseDto getMemberInfo(Long id) {
Member member = memberRepository.findById(id).orElseThrow(
() -> new NullPointerException("회원 상세 조회 실패")
);
return new MemberResponseDto(member);
}
}
3. MemberRepository
package com.sparta.hanghae.repository;
import com.sparta.hanghae.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface MemberRepository extends JpaRepository<Member, Long> {
List<Member> findAllByOrderByModifiedAtDesc();
}
4. Member
package com.sparta.hanghae.entity;
import com.sparta.hanghae.dto.MemberRequestDto;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@Entity
@NoArgsConstructor
public class Member extends Timestamped {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String pw;
public Member(MemberRequestDto requestDto) {
this.name = requestDto.getName();
this.email = requestDto.getEmail();
this.pw = requestDto.getPw();
}
}
5. Timestamped
package com.sparta.hanghae.entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.time.LocalDateTime;
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class Timestamped {
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime modifiedAt;
}
6. MemberRequestDto
package com.sparta.hanghae.dto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class MemberRequestDto {
private String name;
private String email;
private String pw;
}
7. MemberResponseDto
package com.sparta.hanghae.dto;
import com.sparta.hanghae.entity.Member;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class MemberResponseDto {
private Long id;
private String name;
private String email;
private String pw;
protected int statusCode;
protected String message;
public MemberResponseDto(Member member) {
this.id = member.getId();
this.name = member.getName();
this.email = member.getEmail();
this.pw = member.getPw();
}
}