예제 #1
0
        public IActionResult Index(string filter = "upcoming")
        {
            IEnumerable <EventRegistration> singerRegistrations = new List <EventRegistration>();
            VoiceGroup?voice    = VoiceGroup.S1;
            int?       singerId = 0;

            var user = _userMapper.FindBy(u => u.UserName == User.Identity.Name).FirstOrDefault();

            if (user != null && user.SingerId != null)
            {
                singerRegistrations = _eventRegistrationMapper.FindBy(er => er.SingerId == user.SingerId).ToList();
                voice    = _singerMapper.Find(user.SingerId.Value).VoiceGroup;
                singerId = user.SingerId;
            }
            IQueryable <Event> events = new List <Event>().AsQueryable();

            if (filter == "upcoming")
            {
                events = _eventMapper.FindBy(e => e.To >= DateTime.Now).OrderBy(e => e.From);
            }
            else if (filter == "all")
            {
                events = _eventMapper.FindAll().OrderByDescending(e => e.From);
            }

            var eventIds              = events.Select(e => e.Id).ToList();
            var activeSingers         = _singerMapper.FindBy(s => s.IsActive == true).Count();
            var allEventRegistrations = _eventRegistrationMapper.FindBy(er => eventIds.Contains(er.EventId.Value)).ToList();

            var model = events.ToList().Select(e =>
            {
                var answeredYes      = allEventRegistrations.Where(er => er.EventId.Value == e.Id && er.Answer == true).Count();
                var answeredNo       = allEventRegistrations.Where(er => er.EventId.Value == e.Id && er.Answer == false).Count();
                var registrationInfo = singerRegistrations.FirstOrDefault(r => r.EventId == e.Id);
                if (registrationInfo == null)
                {
                    return(new SingerEventListViewModel(e, new EventRegistrationViewModel(voice, e.Id, singerId, e.EndOfRegistration), activeSingers, answeredYes, answeredNo));
                }
                else
                {
                    return(new SingerEventListViewModel(e, new EventRegistrationViewModel(registrationInfo, voice, e.EndOfRegistration), activeSingers, answeredYes, answeredNo));
                }
            }).OrderBy(ri => ri.From).AsEnumerable();

            return(View(model));
        }