|
| 1 | +package com.example.demo.service; |
| 2 | + |
| 3 | +import com.datastax.astra.client.core.query.Filter; |
| 4 | +import com.datastax.astra.client.core.query.Filters; |
| 5 | +import com.example.demo.model.{{CLASS_NAME}}; |
| 6 | +import com.example.demo.repository.{{CLASS_NAME}}Repository; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.springframework.data.domain.*; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | + |
| 12 | +import java.time.Instant; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +/** |
| 17 | + * Service layer for {{CLASS_NAME}} business logic. |
| 18 | + */ |
| 19 | +@Slf4j |
| 20 | +@Service |
| 21 | +@RequiredArgsConstructor |
| 22 | +public class {{CLASS_NAME}}Service { |
| 23 | + |
| 24 | + private final {{CLASS_NAME}}Repository repository; |
| 25 | + |
| 26 | + /** |
| 27 | + * Create a new entity. |
| 28 | + */ |
| 29 | + public {{CLASS_NAME}} create({{CLASS_NAME}} entity) { |
| 30 | + entity.setCreatedAt(Instant.now()); |
| 31 | + entity.setUpdatedAt(Instant.now()); |
| 32 | + log.info("Creating entity: {}", entity); |
| 33 | + return repository.save(entity); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Find entity by ID. |
| 38 | + */ |
| 39 | + public Optional<{{CLASS_NAME}}> findById(String id) { |
| 40 | + return repository.findById(id); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Find all entities. |
| 45 | + */ |
| 46 | + public List<{{CLASS_NAME}}> findAll() { |
| 47 | + return (List<{{CLASS_NAME}}>) repository.findAll(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Update an existing entity. |
| 52 | + */ |
| 53 | + public {{CLASS_NAME}} update(String id, {{CLASS_NAME}} updates) { |
| 54 | + {{CLASS_NAME}} entity = repository.findById(id) |
| 55 | + .orElseThrow(() -> new ResourceNotFoundException("Entity not found: " + id)); |
| 56 | + |
| 57 | + // Update fields |
| 58 | + // entity.setField1(updates.getField1()); |
| 59 | + // entity.setField2(updates.getField2()); |
| 60 | + entity.setUpdatedAt(Instant.now()); |
| 61 | + |
| 62 | + return repository.save(entity); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Delete entity by ID. |
| 67 | + */ |
| 68 | + public void delete(String id) { |
| 69 | + repository.deleteById(id); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Find entities with pagination. |
| 74 | + */ |
| 75 | + public Page<{{CLASS_NAME}}> findAllPaginated(int page, int size, String sortBy) { |
| 76 | + Pageable pageable = PageRequest.of(page, size, Sort.by(sortBy)); |
| 77 | + return repository.findAll(Example.of(new {{CLASS_NAME}}()), pageable); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Search entities using Data API filters. |
| 82 | + */ |
| 83 | + public List<{{CLASS_NAME}}> search(String field, String value) { |
| 84 | + Filter filter = Filters.eq(field, value); |
| 85 | + return (List<{{CLASS_NAME}}>) repository.findAll(filter); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +class ResourceNotFoundException extends RuntimeException { |
| 90 | + public ResourceNotFoundException(String message) { |
| 91 | + super(message); |
| 92 | + } |
| 93 | +} |
0 commit comments