Result Validate(NewPollDto newPollInfo, string authorNickname) { if (newPollInfo.AuthorId < 16) { return(Result.CreateError(Errors.InvalidAuthorId, InvalidAuthorIdMsg)); } if (newPollInfo.GuestNicknames == null || newPollInfo.GuestNicknames.Length < 1) { return(Result.CreateError(Errors.NotEnoughGuests, NotEnoughGuestsMsg)); } if (newPollInfo.GuestNicknames.Select(n => n.ToLowerInvariant()).Contains(authorNickname.ToLowerInvariant())) { return(Result.CreateError(Errors.AuthorCannotBeGuest, AuthorCannotBeGuestMsg)); } if (string.IsNullOrWhiteSpace(newPollInfo.Question)) { return(Result.CreateError(Errors.EmptyQuestion, EmptyQuestionMsg)); } if (newPollInfo.Proposals == null || newPollInfo.Proposals.Length < 2) { return(Result.CreateError(Errors.NotEnoughProposals, NotEnoughProposalsMsg)); } if (newPollInfo.Proposals.Any(p => string.IsNullOrWhiteSpace(p))) { return(Result.CreateError(Errors.EmptyProposal, EmptyProposalMsg)); } return(Result.CreateSuccess()); }
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)); }