public async Task <IActionResult> Create([FromBody] PollBindingModel model) { var entity = _mapper.Map <Poll>(model); entity.CreatedBy = UserId; await _repository.AddAsync(entity); return(Ok()); }
public PollBindingModel CreatePollModel(int id) { var eventInfo = GetEventInfo(id); var pollModel = new PollBindingModel { EventId = eventInfo.Id, EventCode = eventInfo.Code, EventTitle = eventInfo.Title }; return(pollModel); }
public async Task <bool> CreatePollAsync(PollBindingModel model, string userId) { var userIsAuthorized = this.db.Events .Any(e => e.Id == model.EventId && e.CreatorId == userId); if (!userIsAuthorized) { return(false); } var pollQuestion = new PollQuestion() { Content = model.PollQuestion }; var pollAnswers = new List <PollAnswer>(); foreach (var answer in model.Answers) { pollAnswers.Add(new PollAnswer() { Content = answer }); } var poll = new Poll() { EventId = model.EventId, PollQuestion = pollQuestion, PollAnswers = pollAnswers, CreatedOn = DateTime.Now, }; this.db.Polls.Add(poll); await this.db.SaveChangesAsync(); return(true); }
public async Task <IActionResult> Create(PollBindingModel model) { if (!this.ModelState.IsValid) { return(View(model)); } if (!this.UserIsManager()) { return(LocalRedirect("/main/home/index")); } var userId = await this.GetCurrentUserId(); var pollCreated = await this.service.CreatePollAsync(model, userId); if (!pollCreated) { return(LocalRedirect("/main/home/index")); } return(RedirectToAction("Index", new { id = model.EventId })); }
public async Task <IActionResult> Update([FromBody] PollBindingModel model) { var entity = await _repository.FindAsyncWithInclude(model.Id, x => x.DataPoints); if (entity == null) { return(BadRequest(new BadRequestResponseModel(ErrorTypes.BadRequest, ErrorMessages.ItemNotFound))); } entity.GraphTypeId = model.GraphTypeId; entity.Name = model.Name; entity.EventId = model.EventId; entity.UpdatedAt = DateTime.Now; entity.UpdatedBy = UserId; entity.DataPoints = entity.DataPoints.ToList(); foreach (var item in model.DataPoints) { var dataPoint = entity.DataPoints.FirstOrDefault(x => x.Id == item.Id); if (dataPoint == null) { dataPoint = _mapper.Map <PollDataPoint>(item); entity.DataPoints.Add(dataPoint); } else { dataPoint.Name = item.Name; dataPoint.Value = item.Value; _dbContext.Entry(dataPoint).State = EntityState.Modified; } } await _repository.UpdateAsync(entity); return(Ok()); }