Esempio n. 1
0
        public async Task create_poll()
        {
            IUnitOfWork unitOfWork = Substitute.For <IUnitOfWork>();

            IPollRepository pollRepository = Substitute.For <IPollRepository>();

            pollRepository.Create(Arg.Any <Poll>()).Returns(Task.FromResult(Result.CreateSuccess()));

            IUserRepository userRepository = Substitute.For <IUserRepository>();

            userRepository.Create(Arg.Any <User>()).Returns(Task.FromResult(Result.CreateSuccess()));
            User user = new User(1234, "*****@*****.**", "toto", "passwordhash", false);

            userRepository.FindById(Arg.Any <int>()).Returns(Task.FromResult(Result.CreateSuccess(user)));
            userRepository.FindByNickname(Arg.Any <string>()).Returns(Task.FromResult(Result.CreateSuccess(user)));

            NewPollDto newPollDto = new NewPollDto();

            newPollDto.AuthorId       = 1234;
            newPollDto.Question       = "Question?";
            newPollDto.GuestNicknames = new string[] { "Guest1" };
            newPollDto.Proposals      = new string[] { "P1", "P2" };
            PollService sut = new PollService();

            Result <Poll> poll = await sut.CreatePoll(unitOfWork, pollRepository, userRepository, newPollDto);

            poll.IsSuccess.Should().BeTrue();
        }
Esempio n. 2
0
        public int CreatePoll(string name, string Desc, int userOwnerId, DateTime start, DateTime end,
                              bool multipleSelection)
        {
            var poll = new Poll()
            {
                Name              = name,
                Description       = Desc,
                PollOwnerUserId   = userOwnerId,
                PollStartDate     = start,
                PollEndDate       = end,
                MutlipleSelection = multipleSelection,
                Choices           = new List <Choice>()
            };

            return(_pollRepos.Create(poll));
        }
Esempio n. 3
0
        public void CreatePoll(PollCreationDTO pollCreation)
        {
            var poll = new Poll()
            {
                Name              = pollCreation.Name,
                Description       = pollCreation.Description,
                PollOwnerUserId   = pollCreation.OwnerId,
                PollStartDate     = pollCreation.LeftDateTime,
                PollEndDate       = pollCreation.RightDateTime,
                MutlipleSelection = pollCreation.MultipleSelection,
                Choices           = new List <Choice>()
            };

            _pollRepos.Create(poll);
            _managePolicy.GiveAdminPolicyToUser(pollCreation.OwnerId, poll.Id);
        }
Esempio n. 4
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));
        }
Esempio n. 5
0
        public async Task <ICommandResult> Handle(CreatePollCommand command)
        {
            try
            {
                // fail fast validation
                command.Validate();
                if (command.Invalid)
                {
                    return(new GenericCommandResult(true, "Enquete inválida", command.Notifications));
                }

                DescriptionVO description = new DescriptionVO(command.Poll_Description);
                Poll          poll        = new Poll(description);

                List <OptionPoll> opt = new List <OptionPoll>();
                foreach (var item in command.Options)
                {
                    DescriptionVO vo     = new DescriptionVO(item);
                    OptionPoll    option = new OptionPoll(vo);
                    poll.addOptions(option);
                }

                await _pollRepository.Create(poll);

                if (Commit())
                {
                    return(new GenericCommandResult(true, "Enquete gravada com sucesso", new CreatePollCommandResult(poll.Id)));
                }
                else
                {
                    return(new GenericCommandResult(false, "Falha ao gravar enquete", null));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("CreatePollCommand --> ", ex);
                return(new GenericCommandResult(false, "Falha ao gravar enquete", null));
            }
        }