public void ChangeRotationNotification(Database.Rotations rotation, string content, string type) { var usersRotations = _context.UsersRotations.ToList(); bool hasUsers = false; foreach (var userRotation in usersRotations) { if (userRotation.RotationId == rotation.RotationId) { hasUsers = true; break; } } Database.Notifications notification = new Notifications() { Content = content, Type = type, Created = DateTime.Now }; if (hasUsers) { _context.Notifications.Add(notification); _context.SaveChanges(); } foreach (var userRotation in usersRotations) { if (userRotation.RotationId == rotation.RotationId) { Database.UsersNotifications not = new UsersNotifications() { Notification = notification, UserId = userRotation.UserId }; _context.UsersNotifications.Add(not); _context.SaveChanges(); } } }
public void DeleteRotationNotification(Database.Rotations rotation, string content, string type) { var usersRotations = _context.UsersRotations.ToList(); var hasUsers = false; foreach (var usersRotation in usersRotations) { if (usersRotation.RotationId == rotation.RotationId) { hasUsers = true; break; } } Database.Notifications notification = new Notifications() { Content = content, Type = type, Created = DateTime.Now }; var notifications = _context.Notifications.ToList(); bool sameNot = false; var notId = 0; foreach (var n in notifications) { if (n.Content == content) { sameNot = true; notId = n.NotificationId; break; } } if (hasUsers && !sameNot) { _context.Notifications.Add(notification); _context.SaveChanges(); } foreach (var usersRotation in usersRotations) { if (usersRotation.RotationId == rotation.RotationId) { Database.UsersNotifications not = new UsersNotifications() { UserId = usersRotation.UserId }; if (sameNot) { not.NotificationId = notId; } else { not.Notification = notification; } _context.UsersNotifications.Add(not); _context.UsersRotations.Remove(usersRotation); _context.SaveChanges(); } } _context.Rotations.Remove(rotation); _context.SaveChanges(); }