using System; using System.Collections.Generic; using System.Linq; using System.Web; using Neo.Afx.ComponentModel; using Neo.Afx.Services.Notifications.Entities; namespace Neo.Afx.Services.Notifications { /// /// Notifications database context container /// public class NotificationsDatabase : Database { /// /// Initializes a new instance of the class. /// public NotificationsDatabase() : base(new NotificationsContext()) { } #region UserName /// /// Gets the name of the user. /// /// /// The name of the user. /// public string UserName { get { if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated) { return HttpContext.Current.User.Identity.Name.ToLower(); } return ""; } } #endregion #region Create /// /// Creates the specified notification. /// /// The userid that the notification will be displayed to. /// The entity ID. /// The item ID. /// The notification Category ID. /// /// The notification Type ID. /// The notification. /// public KeyValuePair Create(long userID, int entityID, long itemID, short notificationCategoryID, short notificationTypeID, string notification) { KeyValuePair returnResult; try { var result = Context.Pr_NotificationCreate(userID, entityID, itemID, notificationCategoryID, notificationTypeID, notification, UserName).SingleOrDefault(); returnResult = result.HasValue ? new KeyValuePair(result.Value, (result.Value != -1) ? String.Empty : "Unknown error occured") : new KeyValuePair(-1, "Unknown error occured"); } catch (Exception ex) { returnResult = new KeyValuePair(-1, ex.InnerException.Message); } return returnResult; } #endregion #region MarkAsRead /// /// Marks a notification as read. /// /// The notification ID. /// public string MarkAsRead(long notificationID) { string error; try { var result = Context.Pr_NotificationMarkAsRead(notificationID, UserName).SingleOrDefault(); if (result.HasValue) { error = String.Empty; } else { error = "Unknown error occured"; } } catch (Exception ex) { error = ex.InnerException.Message; } return error; } #endregion #region GetNotification /// /// Gets the notification. /// /// The notification ID. /// public Vw_Notifications GetNotification(long notificationID) { return Context.Vw_Notifications.Where(a => a.NotificationID == notificationID).FirstOrDefault(); } #endregion #region GetNotifications /// /// Gets the messages. /// /// The entity ID. /// The item ID. /// public IEnumerable GetNotifications(long userID, int entityID, long itemID, bool unreadOnly) { return Context.Pr_NotificationSearch(userID, entityID, itemID, unreadOnly).ToList(); } #endregion } }