Skip to content

Commit ec823f7

Browse files
committed
#165: added filter options for get work
1 parent eee8ff7 commit ec823f7

File tree

3 files changed

+30
-48
lines changed

3 files changed

+30
-48
lines changed

src/main/java/de/doubleslash/keeptime/model/repos/WorkRepository.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@
1616

1717
package de.doubleslash.keeptime.model.repos;
1818

19-
import java.time.LocalDate;
20-
import java.util.List;
21-
19+
import de.doubleslash.keeptime.model.Work;
2220
import org.springframework.data.jpa.repository.JpaRepository;
2321
import org.springframework.data.jpa.repository.Query;
22+
import org.springframework.data.repository.query.Param;
2423
import org.springframework.stereotype.Repository;
2524

26-
import de.doubleslash.keeptime.model.Work;
25+
import java.time.LocalDate;
26+
import java.util.List;
2727

2828
@Repository
2929
public interface WorkRepository extends JpaRepository<Work, Long> {
3030

3131
@Query(value = "SELECT w FROM Work w WHERE CAST(startTime AS DATE) = ?1 ORDER BY startTime ASC")
3232
List<Work> findByStartDateOrderByStartTimeAsc(LocalDate creationDate);
3333

34-
List<Work> findByProjectId(long id);
34+
@Query("SELECT w FROM Work w WHERE " + "(:projectId IS NULL OR w.project.id = :projectId) "
35+
+ "AND (:minStartTime IS NULL OR CAST(w.startTime AS DATE) >= :minStartTime) "
36+
+ "AND (:maxStartTime IS NULL OR CAST(w.startTime AS DATE) <= :maxStartTime)")
37+
List<Work> findWorkItems(@Param("projectId") Long projectId, @Param("minStartTime") LocalDate minStartTime,
38+
@Param("maxStartTime") LocalDate maxStartTime);
3539
}

src/main/java/de/doubleslash/keeptime/rest/controller/ProjectController.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,6 @@
1616

1717
package de.doubleslash.keeptime.rest.controller;
1818

19-
import java.util.List;
20-
import java.util.Optional;
21-
22-
import org.springframework.dao.DataAccessException;
23-
import org.springframework.http.HttpStatus;
24-
import org.springframework.http.ResponseEntity;
25-
import org.springframework.web.bind.annotation.DeleteMapping;
26-
import org.springframework.web.bind.annotation.GetMapping;
27-
import org.springframework.web.bind.annotation.PathVariable;
28-
import org.springframework.web.bind.annotation.PostMapping;
29-
import org.springframework.web.bind.annotation.PutMapping;
30-
import org.springframework.web.bind.annotation.RequestBody;
31-
import org.springframework.web.bind.annotation.RequestMapping;
32-
import org.springframework.web.bind.annotation.RequestParam;
33-
import org.springframework.web.bind.annotation.ResponseStatus;
34-
import org.springframework.web.bind.annotation.RestController;
35-
import org.springframework.web.server.ResponseStatusException;
36-
3719
import de.doubleslash.keeptime.controller.Controller;
3820
import de.doubleslash.keeptime.model.Model;
3921
import de.doubleslash.keeptime.model.Project;
@@ -46,6 +28,14 @@
4628
import de.doubleslash.keeptime.rest.mapper.ProjectMapper;
4729
import de.doubleslash.keeptime.rest.mapper.WorkMapper;
4830
import jakarta.validation.Valid;
31+
import org.springframework.dao.DataAccessException;
32+
import org.springframework.http.HttpStatus;
33+
import org.springframework.http.ResponseEntity;
34+
import org.springframework.web.bind.annotation.*;
35+
import org.springframework.web.server.ResponseStatusException;
36+
37+
import java.util.List;
38+
import java.util.Optional;
4939

5040
@RestController
5141
@RequestMapping("/api/projects")
@@ -93,7 +83,7 @@ public ResponseEntity<List<ProjectDTO>> getProjectColorDTOsByName(
9383

9484
@GetMapping("/{id}/works")
9585
public List<WorkDTO> getWorksFromProject(@PathVariable final long id) {
96-
return workRepository.findByProjectId(id).stream().map(workMapper::workToWorkDTO).toList();
86+
return workRepository.findWorkItems(id, null, null).stream().map(workMapper::workToWorkDTO).toList();
9787
}
9888

9989
@PostMapping

src/main/java/de/doubleslash/keeptime/rest/controller/WorksController.java

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,20 @@
1616

1717
package de.doubleslash.keeptime.rest.controller;
1818

19-
import java.util.List;
20-
import java.util.Optional;
21-
import java.util.stream.Stream;
22-
23-
import org.springframework.http.HttpStatus;
24-
import org.springframework.http.ResponseEntity;
25-
import org.springframework.web.bind.annotation.DeleteMapping;
26-
import org.springframework.web.bind.annotation.GetMapping;
27-
import org.springframework.web.bind.annotation.PathVariable;
28-
import org.springframework.web.bind.annotation.PutMapping;
29-
import org.springframework.web.bind.annotation.RequestBody;
30-
import org.springframework.web.bind.annotation.RequestMapping;
31-
import org.springframework.web.bind.annotation.RequestParam;
32-
import org.springframework.web.bind.annotation.RestController;
33-
3419
import de.doubleslash.keeptime.model.Model;
3520
import de.doubleslash.keeptime.model.Project;
3621
import de.doubleslash.keeptime.model.Work;
3722
import de.doubleslash.keeptime.model.repos.ProjectRepository;
3823
import de.doubleslash.keeptime.model.repos.WorkRepository;
3924
import de.doubleslash.keeptime.rest.DTO.WorkDTO;
4025
import de.doubleslash.keeptime.rest.mapper.WorkMapper;
26+
import org.springframework.http.HttpStatus;
27+
import org.springframework.http.ResponseEntity;
28+
import org.springframework.web.bind.annotation.*;
29+
30+
import java.time.LocalDate;
31+
import java.util.List;
32+
import java.util.Optional;
4133

4234
@RestController
4335
@RequestMapping("/api/works")
@@ -57,15 +49,11 @@ public WorksController(final WorkRepository workRepository, final ProjectReposit
5749
}
5850

5951
@GetMapping
60-
public List<WorkDTO> getWorks(@RequestParam(name = "name", required = false) final String projectName) {
61-
List<Work> works = workRepository.findAll();
62-
63-
Stream<Work> workStream = works.stream();
64-
65-
if (projectName != null) {
66-
workStream = workStream.filter(work -> work.getProject().getName().equals(projectName));
67-
}
68-
return workStream.map(workMapper::workToWorkDTO).toList();
52+
public List<WorkDTO> getWorks(@RequestParam(name = "id", required = false) final Long projectId,
53+
@RequestParam(name = "fromDate", required = false) final LocalDate fromDate,
54+
@RequestParam(name = "toDate", required = false) final LocalDate toDate) {
55+
List<Work> works = workRepository.findWorkItems(projectId, fromDate, toDate);
56+
return works.stream().map(workMapper::workToWorkDTO).toList();
6957
}
7058

7159
@PutMapping("/{id}")

0 commit comments

Comments
 (0)