Beispiel #1
0
        public override Task OnConnected()
        {
            if (!Context.User.Identity.IsAuthenticated)
            {
                return base.OnConnected();
            }

            User currentUser = new User();
            var notifications = new List<Notification>();
            var recordedNotifications = new List<NotificationViewModel>();
            using (var db = new PetCareDbContext())
            {
                currentUser = db.Users.FirstOrDefault(x => x.UserName == Context.User.Identity.Name);
                if (currentUser.IsVet || currentUser.Pets.Count == 0)
                {
                    return base.OnConnected();
                }

                var room = db.Rooms.FirstOrDefault(x => x.UserId == currentUser.Id);
                if (room == null)
                {
                    db.Rooms.Add(room = new Room()
                    {
                        Name = currentUser.UserName + "Room",
                        UserId = currentUser.Id
                    });
                    db.SaveChanges();
                }

                Groups.Add(Context.ConnectionId, room.Name);

                var pets = currentUser.Pets.Where(x => x.HealthRecord != null).ToList();

                if (pets.Count == 0)
                {
                    return base.OnConnected();
                }

                var visits = pets
                    .SelectMany(x => x.HealthRecord.VetVisits)
                    .Where(x => x.DateTime.Day > DateTime.UtcNow.Day && x.DateTime.Day <= DateTime.UtcNow.AddDays(1).Day)
                    .ToList();

                foreach (var visit in visits)
                {
                    if (visit.Notification == null)
                    {
                        var viewModel = new NotificationViewModel()
                        {
                            DateTime = DateTime.UtcNow,
                            Message = string.Format(GlobalConstants.VetVisitNotificationInThreeDays, visit.Pet.Pet.Name, visit.Vet.FirstName, visit.Vet.LastName),
                            User = currentUser,
                            IsSeen = false,
                            VetVisitId = visit.Id
                        };

                        var notification = AutoMapper.Mapper.Map<NotificationViewModel, Notification>(viewModel);
                        db.Notifications.Add(notification);
                    }
                    else
                    {
                        notifications.Add(visit.Notification);
                    }

                }
                db.SaveChanges();

                recordedNotifications = notifications
                    .Where(x => x.User.Id == currentUser.Id)
                    .AsQueryable()
                    .ProjectTo<NotificationViewModel>()
                    .ToList();
            }

            var anonymous = recordedNotifications.Select(x => new
            {
                DateTime = x.DateTime,
                IsSeen = x.IsSeen,
                Message = x.Message,
                VetVisitId = x.VetVisitId
            }).ToList();

            var json = JsonConvert.SerializeObject(anonymous);
            Clients.Caller.notify(json);

            return base.OnConnected();
        }
Beispiel #2
0
 public void Add(Room room)
 {
     this.rooms.Add(room);
     this.rooms.SaveChanges();
 }