private Answer ValidateParticipantFields(ParticipantCreateUpdateViewModel viewmodel)
        {
            if (viewmodel.FullName.IsEmptyString())
            {
                return(Answer.Error("No Me Name"));
            }

            if (viewmodel.YearOfBirth < (DateTime.Today.Year - 120) ||
                viewmodel.YearOfBirth > DateTime.Today.Year)
            {
                return(Answer.Error("Illegal Year of Birth"));
            }


            return(Answer.Success);
        }
        public ParticipantCreateUpdateViewModel GetParticipantViewModel(string participantId)
        {
            if (participantId.IsEmptyString())
            {
                return(new ParticipantCreateUpdateViewModel());
            }
            else
            {
                var result = new ParticipantCreateUpdateViewModel
                {
                    Id = participantId,
                };

                if (participantId.StartsWith("LongTerm"))
                {
                    var longTerm = Session.Load <LongTermParticipant>(participantId);
                    if (longTerm != null)
                    {
                        result.YearOfBirth          = longTerm.YearOfBirth;
                        result.Email                = longTerm.Email;
                        result.FullName             = longTerm.FullName;
                        result.IsAllowingPromotions = longTerm.IsAllowingPromotions;
                        result.PhoneNumber          = longTerm.PhoneNumber;
                        return(result);
                    }
                }

                if (participantId.StartsWith("ShortTerm"))
                {
                    var shortTerm = Session.Load <ShortTermParticipant>(participantId);

                    if (shortTerm != null)
                    {
                        result.Email       = string.Empty;
                        result.YearOfBirth = shortTerm.YearOfBirth;
                        result.FullName    = shortTerm.FullName;
                        result.PhoneNumber = shortTerm.PhoneNumber;
                        return(result);
                    }
                }

                return(result);
            }
        }
        public async Task <Answer> UpdateParticipant(ParticipantCreateUpdateViewModel viewmodel)
        {
            var answer = ValidateParticipantFields(viewmodel);

            if (answer.AnswerType != AnswerType.Success)
            {
                return(answer);
            }

            var          participantId = viewmodel.Id;
            IParticipant participant   = null;

            if (participantId.StartsWith("ShortTerm"))
            {
                participant = Session.Load <ShortTermParticipant>(participantId);
            }

            if (participantId.StartsWith("LongTerm"))
            {
                participant = Session.Load <LongTermParticipant>(participantId);
            }

            if (participant != null)
            {
                if (participant is LongTermParticipant longTerm)
                {
                    longTerm.IsAllowingPromotions = viewmodel.IsAllowingPromotions;
                }

                participant.FullName    = viewmodel.FullName;
                participant.PhoneNumber = viewmodel.PhoneNumber ?? string.Empty;
                participant.YearOfBirth = viewmodel.YearOfBirth;

                Session.Store(participant, participant.Id);
                Session.SaveChanges();

                return(await Task.FromResult(Answer.Success));
            }
            else
            {
                return(await Task.FromResult(Answer.Error("Can't find participant")));
            }
        }
        public async Task <Answer> CreateParticipant(ParticipantCreateUpdateViewModel viewmodel)
        {
            var answer = ValidateParticipantFields(viewmodel);

            if (answer.AnswerType != AnswerType.Success)
            {
                return(answer);
            }

            IParticipant         model;
            ConventionEngagement engagement = new ConventionEngagement
            {
                ConventionId        = Actor.ManagedConvention.ConventionId,
                ConventionStartDate = Actor.ManagedConvention.Days
                                      .Min(x => x.Date)
                                      .ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
                Payment = new PaymentInvoice()
            };

            if (viewmodel.Email.IsEmptyString())
            {
                model = new ShortTermParticipant
                {
                    CreatedById = Actor.Me.Id
                };
            }
            else
            {
                model = new LongTermParticipant
                {
                    UserName             = viewmodel.Email,
                    Email                = viewmodel.Email,
                    IsAllowingPromotions = viewmodel.IsAllowingPromotions,
                };
                engagement.IsLongTerm = true;
            }

            model.FullName    = viewmodel.FullName;
            model.PhoneNumber = viewmodel.PhoneNumber ?? string.Empty;
            model.YearOfBirth = viewmodel.YearOfBirth;

            var result = await Identities.AddNewParticipant(model);

            engagement.ParticipantId = model.Id;
            if (result.IsSuccess)
            {
                Session.Store(engagement);
                Session.SaveChanges();
            }

            if (result.IsSuccess && result.IsLongTerm)
            {
                await Hub.SendCreationPasswordAsync(model as LongTermParticipant, result.Token);
            }

            if (result.IsSuccess == false)
            {
                return(Answer.Error(result.Errors?.AsJson()));
            }

            return(Answer.Success);
        }