01package com.qingsheng.service;
02
03import java.util.List;
04import org.springframework.stereotype.Service;
05import org.springframework.transaction.annotation.Transactional;
06
07@Service
08public class ProjectService {
09 private final ProjectRepository projects;
10 private final CustomerRepository customers;
11
12 public ProjectService(ProjectRepository projects,
13 CustomerRepository customers) {
14 this.projects = projects;
15 this.customers = customers;
16 }
17
18 @Transactional(readOnly = true)
19 public Page<ProjectView> list(ProjectQuery query) {
20 var page = projects.findAll(query.toSpecification(),
21 PageRequest.of(query.page(), query.size()));
22 return page.map(ProjectView::from);
23 }
24
25 @Transactional
26 public ProjectView create(CreateProjectRequest request) {
27 var customer = customers.findById(request.customerId())
28 .orElseThrow(() -> new NotFoundException("客户不存在"));
29 var project = Project.create(customer, request.name());
30 project.assignTeam(request.members());
31 return ProjectView.from(projects.save(project));
32 }
33}