분석 결과를 기반으로 Spring 전환 체크리스트 작성 (E2E-MINIMAX-DEV-001)
This commit is contained in:
parent
322dcfe5c3
commit
26246428a3
1 changed files with 66 additions and 0 deletions
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.example.migration.service;
|
||||||
|
|
||||||
|
import com.example.migration.dto.SampleRequest;
|
||||||
|
import com.example.migration.dto.SampleResponse;
|
||||||
|
import com.example.migration.entity.SampleEntity;
|
||||||
|
import com.example.migration.exception.ResourceNotFoundException;
|
||||||
|
import com.example.migration.repository.SampleRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 샘플 서비스 - @Service 어노테이션으로 컴포넌트 스캔 대상
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public class SampleService {
|
||||||
|
|
||||||
|
private final SampleRepository sampleRepository;
|
||||||
|
|
||||||
|
public List<SampleResponse> getAllSamples() {
|
||||||
|
return sampleRepository.findAll().stream()
|
||||||
|
.map(this::toResponse)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SampleResponse getSampleById(Long id) {
|
||||||
|
SampleEntity entity = sampleRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("Sample", id));
|
||||||
|
return toResponse(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public SampleResponse createSample(SampleRequest request) {
|
||||||
|
SampleEntity entity = SampleEntity.builder()
|
||||||
|
.name(request.name())
|
||||||
|
.description(request.description())
|
||||||
|
.build();
|
||||||
|
SampleEntity saved = sampleRepository.save(entity);
|
||||||
|
return toResponse(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public SampleResponse updateSample(Long id, SampleRequest request) {
|
||||||
|
SampleEntity entity = sampleRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new ResourceNotFoundException("Sample", id));
|
||||||
|
entity.setName(request.name());
|
||||||
|
entity.setDescription(request.description());
|
||||||
|
return toResponse(sampleRepository.save(entity));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteSample(Long id) {
|
||||||
|
if (!sampleRepository.existsById(id)) {
|
||||||
|
throw new ResourceNotFoundException("Sample", id);
|
||||||
|
}
|
||||||
|
sampleRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SampleResponse toResponse(SampleEntity entity) {
|
||||||
|
return new SampleResponse(entity.getId(), entity.getName(), entity.getDescription());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue