Initial commit
This commit is contained in:
commit
0e87787beb
23 changed files with 686 additions and 0 deletions
|
|
@ -0,0 +1,13 @@
|
|||
package io.github.js0ny.ilp_coursework;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class IlpCourseworkApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(IlpCourseworkApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
package io.github.js0ny.ilp_coursework.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
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.RestController;
|
||||
|
||||
import io.github.js0ny.ilp_coursework.dto.DistanceRequestDto;
|
||||
import io.github.js0ny.ilp_coursework.dto.LngLatDto;
|
||||
import io.github.js0ny.ilp_coursework.dto.MovementRequestDto;
|
||||
import io.github.js0ny.ilp_coursework.dto.RegionCheckRequestDto;
|
||||
import io.github.js0ny.ilp_coursework.service.GpsCalculationService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1")
|
||||
public class ApiController {
|
||||
|
||||
private final String myUid = "s2522255";
|
||||
private final GpsCalculationService gpsService;
|
||||
|
||||
public ApiController(GpsCalculationService gpsService) {
|
||||
this.gpsService = gpsService;
|
||||
}
|
||||
|
||||
@GetMapping("/uid")
|
||||
public String getUid() {
|
||||
return myUid;
|
||||
}
|
||||
|
||||
@PostMapping("/distanceTo")
|
||||
public double getDistance(@RequestBody DistanceRequestDto request) {
|
||||
|
||||
LngLatDto position1 = request.position1();
|
||||
LngLatDto position2 = request.position2();
|
||||
return gpsService.calculateDistance(position1, position2);
|
||||
}
|
||||
|
||||
@PostMapping("/isCloseTo")
|
||||
public boolean getIsCloseTo(@RequestBody DistanceRequestDto request) {
|
||||
LngLatDto position1 = request.position1();
|
||||
LngLatDto position2 = request.position2();
|
||||
return gpsService.isCloseTo(position1, position2);
|
||||
}
|
||||
|
||||
@PostMapping("/nextPosition")
|
||||
public LngLatDto getNextPosition(@RequestBody MovementRequestDto request) {
|
||||
LngLatDto start = request.start();
|
||||
double angle = request.angle();
|
||||
return gpsService.nextPosition(start, angle);
|
||||
}
|
||||
|
||||
@PostMapping("/isInRegion")
|
||||
public boolean getIsInRegion(@RequestBody RegionCheckRequestDto request) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
package io.github.js0ny.ilp_coursework.dto;
|
||||
|
||||
public record DistanceRequestDto(LngLatDto position1, LngLatDto position2) {
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package io.github.js0ny.ilp_coursework.dto;
|
||||
|
||||
public record LngLatDto(double lng, double lat) {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package io.github.js0ny.ilp_coursework.dto;
|
||||
|
||||
/**
|
||||
* Represents the data transfer object for a movement action request.
|
||||
* Used for endpoint: `/api/v1/nextPosition`
|
||||
*/
|
||||
public record MovementRequestDto(LngLatDto start, double angle) {
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package io.github.js0ny.ilp_coursework.dto;
|
||||
|
||||
public record RegionCheckRequestDto(LngLatDto position) {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package io.github.js0ny.ilp_coursework.dto;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record RegionDto(String name, List<LngLatDto> vertices) {
|
||||
|
||||
public boolean isClose() {
|
||||
// Magic number 4: For a polygon, 3 edges is required.
|
||||
// In this dto, edges + 1 vertices is required.
|
||||
if (vertices == null || vertices.size() < 4) {
|
||||
return false;
|
||||
}
|
||||
LngLatDto first = vertices.get(0);
|
||||
LngLatDto last = vertices.get(vertices.size() - 1);
|
||||
return Objects.equals(last, first);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package io.github.js0ny.ilp_coursework.exception;
|
||||
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package io.github.js0ny.ilp_coursework.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import io.github.js0ny.ilp_coursework.dto.LngLatDto;
|
||||
|
||||
@Service
|
||||
public class GpsCalculationService {
|
||||
private static final double STEP = 0.00015;
|
||||
private static final double CLOSE_THRESHOLD = STEP;
|
||||
|
||||
public double calculateDistance(LngLatDto position1, LngLatDto position2) {
|
||||
double lngDistance = position2.lng() - position1.lng();
|
||||
double latDistance = position2.lat() - position1.lat();
|
||||
return Math.sqrt(lngDistance * lngDistance + latDistance * latDistance);
|
||||
}
|
||||
|
||||
public boolean isCloseTo(LngLatDto position1, LngLatDto position2) {
|
||||
double distance = calculateDistance(position1, position2);
|
||||
return distance < CLOSE_THRESHOLD;
|
||||
}
|
||||
|
||||
public LngLatDto nextPosition(LngLatDto start, double angle) {
|
||||
double rad = Math.toRadians(angle);
|
||||
double newLng = Math.cos(rad) * STEP + start.lng();
|
||||
double newLat = Math.sin(rad) * STEP + start.lat();
|
||||
return new LngLatDto(newLng, newLat);
|
||||
}
|
||||
|
||||
}
|
||||
1
src/main/resources/application.properties
Normal file
1
src/main/resources/application.properties
Normal file
|
|
@ -0,0 +1 @@
|
|||
spring.application.name=ilp-coursework
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package io.github.js0ny.ilp_coursework;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class IlpCourseworkApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue