public bool CreatePoll(CreatePollModel poll) { bool wasAdded = false; try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(sqlCreatePoll, conn); cmd.Parameters.AddWithValue("@username", poll.Username); cmd.Parameters.AddWithValue("@favoriteBook", poll.FavoriteBook); cmd.Parameters.AddWithValue("@favoriteAuthors", poll.FavoriteAuthors); cmd.Parameters.AddWithValue("@weekOf", poll.WeekOf); if (cmd.ExecuteNonQuery() > 0) { wasAdded = true; } } } catch (Exception e) { throw e; } return(wasAdded); }
public ActionResult Poll(CreatePollModel poll) { if (base.IsAuthenticated) { poll.Username = CurrentUser; pollDAO.CreatePoll(poll); return(RedirectToAction("Poll", "Poll", new { username = base.CurrentUser })); } var model = new LoginUser(); return(RedirectToAction("Login", "User", model)); }
public async Task <IActionResult> CreatePollAsync([FromBody] CreatePollModel createPoll) { if (ModelState.IsValid) { Concern concern = db.Concern.Where(c => c.Id == createPoll.ConcernId).SingleOrDefault(); int concernStatusId = concern.StatusId; concern.StatusId = 5; db.Update(concern); int userId = (await userManager.GetUserAsync(HttpContext.User)).Id; List <int> answerOptionId = new List <int>(); Poll poll = new Poll { Text = createPoll.Text, Title = createPoll.Title, End = createPoll.End, NeedsLocalCouncil = createPoll.NeedsLocalCouncil, LastUpdatedBy = userId, LastUpdatedAt = DateTime.UtcNow, UserId = userId, StatusId = 2, CategoryId = createPoll.CategoryId }; foreach (string answer in createPoll.Answers) { AnswerOptions answerOption = null; answerOption = db.AnswerOptions.Where(ao => ao.Description.Equals(answer)).SingleOrDefault(); if (answerOption != null) { answerOptionId.Add(answerOption.Id); } else { answerOption = new AnswerOptions { Description = answer }; db.Add(answerOption); //db.SaveChanges(); answerOptionId.Add(answerOption.Id); } } if (!poll.NeedsLocalCouncil) { poll.Approved = true; } else { poll.Approved = false; } db.Add(poll); //db.SaveChanges(); foreach (int aoId in answerOptionId) { db.Add(new AnswerOptionsPoll { PollId = poll.Id, AnswerOptionsId = aoId }); //db.SaveChanges(); } int result = db.SaveChanges(); return(Json(new { result, concernStatusId, concernId = createPoll.ConcernId })); } else { return(Json(new { result = 0 })); } }
public async Task <IActionResult> CreatePollAsync([FromBody] CreatePollModel createPoll) { if (ModelState.IsValid) { //Variablen int concernStatusId = 0; int userId = (await userManager.GetUserAsync(HttpContext.User)).Id; List <int> answerOptionId = new List <int>(); Poll poll = new Poll { Text = createPoll.Text, Title = createPoll.Title, End = createPoll.End, NeedsLocalCouncil = createPoll.NeedsLocalCouncil, LastUpdatedBy = userId, LastUpdatedAt = DateTime.UtcNow, UserId = userId, StatusId = 2, CategoryId = createPoll.CategoryId }; //Nur, wenn Umfrage aus Anliegen erstellt wird, wird das Anliegen auf Status "abgeschlossen" gesetzt. if (createPoll.ConcernId != 0) { Concern concern = db.Concern.Where(c => c.Id == createPoll.ConcernId).SingleOrDefault(); concernStatusId = 6; //concern.StatusId; concern.StatusId = concernStatusId; db.Update(concern); } //Antworten aus Objekt auslesen und in Datenbank speichern foreach (string answer in createPoll.Answers) { AnswerOptions answerOption = null; //Prüfung, ob Antwort schon einmal verwendet wurde answerOption = db.AnswerOptions.Where(ao => ao.Description.Equals(answer)).SingleOrDefault(); if (answerOption != null) { answerOptionId.Add(answerOption.Id); } else { answerOption = new AnswerOptions { Description = answer }; db.Add(answerOption); answerOptionId.Add(answerOption.Id); } } if (!poll.NeedsLocalCouncil) { poll.Approved = true; } else { poll.Approved = false; } //Umfrage Speichern db.Add(poll); //Antworten Speichern foreach (int aoId in answerOptionId) { db.Add(new AnswerOptionsPoll { PollId = poll.Id, AnswerOptionsId = aoId }); } //Dokumente aus dem Anliegen in die Umfrage übernehmen if (createPoll.FileIds != null) { foreach (int fileId in createPoll.FileIds) { File file = db.File.Where(f => f.Id == fileId).SingleOrDefault(); file.PollId = poll.Id; db.Update(file); } } //Bilder aus dem Anliegen in die Umfrage übernehmen if (createPoll.ImageIds != null) { foreach (int imageId in createPoll.ImageIds) { Image image = db.Image.Where(i => i.Id == imageId).SingleOrDefault(); image.PollId = poll.Id; db.Update(image); } } //Commit int result = db.SaveChanges(); return(Json(new { result, concernStatusId, concernId = createPoll.ConcernId })); } return(Json(new { result = 0 })); }
/// <summary> /// Creates a poll. /// </summary> /// <param name="poll">The poll to create</param> /// <returns>The created poll</returns> public async Task <PollModel> CreatePoll(CreatePollModel poll) { Validator.ValidateVariable(poll, "poll"); return((await this.PostDataResultAsync <PollModel>("polls", AdvancedHttpClient.CreateContentFromObject(poll)))?.FirstOrDefault()); }