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.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<TimeRecord> track(@RequestParam String account, Authentication authentication) {
|
||||
public ResponseEntity<RecordOverviewProjection> 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<TimeRecord> entires = recordRepository.findAllByEnddateIsNull(null);
|
||||
Optional<TimeRecord> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user