Merge branch '102-track-endpoint-crashes-if-something-is-null' into 'master'

Resolve "Track endpoint crashes if something is null"

Closes #102

See merge request marcel.schwarz/2020ss-qbc-geofence-timetracking!78
This commit is contained in:
Marcel Schwarz 2020-05-28 17:19:10 +00:00
commit 77c6b85eec

View File

@ -2,10 +2,10 @@ package de.hft.geotime.controllers;
import de.hft.geotime.entities.RecordType; import de.hft.geotime.entities.RecordType;
import de.hft.geotime.entities.TimeRecord; 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.RecordRepository;
import de.hft.geotime.repositories.TimetrackAccountRepository; 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.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication; 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 org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Optional; import java.time.format.DateTimeFormatter;
import java.util.Objects;
@RestController @RestController
public class RecordController { public class RecordController {
private final RecordRepository recordRepository; private final RecordRepository recordRepository;
private final TimetrackAccountRepository accountRepository; 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.recordRepository = recordRepository;
this.accountRepository = accountRepository; this.accountRepository = accountRepository;
this.projectionFactory = projectionFactory;
} }
@GetMapping("/track") @GetMapping("/track")
public ResponseEntity<TimeRecord> track(@RequestParam String account, Authentication authentication) { public ResponseEntity<RecordOverviewProjection> track(@RequestParam String account, Authentication authentication) {
if (account == null || account.isEmpty()) { if (account == null || account.isEmpty()) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); 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) { if (selectedAccount == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} }
Page<TimeRecord> entires = recordRepository.findAllByEnddateIsNull(null); var entires = recordRepository.findAllByEnddateIsNull(null);
Optional<TimeRecord> collect = entires.stream() var collect = entires.get()
.filter(timeRecord -> timeRecord.getAccount().equals(selectedAccount)) .filter(Objects::nonNull)
.filter(timeRecord -> selectedAccount.equals(timeRecord.getAccount()))
.findFirst(); .findFirst();
var now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
if (collect.isPresent()) { if (collect.isPresent()) {
collect.get().setEnddate(LocalDateTime.now()); collect.get().setEnddate(LocalDateTime.parse(now));
recordRepository.save(collect.get()); 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 { } else {
TimeRecord newRecord = new TimeRecord(); var newRecord = new TimeRecord();
newRecord.setType(RecordType.PAID); newRecord.setType(RecordType.PAID);
newRecord.setStartdate(LocalDateTime.now()); newRecord.setStartdate(LocalDateTime.parse(now));
newRecord.setAccount(accountRepository.findByUser_UsernameAndName(authentication.getName(), account)); newRecord.setAccount(accountRepository.findByUser_UsernameAndName(authentication.getName(), account));
recordRepository.save(newRecord); recordRepository.save(newRecord);
return new ResponseEntity<>(newRecord, HttpStatus.CREATED); var projection = projectionFactory.createProjection(RecordOverviewProjection.class, newRecord);
return new ResponseEntity<>(projection, HttpStatus.CREATED);
} }
} }
} }