Exemplo n.º 1
0
        public async Task <Result <Poll> > CreatePoll(IUnitOfWork unitOfWork, IPollRepository pollRepository, IUserRepository userRepository, NewPollDto newPollInfo)
        {
            Result <User> author = await userRepository.FindById(newPollInfo.AuthorId);

            if (!author.IsSuccess)
            {
                return(Result.Map <Poll>(author));
            }

            Result validation = Validate(newPollInfo, author.Value.Nickname);

            if (!validation.IsSuccess)
            {
                return(Result.Map <Poll>(validation));
            }

            Poll   poll         = new Poll(0, newPollInfo.AuthorId, newPollInfo.Question, false);
            Result pollCreation = await pollRepository.Create(poll);

            if (!pollCreation.IsSuccess)
            {
                return(Result.Map <Poll>(pollCreation));
            }

            foreach (string proposalText in newPollInfo.Proposals)
            {
                poll.AddProposal(proposalText);
            }
            Proposal noProposal = await pollRepository.GetNoProposal();

            foreach (string guestNickname in newPollInfo.GuestNicknames)
            {
                Result <User> guest = await userRepository.FindByNickname(guestNickname);

                if (!guest.IsSuccess)
                {
                    return(Result.Map <Poll>(guest));
                }
                poll.AddGuest(guest.Value.UserId, noProposal);
            }

            await unitOfWork.SaveChanges();

            return(Result.CreateSuccess(poll));
        }
Exemplo n.º 2
0
        public Result Answer(int guestId, int proposalId)
        {
            Guest guest = _guests.SingleOrDefault(g => g.UserId == guestId);

            if (guest == null)
            {
                return(Result.CreateError(Errors.UnknownGuest, UnknownGuestMsg));
            }
            if (guest.Vote.ProposalId != 0)
            {
                return(Result.CreateError(Errors.AlreadyAnswered, AlreadyAnsweredMsg));
            }

            Proposal proposal = _proposals.SingleOrDefault(p => p.ProposalId == proposalId);

            if (proposal == null)
            {
                return(Result.CreateError(Errors.UnknownProposal, UnknownProposalMsg));
            }

            guest.Answer(proposal);
            return(Result.CreateSuccess());
        }
Exemplo n.º 3
0
 internal void OnDelete()
 {
     Poll = null;
     Vote = null;
 }
Exemplo n.º 4
0
 internal void Answer(Proposal proposal)
 {
     Vote = proposal;
     proposal.OnChoose(this);
 }
Exemplo n.º 5
0
 internal static Guest Create(int userId, Poll poll, Proposal vote)
 => new Guest(userId, poll.PollId)
 {
     Poll = poll, Vote = vote
 };