/// <summary> /// Create the notification. /// </summary> /// <param name="notification">Notification.</param> /// <returns>int</returns> public async Task <NotificationDTO> CreateNotification(NotificationDTO notification) { using (var context = new NotificationsContext()) { var newNotification = new Notification { Id = notification.Id, Channel = notification.Channel, Receiver = notification.Receiver, Type = notification.Type, Title = notification.Title, Body = notification.Body, IsReaded = notification.IsReaded, CreatedDate = DateTime.Now, Protocol = notification.Protocol }; context.Notifications.Add(newNotification); await context.SaveChangesAsync(); notification.Id = newNotification.Id; notification.CreatedDate = newNotification.CreatedDate; return(notification); } }
public async Task <ActionResult <Notifications> > PostNotifications(Notifications notifications, string NoteId) { _context.Notifications.Add(notifications); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (NotificationsExists(NoteId)) { return(Conflict()); } else { throw; } } return(CreatedAtAction("GetNotifications", new { NoteId = notifications.NoteId }, notifications)); }
/// <summary> /// Set notification as Readed. /// </summary> /// <param name="notificationId">The notification Id.</param> /// <returns>void</returns> public async Task SetNotificationAsReaded(int notificationId) { using (var context = new NotificationsContext()) { var notificationToEdit = context.Notifications.FirstOrDefault(t => t.Id == notificationId); if (notificationToEdit != null) { notificationToEdit.IsReaded = true; await context.SaveChangesAsync(); } } }
public async Task <Unit> Handle(SendEmailCommand request, CancellationToken cancellationToken) { var email = Email.Create(request.Subject, request.Body, request.Addresses); //for simplicity I assume that add email to the database is like sent it _context.Emails.Add(email); var value = await _context.SaveChangesAsync(); if (value == 0) { throw new EmailWasntSentException(); } return(Unit.Value); }
public async Task <NotificationSubscription> Subscribe(NotificationSubscription subscription) { // We're storing at most one subscription per user, so delete old ones. // Alternatively, you could let the user register multiple subscriptions from different browsers/devices. var userId = subscription.UserId; var oldSubscriptions = _db.NotificationSubscriptions.Where(e => e.UserId == userId); _db.NotificationSubscriptions.RemoveRange(oldSubscriptions); // Store new subscription subscription.UserId = userId; _db.NotificationSubscriptions.Attach(subscription); await _db.SaveChangesAsync(); Console.WriteLine($"New subscription with id: {subscription.NotificationSubscriptionId}"); return(subscription); }