From 75d2cb66706aec0c4b2ac0f90e585d07010bad47 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Thu, 28 May 2020 19:03:10 +0200 Subject: [PATCH] Fix crash when all record fields are null --- .../geotime/controllers/RecordController.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/backend/src/main/java/de/hft/geotime/controllers/RecordController.java b/backend/src/main/java/de/hft/geotime/controllers/RecordController.java index 88bfb28..eb10975 100644 --- a/backend/src/main/java/de/hft/geotime/controllers/RecordController.java +++ b/backend/src/main/java/de/hft/geotime/controllers/RecordController.java @@ -2,10 +2,10 @@ package de.hft.geotime.controllers; import de.hft.geotime.entities.RecordType; import de.hft.geotime.entities.TimeRecord; -import de.hft.geotime.entities.TimetrackAccount; +import de.hft.geotime.entities.projections.RecordOverviewProjection; import de.hft.geotime.repositories.RecordRepository; import de.hft.geotime.repositories.TimetrackAccountRepository; -import org.springframework.data.domain.Page; +import org.springframework.data.projection.ProjectionFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; @@ -14,47 +14,55 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.time.LocalDateTime; -import java.util.Optional; +import java.time.format.DateTimeFormatter; +import java.util.Objects; @RestController public class RecordController { private final RecordRepository recordRepository; private final TimetrackAccountRepository accountRepository; + private final ProjectionFactory projectionFactory; - public RecordController(RecordRepository recordRepository, TimetrackAccountRepository accountRepository) { + public RecordController(RecordRepository recordRepository, TimetrackAccountRepository accountRepository, ProjectionFactory projectionFactory) { this.recordRepository = recordRepository; this.accountRepository = accountRepository; + this.projectionFactory = projectionFactory; } @GetMapping("/track") - public ResponseEntity track(@RequestParam String account, Authentication authentication) { + public ResponseEntity track(@RequestParam String account, Authentication authentication) { if (account == null || account.isEmpty()) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } - TimetrackAccount selectedAccount = accountRepository.findByUser_UsernameAndName(authentication.getName(), account); + var selectedAccount = accountRepository.findByUser_UsernameAndName(authentication.getName(), account); if (selectedAccount == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - Page entires = recordRepository.findAllByEnddateIsNull(null); - Optional collect = entires.stream() - .filter(timeRecord -> timeRecord.getAccount().equals(selectedAccount)) + var entires = recordRepository.findAllByEnddateIsNull(null); + var collect = entires.get() + .filter(Objects::nonNull) + .filter(timeRecord -> selectedAccount.equals(timeRecord.getAccount())) .findFirst(); + var now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")); + if (collect.isPresent()) { - collect.get().setEnddate(LocalDateTime.now()); + collect.get().setEnddate(LocalDateTime.parse(now)); recordRepository.save(collect.get()); - return new ResponseEntity<>(collect.get(), HttpStatus.OK); + var projection = projectionFactory.createProjection(RecordOverviewProjection.class, collect.get()); + return new ResponseEntity<>(projection, HttpStatus.OK); } else { - TimeRecord newRecord = new TimeRecord(); + var newRecord = new TimeRecord(); newRecord.setType(RecordType.PAID); - newRecord.setStartdate(LocalDateTime.now()); + newRecord.setStartdate(LocalDateTime.parse(now)); newRecord.setAccount(accountRepository.findByUser_UsernameAndName(authentication.getName(), account)); recordRepository.save(newRecord); - return new ResponseEntity<>(newRecord, HttpStatus.CREATED); + var projection = projectionFactory.createProjection(RecordOverviewProjection.class, newRecord); + return new ResponseEntity<>(projection, HttpStatus.CREATED); } } }