public async Task <IHttpActionResult> DeleteLearnRequest(int id) { var learnRequest = await db.LearnRequests.FindAsync(id); if (learnRequest == null) { var message = "Can't find the learn request."; var modelError = EDCExceptionFactory.GenerateHttpError(message, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } try { db.LearnRequests.Remove(learnRequest); await db.SaveChangesToDbAsync(); } catch (Exception e) { var msg = e.Message; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, modelError); throw new HttpResponseException(response); } return(Ok()); }
public async Task <IHttpActionResult> PutScenario([FromBody] int id) { var scenario = await db.Scenarios.Include(s => s.Students).Where(p => p.ID == id).SingleOrDefaultAsync(); if (scenario == null) { var msg = String.Format("The scenario can't be find."); var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } var student = await db.Students.FindAsync(User.Identity.Name); var scenarioStudent = scenario.Students.Where(s => s.StudentName == User.Identity.Name).SingleOrDefault(); if (scenarioStudent != null) { return(Ok()); } try { scenario.Students.Add(student); await db.SaveChangesToDbAsync(); } catch (Exception) { var msg = "There is something internal errors while adding the scenario"; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } return(Ok()); }
public async Task <IHttpActionResult> GetTeacher(string id) { var teacher = await db.Teachers.Include(p => p.LearnRequests.Select(x => x.RegisteredStudents)) .SingleOrDefaultAsync(p => p.TeacherName == id); if (teacher == null) { var msg = string.Format("Can't find the teacher {0}", id); var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } var learnRequestDtos = new List <EDCLearnRequestDTO>(); foreach (var i in teacher.LearnRequests) { var dto = db.GenerateDTO(i); learnRequestDtos.Add(dto); } return(Ok(new EDCTeacherDTO() { TeacherName = id, LearnRequests = learnRequestDtos })); }
public async Task <IHttpActionResult> PutWord([FromBody] int id) { var word = await db.Words.Include(w => w.Students) .Where(p => p.ID == id).SingleOrDefaultAsync(); if (word == null) { var msg = String.Format("The word can't be find."); var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } var student = await db.Students.FindAsync(User.Identity.Name); var wordStudent = word.Students.Where(s => s.StudentName == User.Identity.Name).SingleOrDefault(); if (wordStudent != null) { return(Ok()); } try { word.Students.Add(student); await db.SaveChangesToDbAsync(); } catch (Exception e) { var msg = e.Message; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } return(Ok()); }
public async Task <IHttpActionResult> GetScene(string date) { string d; TimeConversionUtils.GetDate(date, out d); if (d == null) { var msg = "The input date is not valid."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } var scene = await db.Scenarios .Include(p => p.Images.Select(x => x.Words)) .Where(p => p.Date == d).SingleOrDefaultAsync(); if (scene != null) { var dto = db.GenerateDTO(scene); if (dto != null) { return(Ok(dto)); } } var message = string.Format("Can't find the scenario for {0}.", d); var error = EDCExceptionFactory.GenerateHttpError(message, EDCWebServiceErrorType.Error, true); var httpResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, error); throw new HttpResponseException(httpResponse); }
public async Task <IHttpActionResult> DeleteLearnRequest(int id) { var learnRequest = await db.LearnRequests.Include(p => p.RegisteredStudents) .Where(i => i.ID == id).SingleOrDefaultAsync(); string message = ""; if (learnRequest == null) { message = "Can't find the learn request."; var modelError = EDCExceptionFactory.GenerateHttpError(message, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } var user = await db.Students.FindAsync(User.Identity.Name); if (user == null) { message = "Can't find the user."; var modelError = EDCExceptionFactory.GenerateHttpError(message, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } learnRequest.RegisteredStudents.Remove(user); if (learnRequest.RegisteredStudents.Count == 0) { db.LearnRequests.Remove(learnRequest); } try { await db.SaveChangesToDbAsync(); } catch (Exception e) { var msg = e.Message; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, modelError); throw new HttpResponseException(response); } return(Ok()); }
public async Task <IHttpActionResult> GetWord(string date) { if (date == null || date.Length == 0) { var msg = "The input date is empty."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } string d; TimeConversionUtils.GetDate(date, out d); if (d == null) { var msg = "The input date is not valid."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); //return BadRequest(); } var word = await db.Words .Include(p => p.Phrases.Select(x => x.Examples)) .Include(p => p.Slangs) .Where(p => p.Date == d).SingleOrDefaultAsync(); if (word != null) { EDCWordDTO wordDto = db.GenerateDTO(word); if (wordDto != null) { return(Ok(wordDto)); } } var message = string.Format("Can't find the word for {0}.", d); var error = EDCExceptionFactory.GenerateHttpError(message, EDCWebServiceErrorType.Error, true); var httpResponse = Request.CreateErrorResponse(HttpStatusCode.NotFound, error); throw new HttpResponseException(httpResponse); }
public async Task <IHttpActionResult> DeleteWord(string name, int id) { //var user = await db.Students.Include(p => p.Words) // .Where(p => p.StudentName == User.Identity.Name).SingleOrDefaultAsync(); var user = await db.Students.Include(p => p.Words) .Where(p => p.StudentName == name).SingleOrDefaultAsync(); if (user == null) { var msg = "The user can't be found."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } var word = await db.Words.FindAsync(id); if (word == null) { var msg = "The requested word can't be found."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } try { user.Words.Remove(word); await db.SaveChangesToDbAsync(); } catch (Exception e) { var msg = e.Message; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, modelError); throw new HttpResponseException(response); } return(Ok(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { var message = "There is something wrong with the input"; var modelError = EDCExceptionFactory.GenerateHttpError(message, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); // return BadRequest(ModelState); } var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if (!result.Succeeded) { return(GetErrorResult(result)); } //add emai confirmation //add email confirmation string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); code = HttpUtility.UrlEncode(code); var callbackUrl = this.Url.Link("DefaultApi", new { Controller = "Account/ConfirmEmail", userId = user.Id, code = code }); await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(Ok(new { email = model.Email, userId = user.Id, code = code })); }
public async Task <IHttpActionResult> GetStudent(string id) { var student = await db.Students.Include(p => p.LearnRequests) .Include(p => p.Scenarios.Select(x => x.Images)) .Include(p => p.Words.Select(x => x.Slangs)) .Include(p => p.Words.Select(x => x.Phrases.Select(t => t.Examples))) .Include(p => p.Scenarios.Select(x => x.Images.Select(t => t.Words))) .Where(x => x.StudentName == id).SingleOrDefaultAsync(); if (student == null) { var msg = "Couldn't find the student."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } try { var learnRequests = student.LearnRequests != null ? new List <EDCLearnRequestDTO>() : null; if (learnRequests != null) { foreach (var l in student.LearnRequests) { var dto = db.GenerateDTO(l); learnRequests.Add(dto); } } var scenarios = student.Scenarios != null ? new List <EDCScenarioContentDTO>() : null; if (scenarios != null) { foreach (var s in student.Scenarios) { var dto = db.GenerateDTO(s); scenarios.Add(dto); } } var words = student.Words != null ? new List <EDCWordDTO>() : null; if (words != null) { foreach (var w in student.Words) { var dto = db.GenerateDTO(w); words.Add(dto); } } EDCStudentDTO studentDto = new EDCStudentDTO() { Name = student.StudentName, LearnRequests = learnRequests, Scenarios = scenarios, Words = words }; return(Ok(studentDto)); } catch (Exception e) { var msg = e.Message; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } }
public async Task <IHttpActionResult> EditLearnRequest(int id, EDCLearnRequestEditBindingModel model) { if (!CheckInputEditLearnRequest(model)) { var msg = "There is something wrong with input."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } var learnRequest = await db.LearnRequests.Include(p => p.RegisteredStudents) .Where(p => p.ID == id).SingleOrDefaultAsync(); if (learnRequest == null) { var msg = "Can't find the learn request."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, modelError); throw new HttpResponseException(response); } //see if the request new time and date is already existed var times = TimeConversionUtils.GetStartAndEndTime(model.Time); var startTime = times[0]; var endTime = times[1]; var newRequest = await db.LearnRequests.Include(p => p.RegisteredStudents).Where(p => p.Date == model.Date && p.StartTime == startTime && p.EndTime == endTime).SingleOrDefaultAsync(); if (newRequest != null) { var already = newRequest.RegisteredStudents.Where(p => p.StudentName == User.Identity.Name).SingleOrDefault(); if (already != null) { var msg = "Your requested new date and time are already existed."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Warning, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } } //need to remove the student from the current request var student = await db.Students.FindAsync(User.Identity.Name); learnRequest.RegisteredStudents.Remove(student); if (newRequest == null) { newRequest = new EDCLearnRequest { Date = model.Date, StartTime = times[0], EndTime = times[1], RegisteredStudents = new List <EDCStudent>() }; newRequest.RegisteredStudents.Add(student); string teacherName = "*****@*****.**"; db.AssignTeacherToLearnRequest(newRequest, teacherName); db.LearnRequests.Add(newRequest); //db.RunCommand(sql, new SqlParameter("@Id",id)); try { // await db.SaveChangesToDbAsync(); db.SaveChangesToDb(); } catch (DbUpdateConcurrencyException ex) { var entry = ex.Entries.Single(); var values = entry.CurrentValues; } } else { newRequest.RegisteredStudents.Add(student); db.SetEntityModified <EDCLearnRequest>(newRequest); // db.RunCommand(sql, new SqlParameter("@Id", id)); try { // await db.SaveChangesToDbAsync(); db.SaveChangesToDb(); } catch (DbUpdateConcurrencyException ex) { var entry = ex.Entries.Single(); var values = entry.CurrentValues; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> PostLearnRequest(string name, EDCLearnRequestBindingModel model) { if (!CheckInputLearnRequest(model)) { var msg = "There is something wrong with the input."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } // var name = User.Identity.Name; var student = await db.Students.Where(p => p.StudentName == name).SingleOrDefaultAsync(); var learnRequestObjs = new List <EDCLearnRequest>(); foreach (var l in model.LearnRequests) { var date = l.Date; string shortDate; TimeConversionUtils.GetDate(date, out shortDate); var dateFromDb = db.LearnRequests.Where(p => p.Date == shortDate).Include(p => p.RegisteredStudents); if (dateFromDb.Count() > 0) { foreach (var t in l.Times) { string[] times = TimeConversionUtils.GetStartAndEndTime(t); bool needBuildNew = true; EDCLearnRequest existed = null; foreach (var temp in dateFromDb) { if (times[0] == temp.StartTime && times[1] == temp.EndTime) { existed = temp; needBuildNew = false; break; } } if (needBuildNew) { var obj = await GenerateLearnRequest(date, times[0], times[1], name); if (obj != null) { learnRequestObjs.Add(obj); } } else { if (existed != null) { existed.RegisteredStudents.Add(student); } } } } else { foreach (var t in l.Times) { string[] timesTemp = TimeConversionUtils.GetStartAndEndTime(t); var obj = await GenerateLearnRequest(date, timesTemp[0], timesTemp[1], name); if (obj != null) { learnRequestObjs.Add(obj); } } } } if (learnRequestObjs.Count > 0) { string teacherName = "*****@*****.**"; var tasks = new List <Task>(); foreach (var l in learnRequestObjs) { db.AssignTeacherToLearnRequest(l, teacherName); } foreach (var l in learnRequestObjs) { db.LearnRequests.Add(l); } } try { await db.SaveChangesToDbAsync(); } catch (Exception) { var msg = "There are some internal errors happened."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, modelError); throw new HttpResponseException(response); } return(Ok()); }
// [ResponseType(typeof(EDCWordDTO))] public async Task <IHttpActionResult> PostWord(AddWordBindingModel word) { if (!ModelState.IsValid) { var msg = "Something wrong with the input while adding the word."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } //to see if the word is already there var wordFromDb = await db.Words.Where(p => p.Character == word.Character).SingleOrDefaultAsync(); if (wordFromDb != null) { return(Ok()); } //see if the date is the same var dateFromDb = await db.Words.Where(p => p.Date == word.Date).SingleOrDefaultAsync(); if (dateFromDb != null) { var msg = String.Format("The date {0} already has the word.", word.Date); var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } var edcWord = new EDCWord() { Audio = word.Audio, Pinyin = word.Pinyin, Character = word.Character, BasicMeanings = word.BasicMeanings, Date = word.Date, Explanation = word.Explanation }; if (word.Phrases != null && word.Phrases.Count() > 0) { var phrases = new List <EDCPhrase>(); var examples = new List <EDCPhraseExample>(); foreach (var p in word.Phrases) { if (p.English == null || p.English.Length == 0 || p.Chinese == null || p.Chinese.Length == 0) { continue; } var phraseExamples = new List <EDCPhraseExample>(); foreach (var e in p.Examples) { phraseExamples.Add(new EDCPhraseExample { Englisgh = e.English, Chinese = e.Chinese }); } phrases.Add(new EDCPhrase { English = p.English, Chinese = p.Chinese, Pinyin = p.Pinyin, Examples = phraseExamples }); examples.AddRange(phraseExamples); } edcWord.Phrases = phrases; foreach (var e in examples) { db.PhraseExamples.Add(e); } foreach (var p in phrases) { db.Phrases.Add(p); } } if (word.Slangs != null && word.Slangs.Count() > 0) { var slangs = new List <EDCSlang>(); foreach (var q in word.Slangs) { if (q.SlangChinese == null || q.SlangChinese.Length == 0 || q.SlangEnglish == null || q.SlangEnglish.Length == 0 || q.SlangExampleChinese == null || q.SlangExampleChinese.Length == 0 || q.SlangExampleEnglish == null || q.SlangExampleEnglish.Length == 0) { continue; } slangs.Add(new EDCSlang { SlangChinese = q.SlangChinese, SlangEnglish = q.SlangEnglish, SlangExampleEnglish = q.SlangExampleEnglish, SlangExampleChinese = q.SlangExampleChinese }); } edcWord.Slangs = slangs; foreach (var q in slangs) { db.Slangs.Add(q); } } db.Words.Add(edcWord); try { await db.SaveChangesToDbAsync(); return(Ok()); } catch (Exception) { var msg = "Something internal errors occurred while adding the word."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, modelError); throw new HttpResponseException(response); } //var wordDto = db.GenerateDTO(edcWord); //return CreatedAtRoute("DefaultApi", new { id = edcWord.ID }, wordDto); }
public async Task <IHttpActionResult> PostScene(AddSceneBindingModel model) { if (!ModelState.IsValid) { var msg = "Something wrong with the input while adding the scenario."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } var dateFromDb = await db.Scenarios.Where(p => p.Date == model.Date).SingleOrDefaultAsync(); if (dateFromDb != null) { var msg = String.Format("The date {0} already has the word.", model.Date); var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, modelError); throw new HttpResponseException(response); } FilterBindingModel(model); var images = new List <EDCScenarioImage>(); if (model.Images != null) { foreach (var i in model.Images) { var words = new List <EDCScenarioWord>(); foreach (var w in i.SceneWords) { var word = new EDCScenarioWord { ChineseWord = w.WordChinese, ChineseWordPinyin = w.WordPinyin, ChineseWordAudio = w.WordAudio }; words.Add(word); db.ScenarioWords.Add(word); } var image = new EDCScenarioImage { Image = i.ImageSrc, Words = words }; images.Add(image); db.ScenarioImages.Add(image); } } var scenario = new EDCScenarioContent { ThemeChinese = model.TitleChinese, ThemeEnglish = model.TitleEnglish, Date = model.Date, Images = images }; db.Scenarios.Add(scenario); try { await db.SaveChangesToDbAsync(); return(Ok()); } catch (Exception) { var msg = "Something internal errors occurred while adding the scenario."; var modelError = EDCExceptionFactory.GenerateHttpError(msg, EDCWebServiceErrorType.Error, true); var response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, modelError); throw new HttpResponseException(response); } }