Exemple #1
0
        public async Task <ActionResult> Messages()
        {
            var userId   = User.Identity.GetUserId();
            var messages = _userNotificationService.GetNewNotificationsFor(userId).Select(x => x.Notification).OfType <MessageNotification>().Include(x => x.Message).Include(x => x.Message.FromUser).Include(x => x.Message.Advertisement).ToList();

            return(PartialView("_Message", messages));
        }
Exemple #2
0
        public async Task <ActionResult> Message(int advertisementId)
        {
            var userIdCurrent = User.Identity.GetUserId();

            var messages =
                await
                _messageService.Queryable()
                .Where(x => x.AdvertisementId == advertisementId && (x.FromUserId == userIdCurrent || x.ToUserId == userIdCurrent))
                .Include(x => x.Advertisement)
                .Include(x => x.Advertisement.Photos)
                .Include(x => x.Advertisement.Car)
                .Include(x => x.Advertisement.User.Address)
                .Include(x => x.FromUser)
                .Include(x => x.ToUser)
                .OrderByDescending(x => x.Id)
                .ToListAsync();



            if (!messages.Any())
            {
                return(RedirectToAction("Messages"));
            }

            var viewModel = new MessageDetailsViewModel();

            viewModel.Messages = messages;
            if (messages.First().Advertisement.Photos.Any())
            {
                var picturesModel = messages.First().Advertisement.Photos.Select(x =>
                                                                                 new PictureModel()
                {
                    ID              = x.Id,
                    Url             = string.Format("data:{0};base64,{1}", x.Extension, Convert.ToBase64String(x.Content)),
                    AdvertisementId = messages.First().Advertisement.Id
                }).ToList();

                viewModel.Url = picturesModel.FirstOrDefault().Url;
            }


            var messageNotifications = _userNotificationService.GetNewNotificationsFor(userIdCurrent).Select(x => x.Notification).OfType <MessageNotification>().Include(x => x.Message).ToList();

            foreach (var messageNotification in messageNotifications)
            {
                foreach (var message in messages)
                {
                    if (message.Id == messageNotification.MessageId)
                    {
                        var markAsRead = await _userNotificationService.Query(x => x.NotificationId == messageNotification.Id).SelectAsync();

                        foreach (var userNotification in markAsRead)
                        {
                            userNotification.IsRead      = true;
                            userNotification.ObjectState = ObjectState.Modified;
                        }
                    }
                }
            }


            await _unitOfWorkAsync.SaveChangesAsync();

            return(View(viewModel));
        }