Beispiel #1
0
        public async Task <ActionResult <Notification> > upDateWatchList([FromBody] dynamic Userchanges)
        {
            // int notificationID = toWatch.notifivationID;
            //
            string usrguid = Userchanges.userGuid;
            var    user    = _context.DBUser.Include("watchingNotificcations").Where(x => x.UUID == Guid.Parse(usrguid)).FirstOrDefault();

            if (user == null)
            {
                return(BadRequest("user not registered"));
            }
            JArray changes = Userchanges.changes;

            if (changes.Count == 0)
            {
                return(NoContent());
            }
            HashSet <int> changedNotifications = new HashSet <int>();

            foreach (dynamic change in changes)
            {
                changedNotifications.Add((int)change.ID);
            }
            var notifications = _context.Notification.Include(x => x.RegisteredUsers).Where(x => changedNotifications.Contains(x.NotifivationID)).AsQueryable();

            foreach (dynamic change in changes)
            {
                int  id         = change.ID;
                bool watch      = change.watchState;
                var  notific    = notifications.Where(x => x.NotifivationID == id).FirstOrDefault();
                var  user_notif = new NotificationDBUser()
                {
                    UserUUID       = user.UUID,
                    NotifivationID = notific.NotifivationID
                };
                if (watch)
                {
                    notific.RegisteredUsers.Add(user_notif);
                    user.watchingNotificcations.Add(user_notif);
                }
                else
                {
                    var rev = notific.RegisteredUsers.Where(x => x.UserUUID == user_notif.UserUUID).FirstOrDefault();
                    notific.RegisteredUsers.Remove(rev);
                    user.watchingNotificcations.Remove(rev);
                    //_context.NotificationDBUser.Remove(rev);
                }
            }
            _context.SaveChanges();

            return(NoContent());
        }
Beispiel #2
0
        [Route("watch")]///not used
        public async Task <ActionResult <Notification> > watchNotification([FromBody] dynamic toWatch)
        {
            int    notificationID = toWatch.notifivationID;
            var    notification   = _context.Notification.Include(x => x.RegisteredUsers).Where(x => x.NotifivationID == notificationID).FirstOrDefault();
            string usrguid        = toWatch.userGuid;
            var    user           = _context.DBUser.Include("watchingNotificcations").Where(x => x.UUID == Guid.Parse(usrguid)).FirstOrDefault();

            if (notification == null || user == null)
            {
                return(NotFound());
            }

            var user_notif = new NotificationDBUser()
            {
                //DBUser = user,
                UserUUID = user.UUID,
                // Notification = notification,
                NotifivationID = notification.NotifivationID
            };

            if (user.watchingNotificcations.AsQueryable().Any(x => x.NotifivationID == notification.NotifivationID))  // user already registered for this notification
            {
                return(notification);
            }

            try {
                notification.RegisteredUsers.Add(user_notif);
                user.watchingNotificcations.Add(user_notif);
                _context.SaveChanges();
            }
            catch (Exception err) {
                Console.WriteLine(err);
            }
            //

            return(notification);
        }