public List <ANEventModel> GetJoinedEvent()
        {
            var hostId = this.CurrentUser.Id;
            var events = this.ANDBUnitOfWork.ANEventRepository.GetAll()
                         .Include(x => x.User)
                         .Include(x => x.User.UserProfiles)
                         .Include("User.UserProfiles.Image")
                         .Include(x => x.ANEventImages)
                         .Include("ANEventImages.Image")
                         .Include("ANEventInformations.ANEventLocation")
                         .Include(x => x.ANEventMembers)
                         .Where(e => e.ANEventMembers.Select(f => f.UserId).Contains(hostId)).OrderByDescending(t => t.CreatedDate).ToList();

            var anEventModels = new List <ANEventModel>();

            foreach (var ev in events)
            {
                var model = new ANEventModel();
                model.Id   = ev.Id;
                model.Host = UserMapper.ToModel(ev.User);
                // get cover image
                var coverImageEntity = ev.ANEventImages.Where(x => x.ANEventImageType == (int)Common.ANEventImageType.ANEventCoverImage).OrderBy(x => x.SortPriority).FirstOrDefault();
                if (coverImageEntity != null)
                {
                    model.CoverPhoto = ImageMapper.ToModel(coverImageEntity.Image);
                }
                var information = ev.ANEventInformations.FirstOrDefault();
                model.Information = ANEventInformationMapper.ToModel(information);
                if (model.Information != null)
                {
                    model.Information.ANEventLocation = ANEventLocationMapper.ToModel(information.ANEventLocation);
                }
                model.CreatedDate    = ev.CreatedDate;
                model.NumberOfMember = ev.ANEventMembers.Count;

                anEventModels.Add(model);
            }

            var groupEvents = anEventModels.GroupBy(t => t.CreatedDate.Year)
                              .Select(gr => new ANEventGroupModel
            {
                Year       = gr.Key,
                EventMonth = gr.GroupBy(m => new { m.CreatedDate.Year, m.CreatedDate.Month })
                             .Select(grm => new ANEventMonthGroup
                {
                    Month  = grm.Key.Month,
                    Events = grm.ToList()
                }).ToList()
            }).ToList();

            return(anEventModels);
        }
        public List <ANEventModel> GetEventsInNewFeeds()
        {
            var currentUserId = this.CurrentUser.Id;
            var sandbox       = new ANEventSearchingSandbox(this.ANDBUnitOfWork);
            var results       = sandbox.Search(new SearchingSandbox.Model.SearchCriteria()
            {
                UserId = currentUserId
            });

            var anEventEntities = this.ANDBUnitOfWork.ANEventRepository.GetAll()
                                  .Include("User.UserProfiles.Image")
                                  .Include("ANEventImages.Image")
                                  .Include("ANEventInformations")
                                  .Where(x => results.Contains(x.Id)).ToList()
                                  .OrderBy(x => results.IndexOf(x.Id)).ToList();

            var favoritedEntities = this.ANDBUnitOfWork.ANEventUserFavouriteRepository.GetAll().Where(x => x.ANEventId.HasValue && results.Contains(x.ANEventId.Value) && x.UserId.HasValue && x.UserId.Value == currentUserId).ToList();

            var anEventModels = new List <ANEventModel>();

            foreach (var entity in anEventEntities)
            {
                var anEventModel = ANEventMapper.ToModel(entity);

                // get host information
                if (entity.User != null)
                {
                    anEventModel.Host = UserMapper.ToModel(entity.User);
                    var firstProfile = entity.User.UserProfiles.FirstOrDefault();
                    anEventModel.Host.Profile = UserProfileMapper.ToModel(firstProfile);
                    if (anEventModel.Host.Profile != null)
                    {
                        anEventModel.Host.Profile.Avatar = ImageMapper.ToModel(firstProfile.Image);
                    }
                }
                // get cover image
                var coverImageEntity = entity.ANEventImages.Where(x => x.ANEventImageType == (int)Common.ANEventImageType.ANEventCoverImage).OrderBy(x => x.SortPriority).FirstOrDefault();
                if (coverImageEntity != null)
                {
                    anEventModel.CoverPhoto = ImageMapper.ToModel(coverImageEntity.Image);
                }
                // get information
                var information = entity.ANEventInformations.FirstOrDefault();
                anEventModel.Information           = ANEventInformationMapper.ToModel(information);
                anEventModel.ANEventUserFavourites = ANEventUserFavouriteMapper.ToModel(favoritedEntities.Where(x => x.ANEventId.HasValue && x.ANEventId.Value == anEventModel.Id));
                anEventModels.Add(anEventModel);
            }
            return(anEventModels);
        }
        public List <ANEventModel> GetMyEventFavourites()
        {
            var currentUserId   = this.CurrentUser.Id;
            var anEventEntities = this.ANDBUnitOfWork.ANEventUserFavouriteRepository.GetAll()
                                  .Where(x => x.UserId.HasValue && x.UserId.Value == currentUserId)
                                  .Select(x => x.ANEvent)
                                  .Include("User.UserProfiles.Image")
                                  .Include("ANEventImages.Image")
                                  .Include("ANEventInformations.ANEventLocation")
                                  .ToList()
                                  .OrderByDescending(x => x.CreatedDate).ToList();

            var anEventModels   = new List <ANEventModel>();
            var anEventIds      = anEventEntities.Select(x => x.Id).ToList();
            var numberOfMembers = this.ANDBUnitOfWork.ANEventRepository.GetAll()
                                  .Where(x => anEventIds.Contains(x.Id))
                                  .Select(x => new { Id = x.Id, numberOfMember = x.ANEventMembers.Count() }).ToList();

            foreach (var entity in anEventEntities)
            {
                var anEventModel = ANEventMapper.ToModel(entity);

                // get host information
                if (entity.User != null)
                {
                    anEventModel.Host = UserMapper.ToModel(entity.User);
                    var firstProfile = entity.User.UserProfiles.FirstOrDefault();
                    anEventModel.Host.Profile = UserProfileMapper.ToModel(firstProfile);
                    if (anEventModel.Host.Profile != null)
                    {
                        anEventModel.Host.Profile.Avatar = ImageMapper.ToModel(firstProfile.Image);
                    }
                }
                // get cover image
                var coverImageEntity = entity.ANEventImages.Where(x => x.ANEventImageType == (int)Common.ANEventImageType.ANEventCoverImage).OrderBy(x => x.SortPriority).FirstOrDefault();
                if (coverImageEntity != null)
                {
                    anEventModel.CoverPhoto = ImageMapper.ToModel(coverImageEntity.Image);
                }
                // get information
                var information = entity.ANEventInformations.FirstOrDefault();
                anEventModel.Information    = ANEventInformationMapper.ToModel(information);
                anEventModel.NumberOfMember = numberOfMembers.Where(x => x.Id == anEventModel.Id).Select(x => x.numberOfMember).FirstOrDefault();
                anEventModel.Information.ANEventLocation = ANEventLocationMapper.ToModel(information.ANEventLocation);
                anEventModels.Add(anEventModel);
            }
            return(anEventModels);
        }
        public List <ANEventModel> GetEventInWeek()
        {
            DateTime input  = DateTime.Now;
            int      delta  = DayOfWeek.Monday - input.DayOfWeek;
            int      first  = input.AddDays(delta).Day;
            int      last   = first + 7;
            var      hostId = this.CurrentUser.Id;
            var      events = this.ANDBUnitOfWork.ANEventRepository.GetAll()
                              .Include(x => x.User)
                              .Include(x => x.User.UserProfiles)
                              .Include("User.UserProfiles.Image")
                              .Include(x => x.ANEventImages)
                              .Include("ANEventImages.Image")
                              .Include("ANEventInformations.ANEventLocation")
                              .Include(x => x.ANEventMembers)
                              .Where(e => (e.UserId == (hostId) || e.ANEventMembers.Select(f => f.UserId).Contains(hostId)) &&
                                     e.ANEventInformations.FirstOrDefault().StartDate.Value.Day >= first &&
                                     e.ANEventInformations.FirstOrDefault().StartDate.Value.Day < last)
                              .OrderByDescending(t => t.CreatedDate).ToList();

            var anEventModels = new List <ANEventModel>();

            foreach (var ev in events)
            {
                var model = new ANEventModel();
                model.Id   = ev.Id;
                model.Host = UserMapper.ToModel(ev.User);
                // get cover image
                var coverImageEntity = ev.ANEventImages.Where(x => x.ANEventImageType == (int)Common.ANEventImageType.ANEventCoverImage).OrderBy(x => x.SortPriority).FirstOrDefault();
                if (coverImageEntity != null)
                {
                    model.CoverPhoto = ImageMapper.ToModel(coverImageEntity.Image);
                }
                var information = ev.ANEventInformations.FirstOrDefault();
                model.Information = ANEventInformationMapper.ToModel(information);
                if (model.Information != null)
                {
                    model.Information.ANEventLocation = ANEventLocationMapper.ToModel(information.ANEventLocation);
                }
                model.CreatedDate    = ev.CreatedDate;
                model.NumberOfMember = ev.ANEventMembers.Count;
                anEventModels.Add(model);
            }

            return(anEventModels);
        }
        public ANEventDetailInformationModel GetEventDetailInformation(int Id)
        {
            //Thread.Sleep(3000);
            var entity = this.ANDBUnitOfWork.ANEventRepository.GetAll()
                         .Include("ANEventInformations.ANEventLocation")
                         .Include("User.UserProfiles.Image")
                         .FirstOrDefault(x => x.Id == Id);

            if (entity == null)
            {
                return(null);
            }

            var firstInformation = entity.ANEventInformations.FirstOrDefault();
            var firstUserProfile = entity.User.UserProfiles.FirstOrDefault();
            var host             = UserMapper.ToModel(entity.User);

            if (host != null)
            {
                if (firstUserProfile != null)
                {
                    host.Profile = UserProfileMapper.ToModel(firstUserProfile);
                }

                if (firstUserProfile.Image != null)
                {
                    host.Profile.Avatar = ImageMapper.ToModel(firstUserProfile.Image);
                }
            }

            var informationModel = new ANEventDetailInformationModel()
            {
                EventID          = entity.Id,
                EventInformation = firstInformation != null?
                                   ANEventInformationMapper.ToModel(firstInformation) : null,
                                       Host = host
            };

            if (informationModel.EventInformation != null)
            {
                informationModel.EventInformation.ANEventLocation = ANEventLocationMapper.ToModel(firstInformation.ANEventLocation);
            }
            return(informationModel);
        }
        public List <ANEventModel> GetRelatedEvents(int eventId)
        {
            var hostId = this.ANDBUnitOfWork.ANEventRepository.GetAll().Where(x => x.Id == eventId).Select(x => x.UserId).FirstOrDefault().GetValueOrDefault();

            if (hostId == 0)
            {
                return(null);
            }
            var events = this.ANDBUnitOfWork.ANEventRepository.GetAll()
                         .Include(x => x.User)
                         .Include(x => x.User.UserProfiles)
                         .Include("User.UserProfiles.Image")
                         .Include(x => x.ANEventImages)
                         .Include("ANEventImages.Image")
                         .Include("ANEventInformations.ANEventLocation")
                         .Include(x => x.ANEventMembers)
                         .Where(e => e.UserId.HasValue && e.UserId.Value == hostId && e.Id != eventId).OrderByDescending(t => t.CreatedDate).Take(3).ToList();

            var anEventModels = new List <ANEventModel>();

            foreach (var ev in events)
            {
                var model = new ANEventModel();
                model.Id   = ev.Id;
                model.Host = UserMapper.ToModel(ev.User);
                // get cover image
                var coverImageEntity = ev.ANEventImages.Where(x => x.ANEventImageType == (int)Common.ANEventImageType.ANEventCoverImage).OrderBy(x => x.SortPriority).FirstOrDefault();
                if (coverImageEntity != null)
                {
                    model.CoverPhoto = ImageMapper.ToModel(coverImageEntity.Image);
                }
                var information = ev.ANEventInformations.FirstOrDefault();
                model.Information = ANEventInformationMapper.ToModel(information);
                if (model.Information != null)
                {
                    model.Information.ANEventLocation = ANEventLocationMapper.ToModel(information.ANEventLocation);
                }
                model.CreatedDate    = ev.CreatedDate;
                model.NumberOfMember = ev.ANEventMembers.Count;
                anEventModels.Add(model);
            }

            return(anEventModels);
        }