Example #1
0
        public void NewNotificationCardView(int notificationCarId, string userId)
        {
            NotificationCard card = context.NotificationCards
                                    .FirstOrDefault(c => c.NotificationCardId == notificationCarId);

            NotificationViews view = new NotificationViews
            {
                UserId = userId
            };

            context.Attach(card);
            view.CreatedAt = DateTime.Now;
            view.UpdatedAt = DateTime.Now;
            context.NotificationViews.Add(view);
            context.SaveChanges();
            card.UpdatedAt = DateTime.Now;
            card.NotificationViews.Add(view);
            context.SaveChanges();
        }
Example #2
0
        //notifications
        public void NewNotification(string userId, string subject, string For,
                                    int forId, string forUserId = null)
        {
            Notification notification = context.Notifications
                                        .FirstOrDefault(n => n.Subject == subject && n.For == For &&
                                                        n.ForId == forId);

            if (notification != null)
            {
                AppUser user = userManager.Users
                               .FirstOrDefault(u => u.Id == userId);
                string userName = (user.FirstName == null && user.LastName == null) ?
                                  user.UserName : $"{user.FirstName} {user.LastName}";
                string forName    = "";
                int    totalUsers = 0;

                NotificationCard newNotificationCard = new NotificationCard
                {
                    CreatedBy = user.Id,
                    CreatedAt = DateTime.Now,
                    UpdatedAt = DateTime.Now
                };

                if (notification.Subject == "NewComment")
                {
                    switch (notification.For)
                    {
                    case "Video":
                        forName = context.Videos
                                  .FirstOrDefault(v => v.Id == notification.ForId).Title;
                        totalUsers = context.Comments.Where(c => c.For == "Video" &&
                                                            c.ForId == notification.ForId)
                                     .Select(c => c.CreatedBy).Distinct().Count() - 1;
                        break;

                    case "Course":
                        forName = context.Courses
                                  .FirstOrDefault(c => c.CourseId == notification.ForId).Title;
                        totalUsers = context.Comments.Where(c => c.For == "Course" &&
                                                            c.ForId == notification.ForId)
                                     .Select(c => c.CreatedBy).Distinct().Count() - 1;
                        break;
                    }

                    newNotificationCard.Msg = (totalUsers >= 1) ?
                                              $"{userName} and {totalUsers} others comment on {notification.For} '{forName}'" :
                                              $"{userName} comment on {notification.For} '{forName}'";

                    context.Attach(notification);
                    context.NotificationCards.Add(newNotificationCard);
                    notification.NotificationCards.Add(newNotificationCard);
                    //set datetime of last notification
                    notification.NotificationDateAdded = DateTime.Now;
                    context.SaveChanges();
                }

                else if (notification.Subject == "LikeDislikeVideo")
                {
                    forName = context.Videos
                              .FirstOrDefault(v => v.Id == notification.ForId).Title;

                    //all likes and dislikes on video
                    totalUsers = context.Likes
                                 .Where(l => l.For == "Video" && l.ForId == notification.ForId)
                                 .Select(c => c.UserId).Count();
                    totalUsers += context.Dislikes
                                  .Where(d => d.For == "Video" && d.ForId == notification.ForId)
                                  .Select(c => c.UserId).Count();

                    newNotificationCard.Msg = (totalUsers > 1) ?
                                              $"{userName} and {totalUsers} others reacted on your video '{forName}'" :
                                              $"{userName} reacted on your video '{forName}'";

                    context.Attach(notification);
                    context.NotificationCards.Add(newNotificationCard);
                    notification.NotificationCards.Add(newNotificationCard);
                    //set datetime of last notification
                    notification.NotificationDateAdded = DateTime.Now;
                    context.SaveChanges();
                }

                else if (notification.Subject == "LikeDislikeComment")
                {
                    //notification.for = "Comment"
                    //notification.forID = CommentId
                    //get comment where commentId == notification.forID
                    Comment comment = context.Comments
                                      .FirstOrDefault(c => c.Id == notification.ForId);

                    //all likes and dislikes for comment where comment.id = notification.forId
                    totalUsers = context.Likes
                                 .Where(l => l.For == "Comment" && l.ForId == notification.ForId)
                                 .Select(c => c.UserId).Count() - 1;
                    totalUsers += context.Dislikes
                                  .Where(d => d.For == "Comment" && d.ForId == notification.ForId)
                                  .Select(c => c.UserId).Count() - 1;

                    string commentContent = (comment.Content.Length > 25) ?
                                            comment.Content.Substring(0, 25) + "..." : comment.Content;

                    newNotificationCard.Msg = (totalUsers > 1) ?
                                              $"{userName} and {totalUsers} others reacted on your comment '{commentContent}'" :
                                              $"{userName} reacted on your comment for '{commentContent}'";

                    context.Attach(notification);
                    context.NotificationCards.Add(newNotificationCard);
                    notification.NotificationCards.Add(newNotificationCard);
                    //set datetime of last notification
                    notification.NotificationDateAdded = DateTime.Now;
                    context.SaveChanges();
                }

                /*else if (notification.Subject == "NewVideo")
                 * {
                 *  //for all user on course
                 *  //if video for == course or presentation
                 * }*/
                else if (notification.Subject == "UserJoinCourse")
                {
                    Course course = context.Courses
                                    .FirstOrDefault(c => c.CourseId == notification.ForId);

                    newNotificationCard.Msg =
                        $"{userName} has joined the Course '{course.Title}'";

                    context.Attach(notification);
                    context.NotificationCards.Add(newNotificationCard);
                    notification.NotificationCards.Add(newNotificationCard);
                    //set datetime of last notification
                    notification.NotificationDateAdded = DateTime.Now;
                    context.SaveChanges();
                }
                else if (notification.Subject == "RepresentationRating")
                {
                    //for user who created representation
                    Video representationVideo = context.Videos
                                                .FirstOrDefault(v => v.Id == notification.ForId);

                    if (representationVideo != null)
                    {
                        Representation representation = context.Representations
                                                        .FirstOrDefault(r => r.RepresentationId == representationVideo.ForId);

                        newNotificationCard.Msg =
                            $"Your representation {representation.Title} has been rated.";

                        context.Attach(notification);
                        context.NotificationCards.Add(newNotificationCard);
                        notification.NotificationCards.Add(newNotificationCard);
                        //set datetime of last notification
                        notification.NotificationDateAdded = DateTime.Now;
                        context.SaveChanges();
                    }
                }
                else if (notification.Subject == "NewRepresentation")
                {
                    //for user who created course
                    Video representationVideo = context.Videos
                                                .FirstOrDefault(v => v.Id == notification.ForId);

                    if (representationVideo != null)
                    {
                        Representation representation = context.Representations
                                                        .FirstOrDefault(r => r.RepresentationId == representationVideo.ForId);

                        newNotificationCard.Msg =
                            $"{userName} added a new representation '{representation.Title}'";

                        context.Attach(notification);
                        context.NotificationCards.Add(newNotificationCard);
                        notification.NotificationCards.Add(newNotificationCard);
                        //set datetime of last notification
                        notification.NotificationDateAdded = DateTime.Now;
                        context.SaveChanges();
                    }
                }
                else if (notification.Subject == "AddUserToGroup")
                {
                    Group group = context.Groups.FirstOrDefault(g => g.GroupId == notification.ForId);

                    newNotificationCard.Msg =
                        $"{userName} added you to group '{group.Name}'";

                    context.Attach(notification);
                    context.NotificationCards.Add(newNotificationCard);
                    notification.NotificationCards.Add(newNotificationCard);
                    //set datetime of last notification
                    notification.NotificationDateAdded = DateTime.Now;
                    context.SaveChanges();
                }
                else if (notification.Subject == "NewGroupUser")
                {
                    Group group = context.Groups.FirstOrDefault(g => g.GroupId == notification.ForId);

                    newNotificationCard.Msg =
                        $"{userName} added you to group '{group.Name}'";

                    context.Attach(notification);
                    context.NotificationCards.Add(newNotificationCard);
                    notification.NotificationCards.Add(newNotificationCard);
                    //set datetime of last notification
                    notification.NotificationDateAdded = DateTime.Now;
                    context.SaveChanges();
                }

                notification.UpdatedAt = DateTime.Now;
                context.SaveChanges();
            }
            else
            {
                notification = new Notification
                {
                    Subject               = subject,
                    For                   = For,
                    ForId                 = forId,
                    ForUserId             = forUserId,
                    NotificationDateAdded = DateTime.Now,
                    CreatedAt             = DateTime.Now,
                    UpdatedAt             = DateTime.Now
                };
                context.Notifications.Add(notification);
                context.SaveChanges();

                NewNotification(userId, subject, For, forId);
            }
        }