@RestController
@RequiredArgsConstructor
public class MemberApiController {
@PutMapping("/api/v2/members/{id}")
public UpdateMemberResponse updateMemberV2(@PathVariable("id") Long id,
@RequestBody @Valid UpdateMemberRequest request) {
memberService.update(id, request.getName());
// 트랜잭션이 끝난 후 다시 커리해서 가져온다.
Member member = memberService.findOne(id);
return new UpdateMemberResponse(member.getId(), member.getName());
}
@Data
static class UpdateMemberRequest {
private String name;
}
@Data
@AllArgsConstructor
static class UpdateMemberResponse {
private Long id;
private String name;
}
}
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MemberService {
@Transactional
public void update(Long id, String name) {
Member member = memberRepository.findOne(id);
// 변경 감지를 사용한다.
member.setName(name);
}
}