Fix endpoints, null checks

master
Joshua Sigona 4 years ago
parent 2338ecec39
commit 7e5ba88e8c
  1. 37
      src/main/java/sig/family/Endpoints.java
  2. 13
      src/main/java/sig/family/FamilyContainer.java
  3. 10
      src/main/java/sig/family/FamilyMember.java

@ -71,8 +71,13 @@ public class Endpoints {
}
@GetMapping("/family/{id}")
public Optional<Family> _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<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()));
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<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()));
}
}
}
m.setLastLocationId(loc.get(0).getId());
members.save(m);
}
}

@ -21,17 +21,18 @@ import com.fasterxml.jackson.annotation.JsonInclude;
public class FamilyContainer extends Family{
List<FamilyMember> members = new ArrayList<>();
Long id = -1l;
FamilyContainer(String name
,FamilyRepository families
,FamilyRelationshipRepository relationships
,FamilyMemberRepository members) {
super(name);
List<FamilyRelationship> relations = relationships.findByFamilyId(families.findByName(name).get(0).getId());
id = families.findByName(name).get(0).getId();
List<FamilyRelationship> relations = relationships.findByFamilyId(id);
for (FamilyRelationship r : relations) {
this.members.add(members.findById(r.getMemberId()).get());
}
System.out.println("Called: "+this.members);
}
public List<FamilyMember> getMembers() {
@ -41,4 +42,12 @@ public class FamilyContainer extends Family{
public void setMembers(List<FamilyMember> members) {
this.members = members;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

@ -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;
}
}

Loading…
Cancel
Save