private bool CheckInputLearnRequest(EDCLearnRequestBindingModel model) { if (model == null) { return(false); } var times = model.LearnRequests; if (times == null || times.Count == 0) { return(false); } foreach (var t in times) { if (t == null) { return(false); } var date = t.Date; var timesTemp = t.Times; if (date == null || timesTemp == null) { return(false); } if (date.Length == 0 || timesTemp.Count == 0) { return(false); } foreach (var k in timesTemp) { if (k == null || k.Length == 0) { return(false); } } } return(true); }
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()); }