Skip to content

Commit 20b8444

Browse files
committed
#165: switched some if statements to be easier redable. started to make sure rest-requests do not cause errors in ui
1 parent 884c060 commit 20b8444

File tree

4 files changed

+59
-61
lines changed

4 files changed

+59
-61
lines changed

pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@
5353
<artifactId>mapstruct</artifactId>
5454
<version>1.5.5.Final</version>
5555
</dependency>
56-
<dependency>
57-
<groupId>javax.validation</groupId>
58-
<artifactId>validation-api</artifactId>
59-
<version>1.1.0.Final</version>
60-
</dependency>
6156
<dependency>
6257
<groupId>org.mapstruct</groupId>
6358
<artifactId>mapstruct-processor</artifactId>

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

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626
import de.doubleslash.keeptime.model.Work;
2727
import de.doubleslash.keeptime.model.repos.ProjectRepository;
2828
import de.doubleslash.keeptime.model.repos.WorkRepository;
29+
import javafx.application.Platform;
2930
import org.springframework.dao.DataAccessException;
3031
import org.springframework.http.HttpStatus;
3132
import org.springframework.http.ResponseEntity;
3233
import org.springframework.web.bind.annotation.*;
3334
import org.springframework.web.server.ResponseStatusException;
3435

35-
import javax.validation.Valid;
36+
import jakarta.validation.Valid;
3637
import java.util.List;
3738
import java.util.Optional;
3839
import java.util.stream.Collectors;
@@ -102,8 +103,9 @@ public WorkDTO getWorkByIdFromProject(@PathVariable final long projectId, @PathV
102103
.findFirst()
103104
.map(workMapper::workToWorkDTO)
104105
.orElseThrow(() -> new ResourceNotFoundException(
105-
"Work with id '" + workId + "' related to project with id '" + projectId
106-
+ "' not found"));
106+
"Work with id '" + workId + "' related to project with id '"
107+
+ projectId
108+
+ "' not found"));
107109
}
108110

109111
@PostMapping("")
@@ -113,10 +115,7 @@ public ResponseEntity<ProjectColorDTO> createProject(@Valid @RequestBody final P
113115

114116
controller.addNewProject(newProject);
115117

116-
model.getProjectRepository().save(newProject);
117-
118118
ProjectColorDTO projectDTO = projectMapper.projectToProjectDTO(newProject);
119-
120119
return ResponseEntity.status(HttpStatus.CREATED).body(projectDTO);
121120
} catch (Exception e) {
122121
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
@@ -136,15 +135,7 @@ public ResponseEntity<ProjectColorDTO> updateProjectColorDTO(@PathVariable final
136135
Project existingProject = optionalProject.get();
137136
Project newValuedProject = projectMapper.projectDTOToProject(newValuedProjectDTO);
138137

139-
existingProject.setName(newValuedProject.getName());
140-
existingProject.setDescription(newValuedProject.getDescription());
141-
existingProject.setIndex(newValuedProject.getIndex());
142-
existingProject.setWork(newValuedProject.isWork());
143-
existingProject.setColor(newValuedProject.getColor());
144-
existingProject.setDefault(newValuedProject.isDefault());
145-
existingProject.setEnabled(newValuedProject.isEnabled());
146-
147-
projectRepository.save(existingProject);
138+
controller.editProject(existingProject, newValuedProject);
148139

149140
ProjectColorDTO updatedProjectDTO = projectMapper.projectToProjectDTO(existingProject);
150141

@@ -159,36 +150,36 @@ public ResponseEntity<WorkDTO> createWorkInProject(@PathVariable final long id,
159150
@Valid @RequestBody final Work work) {
160151
Optional<Project> projectOptional = projectRepository.findById(id);
161152

162-
if (projectOptional.isPresent()) {
163-
Project project = projectOptional.get();
164-
work.setProject(project);
165-
workRepository.save(work);
166-
167-
WorkDTO workDTO = workMapper.workToWorkDTO(work);
168-
169-
return ResponseEntity.status(HttpStatus.CREATED).body(workDTO);
170-
} else {
153+
if (projectOptional.isEmpty()) {
171154
return ResponseEntity.notFound().build();
172155
}
156+
157+
Project project = projectOptional.get();
158+
work.setProject(project);
159+
workRepository.save(work);
160+
161+
WorkDTO workDTO = workMapper.workToWorkDTO(work);
162+
163+
return ResponseEntity.status(HttpStatus.CREATED).body(workDTO);
173164
}
174165

175166
@DeleteMapping("/{id}")
176167
public ResponseEntity<String> deleteProject(@PathVariable final long id) {
177168
Optional<Project> projectOptional = projectRepository.findById(id);
178169

179170
if (projectOptional.isEmpty()) {
180-
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Project with the ID '" + id + "' not found");
171+
return ResponseEntity.notFound().build();
181172
}
182173

183174
Project project = projectOptional.get();
184175

185176
if (project.isDefault()) {
186177
return new ResponseEntity<>("Project cannot be deleted as it is the default", HttpStatus.BAD_REQUEST);
187-
} else {
188-
controller.deleteProject(project);
189-
projectRepository.delete(project);
190-
return new ResponseEntity<>("Project successfully deleted", HttpStatus.OK);
191178
}
179+
180+
controller.deleteProject(project);
181+
projectRepository.delete(project);
182+
return new ResponseEntity<>("Project successfully deleted", HttpStatus.OK);
192183
}
193184

194185
@GetMapping("/current")
@@ -209,7 +200,7 @@ public ResponseEntity<ProjectColorDTO> changeProject(@Valid @RequestBody Project
209200
}
210201

211202
@ResponseStatus(value = HttpStatus.NOT_FOUND)
212-
public class ResourceNotFoundException extends RuntimeException {
203+
public static class ResourceNotFoundException extends RuntimeException {
213204
public ResourceNotFoundException(String message) {
214205
super(message);
215206
}

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import de.doubleslash.keeptime.REST_API.DTO.WorkDTO;
2020
import de.doubleslash.keeptime.REST_API.mapper.WorkMapper;
21-
import de.doubleslash.keeptime.controller.Controller;
2221
import de.doubleslash.keeptime.model.Model;
2322
import de.doubleslash.keeptime.model.Work;
2423
import de.doubleslash.keeptime.model.repos.WorkRepository;
@@ -32,7 +31,6 @@
3231
import org.springframework.web.bind.annotation.RequestMapping;
3332
import org.springframework.web.bind.annotation.RequestParam;
3433
import org.springframework.web.bind.annotation.RestController;
35-
import org.springframework.web.server.ResponseStatusException;
3634

3735
import java.util.List;
3836
import java.util.Optional;
@@ -70,33 +68,33 @@ public ResponseEntity<WorkDTO> editWork(@PathVariable("id") Long workId, @Reques
7068
Work newValuedWork = workMapper.workDTOToWork(newValuedWorkDTO);
7169
Optional<Work> optionalWork = workRepository.findById(workId);
7270

73-
if (optionalWork.isPresent()) {
74-
Work workToBeEdited = optionalWork.get();
71+
if (optionalWork.isEmpty()) {
72+
return ResponseEntity.notFound().build();
73+
}
7574

76-
workToBeEdited.setStartTime(newValuedWork.getStartTime());
77-
workToBeEdited.setEndTime(newValuedWork.getEndTime());
78-
workToBeEdited.setNotes(newValuedWork.getNotes());
79-
workToBeEdited.setProject(newValuedWork.getProject());
75+
Work workToBeEdited = optionalWork.get();
8076

81-
Work editedWork = workRepository.save(workToBeEdited);
77+
workToBeEdited.setStartTime(newValuedWork.getStartTime());
78+
workToBeEdited.setEndTime(newValuedWork.getEndTime());
79+
workToBeEdited.setNotes(newValuedWork.getNotes());
80+
workToBeEdited.setProject(newValuedWork.getProject());
8281

83-
return ResponseEntity.ok(workMapper.workToWorkDTO(editedWork));
84-
} else {
85-
return ResponseEntity.notFound().build();
86-
}
82+
Work editedWork = workRepository.save(workToBeEdited);
83+
84+
return ResponseEntity.ok(workMapper.workToWorkDTO(editedWork));
8785
}
8886

8987
@DeleteMapping("/{id}")
9088
public ResponseEntity<String> deleteWork(@PathVariable final long id) {
9189
Optional<Work> optionalWork = workRepository.findById(id);
9290

93-
if (optionalWork.isPresent()) {
94-
Work workToBeDeleted = optionalWork.get();
95-
workRepository.delete(workToBeDeleted);
96-
return new ResponseEntity<>("Work successfully deleted", HttpStatus.OK);
97-
} else {
98-
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Work with the ID " + id + " not found");
91+
if (optionalWork.isEmpty()) {
92+
return ResponseEntity.notFound().build();
9993
}
94+
95+
Work workToBeDeleted = optionalWork.get();
96+
workRepository.delete(workToBeDeleted);
97+
return new ResponseEntity<>("Work successfully deleted", HttpStatus.OK);
10098
}
10199

102100
@GetMapping("/current")

src/main/java/de/doubleslash/keeptime/controller/Controller.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.List;
2424

25+
import javafx.application.Platform;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
2728
import org.springframework.stereotype.Service;
@@ -83,10 +84,10 @@ public void changeProject(final Project newProject, final long minusSeconds) {
8384
// Start new work
8485
final Work newWork = new Work(workEnd, workEnd.plusSeconds(minusSeconds), newProject, "");
8586

86-
model.getPastWorkItems().add(newWork);
87-
88-
model.activeWorkItem.set(newWork);
89-
87+
runInFXThread(() -> {
88+
model.getPastWorkItems().add(newWork);
89+
model.activeWorkItem.set(newWork);
90+
});
9091
}
9192

9293
public Work saveCurrentWork(final LocalDateTime workEnd) {
@@ -106,13 +107,14 @@ public Work saveCurrentWork(final LocalDateTime workEnd) {
106107

107108
// Save in db
108109
return model.getWorkRepository().save(currentWork);
109-
110110
}
111111

112112
public void addNewProject(final Project project) {
113113
LOG.info("Creating new project '{}'.", project);
114-
model.getAllProjects().add(project);
115-
model.getAvailableProjects().add(project);
114+
runInFXThread(() -> {
115+
model.getAllProjects().add(project);
116+
model.getAvailableProjects().add(project);
117+
});
116118

117119
final List<Project> changedProjects = resortProjectIndexes(model.getAvailableProjects(), project,
118120
model.getAvailableProjects().size(), project.getIndex());
@@ -355,4 +357,16 @@ public long calcSeconds(final List<Work> workItems) {
355357

356358
return seconds;
357359
}
360+
361+
/**
362+
* Helper to make sure change is run in FX Thread. Needed when triggered via REST-API
363+
* @param runnable
364+
*/
365+
private void runInFXThread(Runnable runnable){
366+
if(Platform.isFxApplicationThread()){
367+
runnable.run();
368+
}else{
369+
Platform.runLater(runnable);
370+
}
371+
}
358372
}

0 commit comments

Comments
 (0)