Request 1개(ProfileCardUpdaterequest), certificates와 ProfileCard Response 2개(CertificatesResponse / ProfileCardResponse)
package com.likelion.step.profilecard.controller;
import com.likelion.step.profilecard.dto.ProfileCardResponse;
import com.likelion.step.profilecard.dto.ProfileCardUpdateRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/profile")
public class ProfileCardController {
private final ProfileCardService profileCardService;
@PatchMapping
public ProfileCardResponse updateProfileCard(@RequestBody ProfileCardUpdateRequest request) {
Long userId = 1L; // 유저 기능 구현 뒤 수정
return profileCardService.updateProfileCard(userId, request);
}
}
이 클래스가 REST API를 처리하는 컨트롤러라는 뜻
망약 수정 요청이 들어오면 여기있는 메서드가 실행된다.
@Controller가 html 페이지를 반환하는 용도로 많이 쓴다면 @RestController는 Json을 반환하는 API 용도라고 할 수 있다.
final이 붙은 변수만 생성자로 만들어주는 lombok 어노테이션
이 controller의 기본 주소를 알려주는 어노테이션
HPPT PATCH 요청(일부 수정)을 처리하는 메서드라는 뜻
요청으로 들어온 Json을 자배 객체로 바꿔서 받아주는 어노테이션
-> 요청 본문(JSON)을 ProfileCardUpdateRequest 객체로 변환해서 request라는 변수에 담아줘
ProfileCard / Certificates
package com.likelion.step.profilecard.entity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@Entity
@NoArgsConstructor
@Table(name = "profile_Card")
public class ProfileCard {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long profileCardId;
private String collaborationTags;
private String selfIntroduce;
private Long userId;
public ProfileCard(String collaborationTags, String selfIntroduce, Long userId) {
this.collaborationTags = collaborationTags;
this.selfIntroduce = selfIntroduce;
this.userId = userId;
}
public void updateProfileCard(String collaborationTags, String selfIntroduce) {
this.collaborationTags = collaborationTags;
this.selfIntroduce = selfIntroduce;
}
}
기본키(PK)
기본키 자동생성 어노테이션
괄호 속은 자동생성 방식 중 하나 -> DB의 AUTO_INCREMENT 기능을 사용해서 번호를 증가시켜라
이미 존재하는 객체를 수정하는 메서드
void - 값을 반환하지 않음(저장, 수정, 삭제)
String, int, ProfielResponse - 작업 후 결과를 돌려줌
package com.likelion.step.profilecard.entity;
import lombok.NoArgsConstructor;
import lombok.Getter;
import jakarta.persistence.*;
@Entity
@Getter
@NoArgsConstructor
@Table(name ="certificates")
public class Certificates {
@Id
@GeneratedValue
private Long certificatesId;
private String certificates;
private Long profileCardId;
public Certificates(String certificates, Long profileCardId) {
this.certificates = certificates;
this.profileCardId = profileCardId;
}
}
package com.likelion.step.profilecard.repository;
import com.likelion.step.profilecard.entity.ProfileCard;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface ProfileRepository extends JpaRepository<ProfileCard, Long> {
Optional<ProfileCard> findByUserId(Long userId);
}
JpaRepository<엔티티, PK자료형>
-> ProfileCard 엔티티 관리하는 레포고 기본키 타입은 Long이다.
lombok vs JpaRepository
둘다 개발자가 써야할 코드를 자동으로 만들어준다는 공통점이 있지만
lombok - 생성자, Getter, Setter 등 (자바 코드 자동 생성)
Spring Data JPA - SQL과 Repository 메서드 (SQL 자동 생성)
find -> 조회한다.
ByUserId -> userID 컬럼을 통해
Optinal - null을 직접 반환하지 않아 오류를 방지
없는 사용자일 경우 오류가 발생하는 것을 막을 수 있음
package com.likelion.step.profilecard.repository;
import com.likelion.step.profilecard.entity.Certificates;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CertificatesRepsitory extends JpaRepository<Certificates, Long> {
void deleteByProfileCardId(Long profileCardId);
}
삭제 기능 담당
따라서 find가 아닌 delete를 사용