Implement /track endpoint
This commit is contained in:
parent
9f8fd0af1e
commit
5eb6efcffe
@ -1,26 +1,60 @@
|
|||||||
package de.hft.geotime.controllers;
|
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.RecordRepository;
|
||||||
import de.hft.geotime.repositories.TimetrackUserRepository;
|
import de.hft.geotime.repositories.TimetrackAccountRepository;
|
||||||
import org.springframework.data.projection.ProjectionFactory;
|
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.GetMapping;
|
||||||
|
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.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class RecordController {
|
public class RecordController {
|
||||||
|
|
||||||
private final RecordRepository recordRepository;
|
private final RecordRepository recordRepository;
|
||||||
private final TimetrackUserRepository userRepository;
|
private final TimetrackAccountRepository accountRepository;
|
||||||
private final ProjectionFactory projectionFactory;
|
|
||||||
|
|
||||||
public RecordController(RecordRepository recordRepository, TimetrackUserRepository userRepository, ProjectionFactory projectionFactory) {
|
public RecordController(RecordRepository recordRepository, TimetrackAccountRepository accountRepository) {
|
||||||
this.recordRepository = recordRepository;
|
this.recordRepository = recordRepository;
|
||||||
this.userRepository = userRepository;
|
this.accountRepository = accountRepository;
|
||||||
this.projectionFactory = projectionFactory;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/track")
|
@GetMapping("/track")
|
||||||
public void track() {
|
public ResponseEntity<TimeRecord> track(@RequestParam String account, Authentication authentication) {
|
||||||
//"/track?accountid=bla" → start/stop recording for that account
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,11 @@ public class TimeRecord {
|
|||||||
private RecordType type;
|
private RecordType type;
|
||||||
|
|
||||||
public long getDuration() {
|
public long getDuration() {
|
||||||
|
if (enddate == null) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
return startdate.until(enddate, ChronoUnit.MINUTES);
|
return startdate.until(enddate, ChronoUnit.MINUTES);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@ -25,7 +24,4 @@ public class TimetrackAccount {
|
|||||||
@ManyToOne
|
@ManyToOne
|
||||||
private TimetrackUser user;
|
private TimetrackUser user;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "account")
|
|
||||||
private List<TimeRecord> records;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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)")
|
@Query("SELECT record from TimeRecord record where record.account.user.username = :#{principal} AND record.startdate > (current_date-1)")
|
||||||
Page<TimeRecord> today(Pageable pageable);
|
Page<TimeRecord> today(Pageable pageable);
|
||||||
|
|
||||||
|
@RestResource(rel = "openEntries", path = "openEntries")
|
||||||
|
Page<TimeRecord> findAllByEnddateIsNull(Pageable pageable);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package de.hft.geotime.repositories;
|
|||||||
import de.hft.geotime.entities.TimetrackAccount;
|
import de.hft.geotime.entities.TimetrackAccount;
|
||||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
||||||
|
import org.springframework.data.rest.core.annotation.RestResource;
|
||||||
|
|
||||||
@RepositoryRestResource(
|
@RepositoryRestResource(
|
||||||
path = "accounts",
|
path = "accounts",
|
||||||
@ -11,4 +12,7 @@ import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
|||||||
)
|
)
|
||||||
public interface TimetrackAccountRepository extends PagingAndSortingRepository<TimetrackAccount, Long> {
|
public interface TimetrackAccountRepository extends PagingAndSortingRepository<TimetrackAccount, Long> {
|
||||||
|
|
||||||
|
@RestResource(rel = "findByUsernameAndName", path = "findByUsernameAndName")
|
||||||
|
TimetrackAccount findByUser_UsernameAndName(String username, String account);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user