public IActionResult EventRegistration(int singerId, int eventId, [Bind] EventRegistrationViewModel model)
        {
            if (model.RegistrationDeadline < DateTime.Now)
            {
                return(RedirectToAction(nameof(RegistrationDeadline)));
            }

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

            if (user == null || user.SingerId != singerId)
            {
                return(RedirectToAction("AccessDenied", "Account"));
            }
            else
            {
                var newRegistration = model.ToEventRegistration();
                newRegistration.RegistrationDate = DateTime.Now;
                newRegistration.Answer           = true;
                var oldRegistration = _eventRegistrationMapper.FindBy(er => er.SingerId == singerId && er.EventId == eventId).FirstOrDefault();
                if (oldRegistration == null)
                {
                    newRegistration.Id = null;
                    _eventRegistrationMapper.Create(newRegistration);
                }
                else
                {
                    oldRegistration.Answer           = newRegistration.Answer;
                    oldRegistration.Comment          = newRegistration.Comment;
                    oldRegistration.DressOrder       = newRegistration.DressOrder;
                    oldRegistration.RegistrationDate = newRegistration.RegistrationDate;
                    _eventRegistrationMapper.Update(oldRegistration);
                }
                return(RedirectToAction("Index", "Events"));
            }
        }
Beispiel #2
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));
        }