Created
March 27, 2019 09:25
-
-
Save MaryKuz/eb231e3b1ed7431be3327a023c4d8a04 to your computer and use it in GitHub Desktop.
StudentRepositry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StudentRepository implements IStudentRepository { | |
@Override | |
public void addStudent(Student student, OnSaveStudentCallback callback) { | |
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance()); | |
realm.beginTransaction(); | |
Student realmStudent = realm.createObject(Student.class); | |
realmStudent.setId(UUID.randomUUID().toString()); | |
realmStudent.setName(student.getName()); | |
realmStudent.setBirthday(student.getBirthday()); | |
realmStudent.setEmail(student.getEmail()); | |
realm.commitTransaction(); | |
if (callback != null) | |
callback.onSuccess(); | |
} | |
@Override | |
public void addStudentByUniversityId(Student student, String universityId, OnSaveStudentCallback callback) { | |
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance()); | |
realm.beginTransaction(); | |
Student realmStudent = realm.createObject(Student.class); | |
realmStudent.setId(UUID.randomUUID().toString()); | |
realmStudent.setName(student.getName()); | |
realmStudent.setEmail(student.getEmail()); | |
realmStudent.setBirthday(student.getBirthday()); | |
University university = realm.where(University.class).equalTo(RealmTable.ID, universityId).findFirst(); | |
university.getStudents().add(realmStudent); | |
realm.commitTransaction(); | |
if (callback != null) | |
callback.onSuccess(); | |
} | |
@Override | |
public void deleteStudentById(String id, OnDeleteStudentCallback callback) { | |
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance()); | |
realm.beginTransaction(); | |
Student result = realm.where(Student.class).equalTo(RealmTable.ID, id).findFirst(); | |
result.removeFromRealm(); | |
realm.commitTransaction(); | |
if (callback != null) | |
callback.onSuccess(); | |
} | |
@Override | |
public void deleteStudentByPosition(int position, OnDeleteStudentCallback callback) { | |
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance()); | |
realm.beginTransaction(); | |
RealmQuery query = realm.where(Student.class); | |
RealmResults results = query.findAll(); | |
results.remove(position); | |
realm.commitTransaction(); | |
if (callback != null) | |
callback.onSuccess(); | |
} | |
@Override | |
public void getAllStudents(OnGetAllStudentsCallback callback) { | |
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance()); | |
RealmResults results = realm.where(Student.class).findAll(); | |
if (callback != null) | |
callback.onSuccess(results); | |
} | |
@Override | |
public void getAllStudentsByUniversityId(String id, OnGetStudentsCallback callback) { | |
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance()); | |
University university = realm.where(University.class).equalTo(RealmTable.ID, id).findFirst(); | |
RealmList students = university.getStudents(); | |
if (callback != null) | |
callback.onSuccess(students); | |
} | |
@Override | |
public void getStudentById(String id, OnGetStudentByIdCallback callback) { | |
Realm realm = Realm.getInstance(SimpleRealmApp.getInstance()); | |
Student student = realm.where(Student.class).equalTo(RealmTable.ID, id).findFirst(); | |
if (callback != null) | |
callback.onSuccess(student); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment