Skip to content

Commit

Permalink
services added
Browse files Browse the repository at this point in the history
  • Loading branch information
imertetsu committed Aug 13, 2024
1 parent 8f797cc commit 27085da
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/com/spacecraft/services/EventService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.spacecraft.services;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spacecraft.dtos.EventDTO;
import com.spacecraft.dtos.LongitudeDTO;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@Service
@Getter
public class EventService {

private List<EventDTO> events;

@PostConstruct
public void loadData() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
events = objectMapper.readValue(
Files.readAllBytes(Paths.get("src/main/java/com/spacecraft/persistence/events.json")),
new TypeReference<List<EventDTO>>() {}
);
for (EventDTO event : events) {
System.out.println(event);
}
}

}
29 changes: 29 additions & 0 deletions src/main/java/com/spacecraft/services/LatitudeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.spacecraft.services;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spacecraft.dtos.LatitudeDTO;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@Service
@Getter
public class LatitudeService {

private List<LatitudeDTO> latitudes;

@PostConstruct
public void loadData() throws IOException{
ObjectMapper objectMapper = new ObjectMapper();
latitudes = objectMapper.readValue(
Files.readAllBytes(Paths.get("src/main/java/com/spacecraft/persistence/latitudes.json")),
new TypeReference<List<LatitudeDTO>>() {}
);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/spacecraft/services/LongitudeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.spacecraft.services;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spacecraft.dtos.LongitudeDTO;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@Service
@Getter
public class LongitudeService {

public List<LongitudeDTO> longitudes;

@PostConstruct
public void loadData() throws IOException{
ObjectMapper objectMapper = new ObjectMapper();
longitudes = objectMapper.readValue(
Files.readAllBytes(Paths.get("src/main/java/com/spacecraft/persistence/longitudes.json")),
new TypeReference<List<LongitudeDTO>>() {}
);
for (LongitudeDTO longitudeDTO: longitudes){
System.out.println(longitudeDTO);
}
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/spacecraft/services/PositionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.spacecraft.services;

import com.spacecraft.dtos.LatitudeDTO;
import com.spacecraft.dtos.LongitudeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Comparator;

@Service
public class PositionService {
private DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
private final LatitudeService latitudeService;
private final LongitudeService longitudeService;

@Autowired
public PositionService(LatitudeService latitudeService,
LongitudeService longitudeService){
this.latitudeService = latitudeService;
this.longitudeService = longitudeService;
}

public LatitudeDTO getClosestEventLatitude(String eventTime) {
LocalDateTime eventDateTime = LocalDateTime.parse(eventTime, formatter);

return latitudeService.getLatitudes().stream()
.min(Comparator.comparing(lat -> Math.abs(ChronoUnit.SECONDS.between(
LocalDateTime.parse(lat.getTimestamp(), formatter),
eventDateTime))))
.orElse(null);
}

public LongitudeDTO getClosestEventLongitude(String eventTime){
LocalDateTime eventDateTime = LocalDateTime.parse(eventTime, formatter);

return longitudeService.getLongitudes().stream()
.min(Comparator.comparing(lon -> Math.abs(ChronoUnit.SECONDS.between(
LocalDateTime.parse(lon.getTimestamp(), formatter),
eventDateTime))))
.orElse(null);
}
}

0 comments on commit 27085da

Please sign in to comment.