public async Task DeleteGradeAscync(object sender) { var listBoxItem = sender as System.Windows.Controls.ListBoxItem; //get sender gradeIndex = int.Parse(listBoxItem.Tag.ToString()); var listaPrzedmiotow = Subjects.ToList(); foreach (SubjectModel przedmiot in listaPrzedmiotow) { var oceny = przedmiot.Grades.ToList(); foreach (GradeModel ocena in oceny) { if (gradeIndex == ocena.GradeId) { await Task.Delay(200); przedmiot.Grades.Remove(ocena); } } } //Usunięcie oceny z bazy using (var context = new StudentAppContext()) { DBMark dbm = context.DBMarks.FirstOrDefault(x => x.DBMarkId == gradeIndex); context.DBMarks.Remove(dbm); context.SaveChanges(); } }
public void AddNewGrade(string grade, string wage) { if (!isEditing) { //Dodanie oceny do bazy danych int itemId; using (var context = new StudentAppContext()) { DBSubject dbs = context.DBSubjects.FirstOrDefault(x => x.SubjectName == chosenSubject.SubjectName); DBMark dbmark = new DBMark(); //{ Mark = int.Parse(Grade), Date = DateTime.Now, DBSubject = dbs}; dbmark.Mark = double.Parse(Grade); dbmark.Date = DateTime.Now; dbmark.DBSubject = dbs; dbmark.Subject = chosenSubject.SubjectName; dbs.Marks.Add(dbmark); //context.Entry(dbs).State = EntityState.Modified; context.SaveChanges(); var ostatniObiekt = context.DBMarks.OrderByDescending(x => x.DBMarkId).FirstOrDefault(); itemId = ostatniObiekt.DBMarkId; } chosenSubject.Grades.Add(new GradeModel { GradeId = itemId, Date = DateTime.Now, GradeValue = Convert.ToDouble(Grade), Wage = Convert.ToDouble(Wage), Type = SelectedType }); } else { var listaPrzedmiotow = Subjects.ToList(); foreach (SubjectModel przedmiot in listaPrzedmiotow) { var oceny = przedmiot.Grades.ToList(); foreach (GradeModel ocena in oceny) { if (gradeIndex == ocena.GradeId) { ocena.GradeValue = double.Parse(grade); ocena.Wage = double.Parse(wage); Subjects.Refresh(); } } } //Edycja oceny w bazie danych using (var context = new StudentAppContext()) { DBMark dbm = context.DBMarks.FirstOrDefault(x => x.DBMarkId == gradeIndex); dbm.Mark = double.Parse(grade); context.SaveChanges(); } isEditing = false; } Grade = ""; Wage = ""; selectedType = "inne"; IsDialogOpen = false; HelperTextGrade = ""; }