package ru.mcs.tracker.controller;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.mcs.tracker.dto.LocationRequest;
import ru.mcs.tracker.entity.LocationEntity;
import ru.mcs.tracker.repository.LocationRepository;
import java.time.LocalDateTime;
import java.util.List;
@RestController
@RequestMapping("/api/locations")
public class LocationController {
private final LocationRepository repository;
public LocationController(LocationRepository repository) {
this.repository = repository;
}
@PostMapping
public ResponseEntity<Void> addLocation(@RequestBody LocationRequest request) {
LocationEntity entity = new LocationEntity(
request.getDeviceGuid(),
request.getLatitude(),
request.getLongitude(),
request.getTimestamp() != null ? request.getTimestamp() : LocalDateTime.now()
);
repository.save(entity);
return ResponseEntity.ok().build();
}
@GetMapping("/{deviceGuid}")
public List<LocationEntity> getLocations(
@PathVariable String deviceGuid,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end
) {
return repository.findByDeviceGuidAndTimestampBetween(deviceGuid, start, end);
}
}