▸ 코드 카타
▸ CRUD
2024년 1월 25일 - [프로그래머스] 13 : 과일 장수
1. 프로젝트 생성
2. 5개의 패키지 생성
3. Entity
package com.sparta.members.entity;
import com.sparta.members.dto.MemberRequestDto;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Entity
@NoArgsConstructor
@Table(name = "member")
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "userid", nullable = false)
private String userid;
@Column(name = "userpassword", nullable = false)
private String userpassword;
public Member(MemberRequestDto memberRequestDto){
this.userid = memberRequestDto.getUserid();
this.username = memberRequestDto.getUsername();
this.userpassword = memberRequestDto.getUserpassword();
}
public void update(MemberRequestDto memberRequestDto){
this.userid = memberRequestDto.getUserid();
this.username = memberRequestDto.getUsername();
this.userpassword = memberRequestDto.getUserpassword();
}
}
4. Dto
package com.sparta.members.dto;
import lombok.Getter;
@Getter
public class MemberRequestDto {
private String username;
private String userid;
private String userpassword;
}
package com.sparta.members.dto;
import com.sparta.members.entity.Member;
import lombok.Getter;
@Getter
public class MemberResponseDto {
private Long id;
private String username;
private String userid;
private String userpassword;
public MemberResponseDto(Member member){
this.id = member.getId();
this.username = member.getUsername();
this.userid = member.getUserid();
this.userpassword = member.getUserpassword();
}
}
5. Controller
package com.sparta.members.controller;
import com.sparta.members.dto.MemberRequestDto;
import com.sparta.members.dto.MemberResponseDto;
import com.sparta.members.entity.Member;
import com.sparta.members.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequiredArgsConstructor
public class MemberController {
private final MemberService memberService;
@GetMapping("/members")
public List<MemberResponseDto> getMembers(){
return memberService.getMembers();
}
@PostMapping("/members")
public MemberResponseDto saveMember(@RequestBody MemberRequestDto memberRequestDto){
return memberService.saveMember(memberRequestDto);
}
@PutMapping("/members/{membersid}")
public MemberResponseDto updateMember(@PathVariable Long membersid, @RequestBody MemberRequestDto memberRequestDto){
return memberService.updateMember(id, memberRequestDto);
}
@DeleteMapping("/members/{membersid}")
public void deleteMember(@PathVariable Long membersid){
memberService.deleteMember(id);
}
}
6. Service
package com.sparta.members.service;
import com.sparta.members.dto.MemberRequestDto;
import com.sparta.members.dto.MemberResponseDto;
import com.sparta.members.entity.Member;
import com.sparta.members.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@RequiredArgsConstructor
public class MemberService {
private final MemberRepository memberRepository;
public List<MemberResponseDto> getMembers() {
return memberRepository.findAll().stream().map(MemberResponseDto::new).toList();
}
@Transactional
public MemberResponseDto saveMember(MemberRequestDto memberRequestDto) {
//RequestDto -> Entity
Member member = new Member(memberRequestDto);
//DB저장 과 Entity -> Response Dto
return new MemberResponseDto(memberRepository.save(member));
}
@Transactional
public MemberResponseDto updateMember(Long id, MemberRequestDto memberRequestDto) {
Member member = findMember(id);
member.update(memberRequestDto);
return new MemberResponseDto(member);
}
@Transactional
public void deleteMember(Long id) {
Member member = findMember(id);
memberRepository.delete(member);
}
private Member findMember(Long id){
return memberRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("선택한 멤버는 존재하지 않습니"));
}
}
7. Repository
package com.sparta.members.repository;
import com.sparta.members.dto.MemberResponseDto;
import com.sparta.members.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MemberRepository extends JpaRepository<Member, Long> {
}
오류 해결
@PutMapping("/members/{membersid❗️}")
public MemberResponseDto updateMember(@PathVariable Long membersid❗️, @RequestBody MemberRequestDto memberRequestDto){
return memberService.updateMember(id, memberRequestDto);
}