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)); }
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()); }
internal void OnDelete() { Poll = null; Vote = null; }
internal void Answer(Proposal proposal) { Vote = proposal; proposal.OnChoose(this); }
internal static Guest Create(int userId, Poll poll, Proposal vote) => new Guest(userId, poll.PollId) { Poll = poll, Vote = vote };