Implement all endpoints.

master
Joshua Sigona 5 years ago
parent 5a6b750a1b
commit a53f4ea084
  1. 104
      src/main/java/sig/family/Endpoints.java
  2. 2
      src/main/java/sig/family/Family.java
  3. 44
      src/main/java/sig/family/FamilyContainer.java
  4. 2
      src/main/java/sig/family/FamilyMember.java
  5. 4
      src/main/java/sig/family/FamilyRelationshipRepository.java
  6. 3
      src/main/java/sig/family/FamilyRepository.java
  7. 17
      src/main/java/sig/family/KnownLocation.java
  8. 4
      src/main/java/sig/family/KnownLocationRepository.java
  9. 90
      src/main/java/sig/family/Notification.java
  10. 9
      src/main/java/sig/family/NotificationRepository.java

@ -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 (latitude).
* y - The Y coordination of the location (longitude).
* x - The X coordinate of the location (longitude).
* y - The Y coordination of the location (latitude).
* 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;
}
}
}

@ -25,6 +25,8 @@ public class Family {
String name;
Family(){}
Family(String name) {
this.name=name;
}

@ -0,0 +1,44 @@
package sig.family;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
@RequestMapping
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FamilyContainer extends Family{
List<FamilyMember> members = new ArrayList<>();
FamilyContainer(String name
,FamilyRepository families
,FamilyRelationshipRepository relationships
,FamilyMemberRepository members) {
super(name);
List<FamilyRelationship> relations = relationships.findByFamilyId(families.findByName(name).get(0).getId());
for (FamilyRelationship r : relations) {
this.members.add(members.findById(r.getMemberId()).get());
}
System.out.println("Called: "+this.members);
}
public List<FamilyMember> getMembers() {
return members;
}
public void setMembers(List<FamilyMember> members) {
this.members = members;
}
}

@ -23,6 +23,8 @@ public class FamilyMember {
Long mobileDeviceId;
FamilyMember(){}
public FamilyMember(String firstName, String lastName, Long mobileDeviceId) {
this.firstName = firstName;
this.lastName = lastName;

@ -1,10 +1,14 @@
package sig.family;
import java.util.Collection;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface FamilyRelationshipRepository extends CrudRepository<FamilyRelationship,Long>{
List<FamilyRelationship> findByMemberId(Long memberId);
List<FamilyRelationship> findByMemberIdAndRelationshipIn(Long memberId,Collection<String> relationships);
List<FamilyRelationship> findByFamilyIdAndRelationshipIn(Long familyId,Collection<String> relationships);
List<FamilyRelationship> findByFamilyId(Long familyId);
List<FamilyRelationship> findByFamilyIdAndMemberId(Long familyId,Long memberId);
}

@ -1,6 +1,9 @@
package sig.family;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface FamilyRepository extends CrudRepository<Family,Long>{
List<Family> findByName(String name);
}

@ -22,15 +22,18 @@ public class KnownLocation {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long id;
double x,y;
@Column(name="x")
double longitude;
@Column(name="y")
double latitude;
String name;
boolean isSafe;
public KnownLocation() {}
public KnownLocation(double x, double y, String name, boolean isSafe) {
this.x = x;
this.y = y;
this.longitude = x;
this.latitude = y;
this.name=name;
this.isSafe=isSafe;
}
@ -41,16 +44,16 @@ public class KnownLocation {
this.id = id;
}
public double getX() {
return x;
return longitude;
}
public void setX(double x) {
this.x = x;
this.longitude = x;
}
public double getY() {
return y;
return latitude;
}
public void setY(double y) {
this.y = y;
this.latitude = y;
}
public String getName() {

@ -1,7 +1,9 @@
package sig.family;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface KnownLocationRepository extends CrudRepository<KnownLocation,Long>{
List<KnownLocation> findByLongitudeAndLatitude(double x,double y);
}

@ -0,0 +1,90 @@
package sig.family;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
@Entity
@Table(name="notifications")
@RequestMapping
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Notification {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long id;
String message;
Long memberId;
int notificationType;
@Column(columnDefinition="datetime")
@JsonFormat(pattern="MM-dd-yyyy HH:mm:ss")
Date date;
public Notification() {}
public Notification(String message, Long memberId, int notificationType, Date date) {
this.message = message;
this.memberId = memberId;
this.notificationType = notificationType;
this.date = date;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Long getMemberId() {
return memberId;
}
public void setMemberId(Long memberId) {
this.memberId = memberId;
}
public int getNotificationType() {
return notificationType;
}
public void setNotificationType(int notificationType) {
this.notificationType = notificationType;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Notification(Long id, String message, Long memberId, int notificationType, Date date) {
this.id = id;
this.message = message;
this.memberId = memberId;
this.notificationType = notificationType;
this.date = date;
}
}

@ -0,0 +1,9 @@
package sig.family;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface NotificationRepository extends CrudRepository<Notification,Long>{
List<Notification> findByMemberId(Long memberid);
}
Loading…
Cancel
Save