示例#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));
        }
示例#2
0
文件: Poll.cs 项目: Kuinox/ITI-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());
        }
示例#3
0
文件: Poll.cs 项目: Kuinox/ITI-Poll
 internal void OnDelete()
 {
     Poll = null;
     Vote = null;
 }
示例#4
0
文件: Poll.cs 项目: Kuinox/ITI-Poll
 internal void Answer(Proposal proposal)
 {
     Vote = proposal;
     proposal.OnChoose(this);
 }
示例#5
0
文件: Poll.cs 项目: Kuinox/ITI-Poll
 internal static Guest Create(int userId, Poll poll, Proposal vote)
 => new Guest(userId, poll.PollId)
 {
     Poll = poll, Vote = vote
 };