public IHttpActionResult AddFile() { int AdminId; HttpPostedFile CSV; try { AdminId = Convert.ToInt32(HttpContext.Current.Request.Form.Get("id")); CSV = HttpContext.Current.Request.Files[0]; } catch { return(BadRequest("No file found")); } QuestionFile file = new QuestionFile(); file.SetProperties(AdminId: AdminId, FileName: CSV.FileName); db.QuestionFiles.Add(file); db.SaveChanges(); bool headersRow = true; CSVQuestion temp; using (CSVReader reader = new CSVReader(CSV.InputStream)) { CSVQuestion row = new CSVQuestion(); while (reader.ReadRow(row)) { if (headersRow) { headersRow = false; continue; } temp = new CSVQuestion(); temp.AddRange(row); Question question = new Question(); if (question.SetProperties(file.Id, temp)) { try { db.Questions.Add(question); db.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } } } return(CreatedAtRoute("DefaultApi", new { id = file.Id }, file)); }
public IHttpActionResult DeleteFile(int AdminId, int FileId) { QuestionFile file = db.QuestionFiles.Find(FileId); if (file == null || !file.IsCurrent) { return(NotFound()); } file.SetProperties(AdminId: AdminId, IsCurrent: false); db.SaveChanges(); return(Ok(file)); }
public virtual async Task <HttpResponseMessage> Post([FromBody] Activity activity) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); Activity reply = null; List <CardAction> cards = new List <CardAction>(); try { this.HandleSystemMessage(activity); // 設定語系的快取 if (activity.Text == "English" || activity.Text == "繁體中文") { memoryCache.Set(activity.Conversation.Id + "_LANG", activity.Text, DateTimeOffset.UtcNow.AddDays(1)); } // 判斷是否為直接連絡真人的清單 var strIsConnect = memoryCache.Get(activity.Conversation.Id); // 如果不在真人聯絡清單中,就找出問題的內容 if (strIsConnect == null) { if (activity.GetActivityType() == ActivityTypes.Message) { QuestionObj Question = new QuestionObj(); // 找出是否有答案 QuestionFile answer = Question.GetQuestionAnswer(activity.Text); if (answer == null) { // 找不到問題,也找不到答案,後送QnA // await Conversation.SendAsync(activity, () => new BasicQnAMakerDialog()); string strAnswer = QnAMaker.GetAnswer(activity.Text); reply = this.ReplyOptions(activity, strAnswer, Question.GetQuestionCards(0)); } else { // 如果是回到主選單,就取出根目錄的內容 if (activity.Text == "<-回主選單" || activity.Text == "<-Back to Menu") { cards = Question.GetQuestionCards(0); } else { cards = Question.GetNextQuestionCards(activity.Text); } // 找到問題,判斷是否有下一層,有下一層就產生選單 if (cards.Count > 0) { reply = this.ReplyOptions(activity, null, cards); } else { // 沒有下一層的話,就判斷回覆的內容是否往QnA送,並顯示相同層級的問題清單 // 一般問題就直接回覆 if (answer.AnswerType == "QnA") { await Conversation.SendAsync(activity, () => new BasicQnAMakerDialog()); // 我想聯絡小編就不發回覆 if (activity.Text != "我想連絡小編" || activity.Text != "I want contact editor") { reply = this.ReplyOptions(activity, null, Question.GetSameLevelQuestion(activity.Text)); } } else { reply = this.ReplyOptions(activity, answer.Answer, Question.GetSameLevelQuestion(activity.Text)); } } } } await connector.Conversations.ReplyToActivityAsync(reply); } } catch (Exception e) { reply = this.ReplyOptions(activity, e.Message, null); await connector.Conversations.ReplyToActivityAsync(reply); } return(new HttpResponseMessage(System.Net.HttpStatusCode.Accepted)); }