public async Task <IActionResult> AddAllOnetoOneCourseInstance() { var result = new Result <dynamic>(); // _lessonGenerateService = new LessonGenerateService(ablemusicContext, mapper); await _lessonGenerateService.GetTerm(DateTime.UtcNow.ToNZTimezone(), 0, 3); if (result.IsSuccess == false) { return(BadRequest(result)); } else { return(Ok(result)); } }
public async Task <IActionResult> AddLearnerGroupCourseController([FromBody] LearnerGroupCourseModel model) { var result = new Result <string>(); try { model.LearnerGroupCourses.ForEach(s => { if (_ablemusicContext.LearnerGroupCourse.FirstOrDefault(w => w.LearnerId == s.LearnerId && w.GroupCourseInstanceId == s.GroupCourseInstanceId) != null) { throw new Exception("Learner has joined this group course"); } var item = new LearnerGroupCourse { LearnerId = s.LearnerId, GroupCourseInstanceId = s.GroupCourseInstanceId, Comment = s.Comment, BeginDate = s.BeginDate, CreatedAt = toNZTimezone(DateTime.UtcNow), IsActivate = 1 }; _ablemusicContext.Add(item); }); await _ablemusicContext.SaveChangesAsync(); model.LearnerGroupCourses.ForEach(async s => { await _lessonGenerateService.GetTerm((DateTime)s.BeginDate, (int)s.GroupCourseInstanceId, 0); }); result.Data = "success"; return(Ok(result)); } catch (Exception ex) { result.IsSuccess = false; result.ErrorMessage = ex.Message; return(BadRequest(result)); } }
public async Task <IActionResult> AddOnetoOneCourseInstance([FromBody] OnetoOneCourseInstancesModel model) { var result = new Result <string>(); try { using (var dbtransaction = await _ablemusicContext.Database.BeginTransactionAsync()) { model.OnetoOneCourses.ForEach(s => { var room = _ablemusicContext.AvailableDays.FirstOrDefault( q => q.TeacherId == s.TeacherId && q.OrgId == s.OrgId && q.DayOfWeek == s.Schedule.DayOfWeek); if (room == null) { throw new Exception("this teacher is not available"); } if (room.RoomId == null) { throw new Exception("Room of this teacher in this branch does not found"); } var durationType = _ablemusicContext.Course.FirstOrDefault(w => w.CourseId == s.CourseId).Duration; _ablemusicContext.Add(new One2oneCourseInstance { CourseId = s.CourseId, TeacherId = s.TeacherId, OrgId = s.OrgId, BeginDate = s.BeginDate, EndDate = s.EndDate, LearnerId = s.LearnerId, RoomId = room.RoomId, CourseSchedule = new List <CourseSchedule>() { new CourseSchedule { DayOfWeek = s.Schedule.DayOfWeek, BeginTime = s.Schedule.BeginTime, EndTime = GetEndTimeForOnetoOneCourseSchedule(s.Schedule.BeginTime, durationType) } } }); }); await _ablemusicContext.SaveChangesAsync(); model.OnetoOneCourses.ForEach(async s => { await _lessonGenerateService.GetTerm((DateTime)s.BeginDate, s.id, 1); }); dbtransaction.Commit(); } result.Data = "success"; return(Ok(result)); } catch (Exception ex) { result.IsSuccess = false; result.ErrorMessage = ex.Message; return(BadRequest(result)); } }
public async Task <IActionResult> StudentRegister([FromForm(Name = "photo")] IFormFile image, [FromForm(Name = "ABRSM")] IFormFile ABRSM, [FromForm(Name = "Form")] IFormFile Form, [FromForm(Name = "Otherfile")] IFormFile Otherfile , [FromForm] string details) { Result <object> result = new Result <object>(); try { using (var dbContextTransaction = _ablemusicContext.Database.BeginTransaction()) { var all_terms = await _ablemusicContext.Term.Select(x => new { x.TermId, x.BeginDate, x.EndDate }).ToListAsync(); var detailsJson = JsonConvert.DeserializeObject <StudentRegister>(details); var newLearner = new Learner(); _mapper.Map(detailsJson, newLearner); newLearner.IsActive = 1; newLearner.CreatedAt = toNZTimezone(DateTime.UtcNow); _ablemusicContext.Add(newLearner); await _ablemusicContext.SaveChangesAsync(); newLearner.LearnerGroupCourse.ToList().ForEach(s => { s.CreatedAt = toNZTimezone(DateTime.UtcNow); s.IsActivate = 1; _ablemusicContext.Update(s); }); await _ablemusicContext.SaveChangesAsync(); //generate new waiting invoice and group lesson newLearner.LearnerGroupCourse.ToList().ForEach(async s => { await _lessonGenerateService.GetTerm((DateTime)s.BeginDate, (int)s.GroupCourseInstanceId, 0); }); if (detailsJson.OneToOneCourseInstance != null) { detailsJson.OneToOneCourseInstance.ForEach(s => { var room = _ablemusicContext.AvailableDays.FirstOrDefault( q => q.TeacherId == s.TeacherId && q.OrgId == s.OrgId && q.DayOfWeek == s.Schedule.DayOfWeek); if (room == null) { throw new Exception("Room does not found"); } var durationType = _ablemusicContext.Course.FirstOrDefault(w => w.CourseId == s.CourseId).Duration; _ablemusicContext.Add(new One2oneCourseInstance { CourseId = s.CourseId, TeacherId = s.TeacherId, OrgId = s.OrgId, BeginDate = s.BeginDate, EndDate = s.EndDate, LearnerId = newLearner.LearnerId, RoomId = room.RoomId, CourseSchedule = new List <CourseSchedule>() { new CourseSchedule { DayOfWeek = s.Schedule.DayOfWeek, BeginTime = s.Schedule.BeginTime, EndTime = GetEndTimeForOnetoOneCourseSchedule(s.Schedule.BeginTime, durationType) } } }); }); } await _ablemusicContext.SaveChangesAsync(); //generate new waiting invoice and one2one lesson newLearner.One2oneCourseInstance.ToList().ForEach(async s => { await _lessonGenerateService.GetTerm((DateTime)s.BeginDate, s.CourseInstanceId, 1); }); //add new fund row for new student var fundItem = new Fund { Balance = 0, LearnerId = newLearner.LearnerId }; _ablemusicContext.Add(fundItem); await _ablemusicContext.SaveChangesAsync(); //add create new user for this learner var newUser = new User { UserName = "******" + newLearner.LearnerId.ToString(), Password = "******", CreatedAt = toNZTimezone(DateTime.UtcNow), RoleId = 4, IsActivate = 1 }; _ablemusicContext.Add(newUser); await _ablemusicContext.SaveChangesAsync(); //keep userid to learner table newLearner.UserId = newUser.UserId; _ablemusicContext.Update(newLearner); await _ablemusicContext.SaveChangesAsync(); //upload file var strDateTime = toNZTimezone(DateTime.UtcNow).ToString("yyMMddhhmmssfff"); if (image != null) { newLearner.Photo = $"images/learner/Photos/{newLearner.LearnerId + strDateTime + Path.GetExtension(image.FileName)}"; _ablemusicContext.Update(newLearner); await _ablemusicContext.SaveChangesAsync(); var uploadResult = UploadFile(image, "learner/Photos/", newLearner.LearnerId, strDateTime); if (!uploadResult.IsUploadSuccess) { throw new Exception(uploadResult.ErrorMessage); } } if (ABRSM != null) { newLearner.G5Certification = $"images/learner/ABRSM_Grade5_Certificate/{newLearner.LearnerId + strDateTime + Path.GetExtension(ABRSM.FileName)}"; newLearner.IsAbrsmG5 = 1; _ablemusicContext.Update(newLearner); await _ablemusicContext.SaveChangesAsync(); var uploadResult = UploadFile(ABRSM, "learner/ABRSM_Grade5_Certificate/", newLearner.LearnerId, strDateTime); if (!uploadResult.IsUploadSuccess) { throw new Exception(uploadResult.ErrorMessage); } } if (Form != null) { newLearner.FormUrl = $"images/learner/Form/{newLearner.LearnerId + strDateTime + Path.GetExtension(Form.FileName)}"; _ablemusicContext.Update(newLearner); await _ablemusicContext.SaveChangesAsync(); var uploadResult = UploadFile(Form, "learner/Form", newLearner.LearnerId, strDateTime); if (!uploadResult.IsUploadSuccess) { throw new Exception(uploadResult.ErrorMessage); } } if (Otherfile != null) { newLearner.OtherfileUrl = $"images/learner/Otherfile/{newLearner.LearnerId + strDateTime + Path.GetExtension(Otherfile.FileName)}"; _ablemusicContext.Update(newLearner); await _ablemusicContext.SaveChangesAsync(); var uploadResult = UploadFile(Otherfile, "learner/Otherfile", newLearner.LearnerId, strDateTime); if (!uploadResult.IsUploadSuccess) { throw new Exception(uploadResult.ErrorMessage); } } dbContextTransaction.Commit(); result.Data = new { LearnerId = newLearner.LearnerId }; return(Ok(result)); } } catch (Exception ex) { result.IsSuccess = false; result.ErrorMessage = ex.Message; return(BadRequest(result)); } }