diff --git a/src/main/java/sig/family/Endpoints.java b/src/main/java/sig/family/Endpoints.java index 9a15a99..9cea62e 100644 --- a/src/main/java/sig/family/Endpoints.java +++ b/src/main/java/sig/family/Endpoints.java @@ -71,8 +71,13 @@ public class Endpoints { } @GetMapping("/family/{id}") - public Optional _2(@PathVariable Long id) { - return families.findById(id); + public FamilyContainer _2(@PathVariable Long id) { + if (families.existsById(id)) { + Family f = families.findById(id).get(); + return new FamilyContainer(f.getName(),families,relationships,members); + } else { + return null; + } } @PostMapping("/family") @@ -173,20 +178,24 @@ public class Endpoints { 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 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())); + if (m.getLastLocationId()==null||m.getLastLocationId()!=loc.get(0).getId()) { + 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 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())); + } } } + m.setLastLocationId(loc.get(0).getId()); + members.save(m); } } diff --git a/src/main/java/sig/family/FamilyContainer.java b/src/main/java/sig/family/FamilyContainer.java index 4f3b9a0..b55ab66 100644 --- a/src/main/java/sig/family/FamilyContainer.java +++ b/src/main/java/sig/family/FamilyContainer.java @@ -21,17 +21,18 @@ import com.fasterxml.jackson.annotation.JsonInclude; public class FamilyContainer extends Family{ List members = new ArrayList<>(); + Long id = -1l; FamilyContainer(String name ,FamilyRepository families ,FamilyRelationshipRepository relationships ,FamilyMemberRepository members) { super(name); - List relations = relationships.findByFamilyId(families.findByName(name).get(0).getId()); + id = families.findByName(name).get(0).getId(); + List relations = relationships.findByFamilyId(id); for (FamilyRelationship r : relations) { this.members.add(members.findById(r.getMemberId()).get()); } - System.out.println("Called: "+this.members); } public List getMembers() { @@ -41,4 +42,12 @@ public class FamilyContainer extends Family{ public void setMembers(List members) { this.members = members; } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } } diff --git a/src/main/java/sig/family/FamilyMember.java b/src/main/java/sig/family/FamilyMember.java index 0293d41..f9592eb 100644 --- a/src/main/java/sig/family/FamilyMember.java +++ b/src/main/java/sig/family/FamilyMember.java @@ -23,6 +23,8 @@ public class FamilyMember { Long mobileDeviceId; + Long lastLocationId; + FamilyMember(){} public FamilyMember(String firstName, String lastName, Long mobileDeviceId) { @@ -55,4 +57,12 @@ public class FamilyMember { public void setMobileDeviceId(Long mobileDeviceId) { this.mobileDeviceId = mobileDeviceId; } + + public Long getLastLocationId() { + return lastLocationId; + } + + public void setLastLocationId(Long lastLocationId) { + this.lastLocationId = lastLocationId; + } }