๐ฌ
Controller - ์๋์๋ต๊ธฐ
Service - ์ ๋ฐ์ดํธ ์ฌ์ฉ
Repository - ์ง์ ์ฟผ๋ฆฌ ๋ ๋ฆผ
@RequestBody : http์์ฒญ์ ๋ณธ๋ฌธ(body)์ ๊ทธ๋๋ก ์ ๋ฌํ๋ค. json ๊ธฐ๋ฐ์ ๋ฉ์์ง๋ฅผ ์ฌ์ฉํ๋ ์์ฒญ์์ ์ฌ์ฉํ๋ค.
@RequiredArgsConstructor
@Service
public class PersonService {
private final PersonRepository personRepository;
@Transactional
public Long update(Long id, PersonRequestDto requestDto) {
Person person = personRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("์์ด๋๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.")
);
person.update(requestDto);
return id;
}
}
@RequriedArgsConstructor : ์ด๊ธฐํ๋์ง ์์ final ํ๋์ ๋ํด ์์ฑ์๋ฅผ ์์ฑํด์ค๋ค.
@Transactional : ๋ฉ์๋์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋์ db์ ๋ฐ์ํด์ค๋ค.
์ด๋ ธํ ์ด์ ์ด ๋๋ฌด ๋ง์์ ํท๊ฐ๋ฆฐ๋ค.๐
- Repository๋ฅผ ๋ง๋๋ ค๋ฉด ๋ญ๊ฐ ํ์ํ์ง ์์์ผ ํ๋ค.
- ๋ญ๊ฐ ํ์ํ์ง ์๊ธฐ ์ํด์๋ API๋ฅผ ์ค๊ณํด์ผ ํ๋ค.
๐ Repository๋ฅผ ๋ง๋ค๊ธฐ ์ ์ API ์ค๊ณํ๊ธฐ!
ํด๋์ค๋ฅผ ์์ ๋ฐ๋ ์์ ํด๋์ค์๊ฒ ๋งคํ ์ ๋ณด๋ง ์ ๊ณตํ๊ณ ์ถ์ ๋๋ @MappedSuperclass๋ฅผ ์ฌ์ฉํ๋ค.
์ถ์ํด๋์ค์ด๊ธฐ ๋๋ฌธ์ ์์์ด ๋์ด์ผ๋ง ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค. (new Timestamped() ๋ถ๊ฐ๋ฅ)
๋ด์ผ ๊ฐ์ ๋ ๋ฃ๊ณ ๋ค์ ์ ๋ฆฌํ๊ธฐ!
ํงํ !