public async Task DeleteStudentAsync(int studentId) { FirestoreDb db = FirestoreDb.Create(Project); Query docRef = db.Collection(Collection).WhereEqualTo("Id", studentId).Limit(1); QuerySnapshot snapshot = await docRef.GetSnapshotAsync(); if (snapshot.Count > 0) { DocumentReference studentRef = db.Collection(Collection).Document(snapshot.ElementAt(0).Id); await studentRef.DeleteAsync(); } }
public async Task <StudentDetailModel> GetStudentByIdAsync(int studentId) { FirestoreDb db = FirestoreDb.Create(Project); Query docRef = db.Collection(Collection).WhereEqualTo("Id", studentId).Limit(1); QuerySnapshot snapshot = await docRef.GetSnapshotAsync(); if (snapshot.Count > 0) { Student student = snapshot.ElementAt(0).ConvertTo <Student>(); return(new StudentDetailModel() { About = student.About, EnrollmentDate = student.EnrollmentDate.ToDateTime(), FirstName = student.FirstName, Id = student.Id, LastName = student.LastName }); } else { return(null); } }
public async Task UpdateStudentAsync(StudentDetailModel student) { FirestoreDb db = FirestoreDb.Create(Project); Query docRef = db.Collection(Collection).WhereEqualTo("Id", student.Id).Limit(1); QuerySnapshot snapshot = await docRef.GetSnapshotAsync(); if (snapshot.Count > 0) { DocumentReference studentRef = db.Collection(Collection).Document(snapshot.ElementAt(0).Id); Dictionary <string, object> updates = new Dictionary <string, object> { { nameof(student.About), student.About }, { nameof(student.EnrollmentDate), Timestamp.FromDateTime(student.EnrollmentDate.ToUniversalTime()) }, { nameof(student.FirstName), student.FirstName }, { nameof(student.LastName), student.LastName } }; await studentRef.UpdateAsync(updates); } }