22301102 | b108437 | 2025-06-01 16:44:23 +0800 | [diff] [blame] | 1 | package com.pt.service; |
| 2 | |
| 3 | import com.pt.entity.Resource; |
| 4 | import com.pt.repository.ResourceRepository; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.stereotype.Service; |
| 7 | |
| 8 | import java.time.LocalDateTime; |
| 9 | import java.util.List; |
| 10 | |
| 11 | @Service |
| 12 | public class ResourceService { |
| 13 | |
| 14 | @Autowired |
| 15 | private ResourceRepository resourceRepository; |
| 16 | |
| 17 | public ResourceService(ResourceRepository resourceRepository) { |
| 18 | this.resourceRepository = resourceRepository; |
| 19 | } |
| 20 | |
| 21 | public List<Resource> getAllResources() { |
| 22 | return resourceRepository.findAll(); |
| 23 | } |
| 24 | |
| 25 | public void publishResource(String name, String description, String publisher, double size) { |
| 26 | Resource resource = new Resource(); |
| 27 | resource.setName(name); |
| 28 | resource.setSize(size); |
| 29 | resource.setDescription(description); |
| 30 | resource.setAuthor(publisher); |
| 31 | resource.setPublishTime(LocalDateTime.now()); |
| 32 | resourceRepository.save(resource); |
| 33 | } |
| 34 | |
| 35 | public Resource getResourceById(int resourceId) { |
| 36 | return resourceRepository.findById(resourceId).orElse(null); |
| 37 | } |
| 38 | |
| 39 | public List<Resource> searchByQuery(String query) { |
| 40 | return resourceRepository.findByNameContainingIgnoreCase(query); |
| 41 | } |
| 42 | |
| 43 | public List<Resource> getResourcesByAuthor(String author) { |
| 44 | return resourceRepository.findByAuthor(author); |
| 45 | } |
| 46 | |
| 47 | public void deleteResource(int resourceId) { |
| 48 | resourceRepository.deleteById(resourceId); |
| 49 | } |
| 50 | } |