public async Task <BotScenarioViewModel> GetDetailScenario(int id) { try { BotScenarioViewModel botScenarioViewModel = new BotScenarioViewModel(); botScenarioViewModel.BotScenario = this._context.BotScenarios.Single(sc => sc.SCENARIO_ID == id); botScenarioViewModel.BotQuestions = this._context.BotQuestions.Where(q => q.SCENARIO_ID == id).ToList(); botScenarioViewModel.BotAnswers = this._context.BotAnswers.Where(a => botScenarioViewModel.BotQuestions.Any(q => q.QUESTION_ID == a.QUESTION_ID)).ToList(); return(botScenarioViewModel); } catch (Exception ex) { return(new BotScenarioViewModel()); } }
//[Authorize(Roles ="UpdatePlugin")] public async Task <ObjectResult> Put(int id, [FromBody] BotScenarioViewModel botScenarioViewModel) { GenericResult rs = new GenericResult(); //DEFACEWEBSITEContext context = new DEFACEWEBSITEContext(); try { this._context.BotScenarios.Update(botScenarioViewModel.BotScenario); var deleteQuestion = this._context.BotQuestions.Where(question => question.SCENARIO_ID == id).ToList(); this._context.BotAnswers.RemoveRange(this._context.BotAnswers.Where(ans => deleteQuestion.Any(qu => qu.QUESTION_ID == ans.QUESTION_ID))); this._context.BotQuestions.RemoveRange(this._context.BotQuestions.Where(question => question.SCENARIO_ID == id)); int countQuestion = botScenarioViewModel.BotQuestions.Count; for (int i = 0; i < countQuestion; i++) { botScenarioViewModel.BotQuestions[i].QUESTION_ID = 0; botScenarioViewModel.BotQuestions[i].SCENARIO_ID = botScenarioViewModel.BotScenario.SCENARIO_ID; botScenarioViewModel.BotQuestions[i].LEVEL = 1; botScenarioViewModel.BotQuestions[i].RECORD_STATUS = 1; this._context.BotQuestions.Add(botScenarioViewModel.BotQuestions[i]); this._context.SaveChanges(); //if (botScenarioViewModel.BotQuestions[i].QUESTION_TYPE != 1) //{ botScenarioViewModel.BotAnswers[i].ANSWER_ID = 0; botScenarioViewModel.BotAnswers[i].QUESTION_ID = botScenarioViewModel.BotQuestions[i].QUESTION_ID; botScenarioViewModel.BotAnswers[i].LEVEL = 1; botScenarioViewModel.BotAnswers[i].IS_END = true; botScenarioViewModel.BotAnswers[i].RECORD_STATUS = 1; this._context.BotAnswers.Add(botScenarioViewModel.BotAnswers[i]); this._context.SaveChanges(); //} } rs.Succeeded = true; rs.Message = "Cập nhật domain thành công"; ObjectResult objRes = new ObjectResult(rs); //context.Dispose(); return(objRes); } catch (Exception ex) { rs.Succeeded = false; rs.Message = ex.Message; ObjectResult objRes = new ObjectResult(rs); //context.Dispose(); return(objRes); } }
public async Task <ObjectResult> Post([FromBody] BotScenarioViewModel botScenarioViewModel) { GenericResult rs = new GenericResult(); try { botScenarioViewModel.BotScenario.RECORD_STATUS = 1; this._context.BotScenarios.Add(botScenarioViewModel.BotScenario); this._context.SaveChanges(); int countQuestion = botScenarioViewModel.BotQuestions.Count; for (int i = 0; i < countQuestion; i++) { botScenarioViewModel.BotQuestions[i].SCENARIO_ID = botScenarioViewModel.BotScenario.SCENARIO_ID; botScenarioViewModel.BotQuestions[i].LEVEL = 1; botScenarioViewModel.BotQuestions[i].IS_END = false; botScenarioViewModel.BotQuestions[i].RECORD_STATUS = 1; this._context.BotQuestions.Add(botScenarioViewModel.BotQuestions[i]); this._context.SaveChanges(); //if (botScenarioViewModel.BotQuestions[i].QUESTION_TYPE != 1) //{ botScenarioViewModel.BotAnswers[i].QUESTION_ID = botScenarioViewModel.BotQuestions[i].QUESTION_ID; botScenarioViewModel.BotAnswers[i].LEVEL = 1; botScenarioViewModel.BotAnswers[i].IS_END = true; botScenarioViewModel.BotAnswers[i].RECORD_STATUS = 1; this._context.BotAnswers.Add(botScenarioViewModel.BotAnswers[i]); this._context.SaveChanges(); //} } rs.Succeeded = true; rs.Message = "Thêm kịch bản chat thành công"; ObjectResult objRes = new ObjectResult(rs); //context.Dispose(); return(objRes); } catch (Exception ex) { rs.Message = "Lỗi " + ex.Message; rs.Succeeded = false; ObjectResult objRes = new ObjectResult(rs); //context.Dispose(); return(objRes); } }