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> 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> 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()); }