Implement /track endpoint

This commit is contained in:
Marcel Schwarz 2020-05-20 23:26:20 +02:00
parent 9f8fd0af1e
commit 5eb6efcffe
5 changed files with 55 additions and 14 deletions

View File

@ -1,26 +1,60 @@
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.repositories.RecordRepository;
import de.hft.geotime.repositories.TimetrackUserRepository;
import org.springframework.data.projection.ProjectionFactory;
import de.hft.geotime.repositories.TimetrackAccountRepository;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.Optional;
@RestController
public class RecordController {
private final RecordRepository recordRepository;
private final TimetrackUserRepository userRepository;
private final ProjectionFactory projectionFactory;
private final TimetrackAccountRepository accountRepository;
public RecordController(RecordRepository recordRepository, TimetrackUserRepository userRepository, ProjectionFactory projectionFactory) {
public RecordController(RecordRepository recordRepository, TimetrackAccountRepository accountRepository) {
this.recordRepository = recordRepository;
this.userRepository = userRepository;
this.projectionFactory = projectionFactory;
this.accountRepository = accountRepository;
}
@GetMapping("/track")
public void track() {
//"/track?accountid=bla" start/stop recording for that account
public ResponseEntity<TimeRecord> 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);
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))
.findFirst();
if (collect.isPresent()) {
collect.get().setEnddate(LocalDateTime.now());
recordRepository.save(collect.get());
return new ResponseEntity<>(collect.get(), HttpStatus.OK);
} else {
TimeRecord newRecord = new TimeRecord();
newRecord.setType(RecordType.PAID);
newRecord.setStartdate(LocalDateTime.now());
newRecord.setAccount(accountRepository.findByUser_UsernameAndName(authentication.getName(), account));
recordRepository.save(newRecord);
return new ResponseEntity<>(newRecord, HttpStatus.CREATED);
}
}
}

View File

@ -30,7 +30,11 @@ public class TimeRecord {
private RecordType type;
public long getDuration() {
return startdate.until(enddate, ChronoUnit.MINUTES);
if (enddate == null) {
return 0;
} else {
return startdate.until(enddate, ChronoUnit.MINUTES);
}
}
}

View File

@ -6,7 +6,6 @@ import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.List;
@Data
@NoArgsConstructor
@ -25,7 +24,4 @@ public class TimetrackAccount {
@ManyToOne
private TimetrackUser user;
@OneToMany(mappedBy = "account")
private List<TimeRecord> records;
}

View File

@ -48,4 +48,7 @@ public interface RecordRepository extends PagingAndSortingRepository<TimeRecord,
@Query("SELECT record from TimeRecord record where record.account.user.username = :#{principal} AND record.startdate > (current_date-1)")
Page<TimeRecord> today(Pageable pageable);
@RestResource(rel = "openEntries", path = "openEntries")
Page<TimeRecord> findAllByEnddateIsNull(Pageable pageable);
}

View File

@ -3,6 +3,7 @@ package de.hft.geotime.repositories;
import de.hft.geotime.entities.TimetrackAccount;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
@RepositoryRestResource(
path = "accounts",
@ -11,4 +12,7 @@ import org.springframework.data.rest.core.annotation.RepositoryRestResource;
)
public interface TimetrackAccountRepository extends PagingAndSortingRepository<TimetrackAccount, Long> {
@RestResource(rel = "findByUsernameAndName", path = "findByUsernameAndName")
TimetrackAccount findByUser_UsernameAndName(String username, String account);
}