public async Task <ActionResult> Put(int id, [FromBody] PollDefinition pollDefinition)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (pollDefinition.Id != id)
            {
                return(BadRequest("Body(id) does not match this poll id "));
            }

            try {
                await _pollDefinitionRepository.UpdatePollDefinitionAsync(pollDefinition);

                _logger.LogInformation(LoggingEvents.UpdatePollDefinition, $"Poll Definition {id} Updated", pollDefinition.Id);

                // return new NoContentResult();
                return(new NoContentResult());
            }
            catch (Exception e) {
                _logger.LogError(LoggingEvents.UpdatePollDefinition, $"Error updating poll definition {id}: Exception {e.Message}");
                return(StatusCode(500, e.Message));
            }
        }
Пример #2
0
        public async Task <string> SavePollAsync(PollDefinition poll)
        {
            if (string.IsNullOrEmpty(poll.Id))
            {
                poll.Id = Guid.NewGuid().ToString();

                var topic = (await this._snsClient.CreateTopicAsync("pollster-poll-" + poll.Id)).TopicArn;
                poll.TopicArn = topic;
                await this._dbContext.SaveAsync <PollDefinition>(poll);

                await this._snsClient.SubscribeAsync(topic, "email", poll.AuthorEmail);
            }
            await this._dbContext.SaveAsync(poll);

            await this._swfClient.StartWorkflowExecutionAsync(new StartWorkflowExecutionRequest
            {
                Domain       = Constants.SWF_DOMAIN,
                WorkflowId   = poll.Id,
                WorkflowType = new WorkflowType()
                {
                    Name    = Constants.SWF_WORKFLOW_TYPE_NAME,
                    Version = _swfWorkflowTypeVersion,
                },
                TaskList = new TaskList
                {
                    Name = Constants.SWF_DECIDER_TASKLIST
                },
                Input = poll.Id
            });

            return(poll.Id);
        }
        /// <summary>
        /// Create a new poll definition
        /// </summary>
        /// <param name="pollDefinition"></param>
        /// <returns></returns>
        public async Task <PollDefinition> AddPollDefinitionAsync(PollDefinition pollDefinition)
        {
            await _context.PollDefinitions
            .AddAsync(pollDefinition);

            await _context.SaveChangesAsync();

            return(pollDefinition);
        }
Пример #4
0
        public async Task Save(PollDefinition poll)
        {
            if (string.IsNullOrEmpty(poll.Id))
            {
                var response = await base.Post <string>("api/Polls", poll);

                Logger.LogMessage("Response from saving poll: {0}", response);
            }
            else
            {
                var response = await base.Put <string>("api/Polls/" + poll.Id, poll);

                Logger.LogMessage("Response from saving poll: {0}", response);
            }
        }
Пример #5
0
        public async Task Put(string id, [FromBody] PollDefinition value)
        {
            try
            {
                value.Id = id;
                await PollWriterManager.Instance.SavePollAsync(value);

                Logger.LogMessage("Saved existing poll saved: {0}", id);
            }
            catch (Exception e)
            {
                Logger.LogMessage("Unknown saving existing poll: {0}", Utilities.FormatInnerException(e));
                throw;
            }
        }
        public async Task <IActionResult> Index(PollCreatorViewModel model)
        {
            PollConfirmationViewModel confirmModel = new PollConfirmationViewModel();

            try
            {
                var pollDefinition = new PollDefinition
                {
                    Title       = model.Title,
                    Question    = model.Question,
                    AuthorEmail = model.AuthorEmail,
                    Options     = new Dictionary <string, PollDefinition.Option>()
                };
                pollDefinition.StartTime = model.StartDate.Date.Add(model.StartTime.TimeOfDay);
                pollDefinition.EndTime   = model.EndDate.Date.Add(model.EndTime.TimeOfDay);

                string line;
                using (var reader = new StringReader(model.Options))
                {
                    int index = 0;
                    while ((line = reader.ReadLine()) != null)
                    {
                        pollDefinition.Options.Add(index.ToString(), new PollDefinition.Option {
                            Text = line
                        });
                        index++;
                    }
                }

                await this._pollWriter.Save(pollDefinition);

                confirmModel.Title     = pollDefinition.Title;
                confirmModel.StartTime = pollDefinition.StartTime;
                confirmModel.EndTime   = pollDefinition.EndTime;
                confirmModel.Success   = true;
            }
            catch (Exception e)
            {
                confirmModel.Success      = false;
                confirmModel.ErrorMessage = string.Format("Unknown error saving poll: {0}", e.Message);
            }

            return(View("Confirmation", confirmModel));
        }
        public async Task <ActionResult> Post([FromBody] PollDefinition pollDefinition)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                await _pollDefinitionRepository.AddPollDefinitionAsync(pollDefinition);;

                _logger.LogInformation(LoggingEvents.InsertPollDefinition, $"Poll Definition {pollDefinition.Id} Created");
                return(CreatedAtRoute("GetPollDefinition", new { id = pollDefinition.Id }, pollDefinition));
            }
            catch (Exception e)
            {
                _logger.LogError(LoggingEvents.InsertPollDefinition, $"Error creating new poll definition: Exception {e.Message}");
                return(StatusCode(500, e.Message));
            }
        }
        /// <summary>
        /// Update a poll definition
        /// </summary>
        /// <param name="pollDefinition"></param>
        /// <returns></returns>
        public async Task <PollDefinition> UpdatePollDefinitionAsync(PollDefinition pollDefinition)
        {
            _context.PollDefinitions.Update(pollDefinition);

            try
            {
                await _context.SaveChangesAsync();

                return(pollDefinition);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PollDefinitionExists(pollDefinition.Id))
                {
                    throw new PollDefNotFoundException();
                }
                else
                {
                    throw;
                }
            }
        }
Пример #9
0
        public async Task <IActionResult> Post([FromBody] PollDefinition poll)
        {
            try
            {
                if (!this.ModelState.IsValid)
                {
                    return(BadRequest(this.ModelState));
                }

                if (string.IsNullOrEmpty(poll.Id))
                {
                    poll.Id = Guid.NewGuid().ToString();
                }

                if (poll.StartTime < DateTime.Now)
                {
                    poll.StartTime = DateTime.Now;
                }

                if (poll.EndTime < DateTime.Now)
                {
                    return(BadRequest("End time for the poll is in the past"));
                }
                if (poll.EndTime <= poll.StartTime)
                {
                    return(BadRequest("End time is before start time"));
                }

                await this._manager.SavePollAsync(poll);

                return(Ok(poll));
            }
            catch (Exception e)
            {
                _logger.LogError($"Unknown error getting feed of polls: {e}");
            }

            return(BadRequest("Failed to get current feed of polls"));
        }