Skip to content

Commit

Permalink
ADD: Sms Api functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-777 committed Oct 19, 2019
1 parent 601eb52 commit e17e945
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public ResponseEntity<FireReport> getFireReportById(@PathVariable Long id) {
new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@PostMapping("/registered/{userId}")
public ResponseEntity<FireReportDTO> addFireReport(@RequestBody FireReportDTO fireReportDTO, @PathVariable Long userId) {
@PostMapping("/registered")
public ResponseEntity<FireReportDTO> addFireReport(@RequestBody FireReportDTO fireReportDTO) {
FireReport fireReport = new FireReport(fireReportDTO.getReporterId(),
fireReportDTO.getX(), fireReportDTO.getY(),
fireReportDTO.getStartDate(), fireReportDTO.getFireReportApproveCounter(),
Expand All @@ -56,7 +56,6 @@ public ResponseEntity<FireReportDTO> addFireReport(@RequestBody FireReportDTO fi
fireReportDTO.getIsHazardousMaterial(), fireReportDTO.getPhoto(),
fireReportDTO.getAddress());

fireReport.setReporterId(userId);
fireReportService.saveFireReport(fireReport);

log.info(FireReportMessages.SAVED_FIRE_REPORT + fireReport.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

public interface NotificationService {

public void sendNotification(FireReport fireReport);
void sendNotification(FireReport fireReport);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.nasa.entities.FireReport;
import com.example.nasa.repositories.FireReportRepository;
import com.example.nasa.services.FireReportService;
import com.example.nasa.services.NotificationService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -15,6 +16,7 @@
public class FireReportServiceImpl implements FireReportService {

private final FireReportRepository fireReportRepository;
private final NotificationService notificationService;

@Override
public List<FireReport> getAllFireReports() {
Expand Down Expand Up @@ -64,7 +66,7 @@ public FireReportDTO editFireReportById(Long id, FireReportDTO fireReportDTO) {

@Override
public FireReport saveFireReport(FireReport fireReport) {

notificationService.sendNotification(fireReport);
return fireReportRepository.save(fireReport);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import com.example.nasa.utils.DistanceCalculator;
import com.example.nasa.utils.SmsApi;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.http.HttpHeaders;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -23,30 +23,32 @@ public class NotificationServiceImpl implements NotificationService {

private final UserService userService;
private final SmsApiConfig smsApiConfig;
private final SmsApi smsApi;

public void sendNotification(FireReport fireReport){
public void sendNotification(FireReport fireReport) {
sendSmsToGateway(fireReport);
}

private void sendSmsToGateway(FireReport fireReport){
SmsApi smsApi = new SmsApi();
private void sendSmsToGateway(FireReport fireReport) {

List<UserDTO> userDTOList = userService.getAllUsers().stream()
.filter(userDTO -> {
Long distanceFromFireInMeters = DistanceCalculator.calculate(fireReport.getY(),fireReport.getX(),userDTO.getX(),userDTO.getY());
return userDTO.getNotificationRangeInMeters()>distanceFromFireInMeters;
Long distanceFromFireInMeters = DistanceCalculator.calculate(fireReport.getX(), fireReport.getY(), userDTO.getX(), userDTO.getY());
return userDTO.getNotificationRangeInMeters() > distanceFromFireInMeters;
}).collect(Collectors.toList());

smsApi.urlForFireLocationSms(userDTOList,fireReport.getX(),fireReport.getY());
String urlPost = smsApi.urlForFireLocationSms(userDTOList, fireReport.getX(), fireReport.getY());

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();

headers.add("Authorization", "Bearer " + smsApiConfig.getToken());

RestTemplate restTemplate = new RestTemplate();

MultiValueMap map = new LinkedMultiValueMap();
HttpHeaders headers = new HttpHeaders(map);
HttpEntity<String> request = new HttpEntity<String>("", headers);

var response = restTemplate.postForObject(urlPost, request, Object.class);

}

private Boolean is

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public List<UserDTO> getAllUsers() {
List<UserDTO> userDTOs = new ArrayList<>();
for (User user : users) {
userDTOs.add(new UserDTO(user.getId(), user.getName(), user.getSurname(), user.getCellPhoneNumber(),
user.getEmail(), user.getPhoto(), user.getNotificationRangeInMeters()));
user.getEmail(), user.getPhoto(), user.getNotificationRangeInMeters(), user.getX(),user.getY()));
}

return userDTOs;
Expand All @@ -35,7 +35,7 @@ public UserDTO getUserById(Long id) {

return user.map(value -> new UserDTO(value.getId(), value.getName(), value.getSurname(),
value.getCellPhoneNumber(), value.getEmail(),
value.getPhoto(), value.getNotificationRangeInMeters())).orElse(null);
value.getPhoto(), value.getNotificationRangeInMeters(), value.getX(),value.getY())).orElse(null);

}

Expand All @@ -47,7 +47,7 @@ public UserDTO deleteUserById(Long id) {
userRepository.deleteById(id);
return new UserDTO(user.get().getId(), user.get().getName(), user.get().getSurname(),
user.get().getCellPhoneNumber(), user.get().getEmail(),
user.get().getPhoto(), user.get().getNotificationRangeInMeters());
user.get().getPhoto(), user.get().getNotificationRangeInMeters(), user.get().getX(),user.get().getY());
}

return null;
Expand All @@ -61,7 +61,7 @@ public UserDTO editUserById(Long id, User userCommand) {
userRepository.deleteById(id);
return new UserDTO(userRepository.save(userCommand).getId(), userCommand.getName(), userCommand.getSurname(),
userCommand.getCellPhoneNumber(), userCommand.getEmail(),
userCommand.getPhoto(), userCommand.getNotificationRangeInMeters());
userCommand.getPhoto(), userCommand.getNotificationRangeInMeters(),userCommand.getX(),userCommand.getY());
}
return null;
}
Expand All @@ -71,6 +71,6 @@ public UserDTO saveUser(User user) {
userRepository.save(user);
return new UserDTO(user.getId(), user.getName(), user.getSurname(),
user.getCellPhoneNumber(), user.getEmail(), user.getPhoto(),
user.getNotificationRangeInMeters());
user.getNotificationRangeInMeters(),user.getX(),user.getY());
}
}
12 changes: 10 additions & 2 deletions src/main/java/com/example/nasa/utils/SmsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import com.example.nasa.config.properties.SmsApiConfig;
import com.example.nasa.dto.UserDTO;
import com.example.nasa.entities.User;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@Service
@Slf4j
public class SmsApi {

private SmsApiConfig smsApiConfig;
Expand All @@ -28,11 +34,13 @@ public String urlForFireLocationSms(List<UserDTO> userListInRange, Double fireLa
}).collect(Collectors.joining(",")));
responseBuilder.append("&");
responseBuilder.append("message=").append("Fire is on location: ");
responseBuilder.append("GOOGLE fire LOCATION: lat:").append(fireLat).append(" long: ").append(fireLong); //FIXME add google fire link
responseBuilder.append("http://www.google.com/maps/place/").append(fireLat).append(",").append(fireLong);
responseBuilder.append("&");
responseBuilder.append("format=json");
String response = responseBuilder.toString();

return responseBuilder.toString();
log.info("SMS response: " + response);
return response;
}

}
4 changes: 2 additions & 2 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
INSERT INTO user(id,name,surname,password,cell_phone_number,email) VALUES ('1','Tom','Kowalski','tomKowalski1','+48605673426','[email protected]');
INSERT INTO user(id,name,surname,password,cell_phone_number,email) VALUES ('2','Anna','Nowak','tomKowalski1','+48664679982','[email protected]');
INSERT INTO user(id,name,surname,password,cell_phone_number,email,notification_range_in_meters,x,y) VALUES ('1','Tom','Kowalski','tomKowalski1','+48605673426','[email protected]','30000','53.421132','14.537240');
INSERT INTO user(id,name,surname,password,cell_phone_number,email,notification_range_in_meters,x,y) VALUES ('2','Anna','Nowak','tomKowalski1','+48664679982','[email protected]','10000','53.421132','14.537240');

0 comments on commit e17e945

Please sign in to comment.