@ -24,6 +24,8 @@ import java.net.HttpURLConnection;
import java.net.URL ;
import java.nio.channels.Channels ;
import java.nio.channels.ReadableByteChannel ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Date ;
import java.util.HashMap ;
import java.util.List ;
@ -42,23 +44,30 @@ public class Endpoints {
FamilyRelationshipRepository relationships ;
LocationRepository locations ;
KnownLocationRepository knownlocations ;
NotificationRepository notifications ;
RestTemplate connection = new RestTemplate ( ) ;
public Endpoints ( FamilyRepository families ,
FamilyMemberRepository members ,
FamilyRelationshipRepository relationships ,
LocationRepository locations ,
KnownLocationRepository knownlocations ) {
KnownLocationRepository knownlocations ,
NotificationRepository notifications ) {
this . families = families ;
this . members = members ;
this . relationships = relationships ;
this . locations = locations ;
this . knownlocations = knownlocations ;
this . notifications = notifications ;
}
@GetMapping ( "/family" )
public Iterable < Family > _1 ( ) {
return families . findAll ( ) ;
public List < FamilyContainer > _1 ( ) {
List < FamilyContainer > list = new ArrayList < > ( ) ;
for ( Family f : families . findAll ( ) ) {
list . add ( new FamilyContainer ( f . getName ( ) , families , relationships , members ) ) ;
}
return list ;
}
@GetMapping ( "/family/{id}" )
@ -126,6 +135,24 @@ public class Endpoints {
return null ;
}
@DeleteMapping ( "/member/{id}" )
public FamilyMember _7 (
@PathVariable Long id ) {
if ( members . existsById ( id ) ) {
FamilyMember m = members . findById ( id ) . get ( ) ;
members . delete ( m ) ;
List < FamilyRelationship > s = relationships . findByMemberId ( m . getId ( ) ) ;
for ( FamilyRelationship ss : s ) {
relationships . delete ( ss ) ;
}
return m ;
} else {
return null ;
}
}
@PostMapping ( "/location" )
/ * *
* @RequestBody requires :
@ -136,6 +163,34 @@ public class Endpoints {
* /
public Location _7 ( @RequestBody Map < String , String > body ) {
if ( body . containsKey ( "member" ) & & body . containsKey ( "x" ) & & body . containsKey ( "y" ) & & members . existsById ( Long . parseLong ( body . get ( "member" ) ) ) ) {
double x = Double . parseDouble ( String . format ( "%.3f" , Double . parseDouble ( body . get ( "x" ) ) ) ) , y = Double . parseDouble ( String . format ( "%.3f" , Double . parseDouble ( body . get ( "y" ) ) ) ) ;
//If someone is within 0.001 distance of a location, they are "at" that location.
//That's why all known locations will be restricted to 3 decimal places for lookups.
List < KnownLocation > loc = knownlocations . findByLongitudeAndLatitude ( x , y ) ;
/ * - Notify parents of children location changes .
* - Notify anyone of dangerous locations . * /
FamilyRelationship fr = relationships . findByMemberId ( Long . parseLong ( body . get ( "member" ) ) ) . get ( 0 ) ;
FamilyMember m = members . findById ( fr . getMemberId ( ) ) . get ( ) ;
boolean isParent = relationships . findByMemberIdAndRelationshipIn ( Long . parseLong ( body . get ( "member" ) ) , Arrays . asList ( "Father" , "Mother" , "Parent" ) ) . size ( ) > 0 ;
if ( loc . size ( ) > 0 ) {
KnownLocation ll = loc . get ( 0 ) ;
if ( ! ll . isSafe ( ) ) {
notifications . save ( new Notification ( "You are arriving at " + ll . getName ( ) + ", which is considered an unsafe location! Be careful!" , m . getId ( ) , 1 , new Date ( ) ) ) ;
}
if ( ! isParent ) {
//Send a notification to parents.
List < FamilyRelationship > parents = relationships . findByFamilyIdAndRelationshipIn ( fr . getFamilyId ( ) , Arrays . asList ( "Father" , "Mother" , "Parent" ) ) ;
for ( FamilyRelationship f : parents ) {
if ( ! ll . isSafe ( ) ) {
notifications . save ( new Notification ( m . getFirstName ( ) + " " + m . getLastName ( ) + " has arrived at " + ll . getName ( ) + ", this is an unsafe location!" , f . getMemberId ( ) , 1 , new Date ( ) ) ) ;
} else {
notifications . save ( new Notification ( m . getFirstName ( ) + " " + m . getLastName ( ) + " has arrived at " + ll . getName ( ) + "." , f . getMemberId ( ) , 0 , new Date ( ) ) ) ;
}
}
}
}
//notifications.save(new Notification());
return locations . save ( new Location ( Double . parseDouble ( body . get ( "x" ) ) , Double . parseDouble ( body . get ( "y" ) ) , Long . parseLong ( body . get ( "member" ) ) , new Date ( ) ) ) ;
} else {
return null ;
@ -146,14 +201,15 @@ public class Endpoints {
/ * *
* @RequestBody requires :
* name - The name of the location .
* x - The X coordinate of the location ( lat itude ) .
* y - The Y coordination of the location ( long itude ) .
* x - The X coordinate of the location ( long itude ) .
* y - The Y coordination of the location ( lat itude ) .
* safe - True if safe , false if unsafe location .
* @return
* /
public KnownLocation _8 ( @RequestBody Map < String , String > body ) {
if ( body . containsKey ( "name" ) & & body . containsKey ( "x" ) & & body . containsKey ( "y" ) & & body . containsKey ( "safe" ) ) {
return knownlocations . save ( new KnownLocation ( Double . parseDouble ( body . get ( "x" ) ) , Double . parseDouble ( body . get ( "y" ) ) , body . get ( "name" ) , Boolean . parseBoolean ( body . get ( "safe" ) ) ) ) ;
double x = Double . parseDouble ( String . format ( "%.3f" , Double . parseDouble ( body . get ( "x" ) ) ) ) , y = Double . parseDouble ( String . format ( "%.3f" , Double . parseDouble ( body . get ( "y" ) ) ) ) ;
return knownlocations . save ( new KnownLocation ( x , y , body . get ( "name" ) , Boolean . parseBoolean ( body . get ( "safe" ) ) ) ) ;
} else {
return null ;
}
@ -169,4 +225,40 @@ public class Endpoints {
return null ;
}
}
@GetMapping ( "/notification/{id}" )
public List < Notification > _10 ( @PathVariable Long id ) {
return notifications . findByMemberId ( id ) ;
}
@PostMapping ( "/notification" )
/ * *
* @RequestBody requires :
* fromMember - The ID of the member sending the notification .
* toMember - The ID of the member receiving the notification .
* message - The message .
* @return
* /
public Notification _11 ( @RequestBody Map < String , String > body ) {
if ( body . containsKey ( "fromMember" ) & & body . containsKey ( "toMember" ) & & body . containsKey ( "message" ) ) {
FamilyMember m = members . findById ( Long . parseLong ( body . get ( "fromMember" ) ) ) . get ( ) ;
return notifications . save ( new Notification ( "Received Message from " + m . getFirstName ( ) + " " + m . getLastName ( ) + ": " + body . get ( "message" ) , Long . parseLong ( body . get ( "toMember" ) ) , 2 , new Date ( ) ) ) ;
} else {
return null ;
}
}
@DeleteMapping ( "/notification/{notificationid}" )
public Notification _11 ( @PathVariable Long notificationid ) {
if ( notifications . existsById ( notificationid ) ) {
Notification n = notifications . findById ( notificationid ) . get ( ) ;
notifications . delete ( n ) ;
return n ;
} else {
return null ;
}
}
}