public ActionResult SavePoll(PollWrapper configPoll, List<PollUnit> poll, Boolean? hasUser) { try { if (Session["user"] != null) { configPoll.UserId = ((dynamic)Session["user"]).Id; } var project= ConstructorHelper.Save(configPoll, poll); return new JsonResult() { Data =new {project.UrlCode } }; } catch (Exception ex) { Response.StatusCode = 400; return new JsonResult() { Data = null }; } }
/// <summary> /// Save new project and return Project data /// </summary> public static Project Save(PollWrapper configPoll, List<PollUnit> poll) { using (var Db = new Repository()) { Db.Context.Configuration.ProxyCreationEnabled = false; using (var dbContextTransaction = Db.Context.Database.BeginTransaction()) { try { if (configPoll == null) throw new Exception("Poll null"); var pollShare = Db.Context.PollShares.Where(x => x.Value == configPoll.PollShare).Select(x => x.Id).First(); var pollAccess = Db.Context.PollAccesses.Where(x => x.Value == configPoll.PollAccess).Select(x => x.Id).First(); var pollTypes = Db.Context.PollTypes.Where(x => x.Value == configPoll.PollType).Select(x => x.Id).First(); Project newProj = new Project(); newProj.UserId = configPoll.UserId; newProj.Name = configPoll.PollName; newProj.ShareId = pollShare; newProj.AccessId = pollAccess; newProj.TypeId = pollTypes; newProj.CreatedOn = DateTime.Now; newProj.ModifiedOn= DateTime.Now; newProj.IsActive = true; newProj.UrlCode = GenerateProjectCode(); if (pollTypes == (Int32)DbEnum.PollType.Quiz) { newProj.QuizConfigurator = configPoll.QuizConfigurator; } Db.Add(newProj); Db.Save(); if (poll != null) { foreach (var el in poll) { var questionType = Db.Context.QuestionTypes.Where(x => x.Value == el.QuestionType).Select(x => x.Id).First(); el.Question.ProjectId = newProj.Id; el.Question.QuestionTypeId = questionType; Db.Add(el.Question); Db.Save(); int order = 1; List<Answer> answList = new List<Answer>(); foreach (var answerString in el.Answers) { var answer = new Answer(); answer.QuestionId = el.Question.Id; answer.OrderValue = order; answer.Value = answerString; order++; answList.Add(answer); } Db.AddRange(answList); Db.Save(); } } dbContextTransaction.Commit(); SetAccess(configPoll, newProj.Id); return newProj; } catch (Exception ex) { dbContextTransaction.Rollback(); return null; } } } }
public static void SetAccess(PollWrapper poll,int projectId) { var id = poll.PollAccess; using (var Db = new Repository()) { var access = Db.Context.PollAccesses.Where(x => x.Id == id).Select(x => x.Value).FirstOrDefault(); if(access== (Int32)DbEnum.PollAccess.CodeSet) SetCodeArray(poll.CodeSet, projectId); else if (access == (Int32)DbEnum.PollAccess.UserSet) SetUserArray(poll.UserSet, projectId); } }