Fix crash when all record fields are null
This commit is contained in:
parent
19d01b1502
commit
75d2cb6670
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user