public ActionResult Delete(TimeTable model) { try { int id = model.TeachingSubjectID; // TODO: Add delete logic here TimeTable theTable = new TimeTable(); TeachingSubject theSubject = work.TeachingSubjectRepository.GetByID(id); int TeachingDayID = theSubject.TeachingDay.TeachingDayID; int TeachingClassID = theSubject.TeachingDay.TeachingClass.TeachingClassID; int TeacherID = theSubject.TeachingDay.TeachingClass.Teacher.TeacherID; work.TeachingSubjectRepository.Delete(theSubject.TeachingSubjectID); work.TeachingDayRepository.Delete(TeachingDayID); work.TeachingClassRepository.Delete(TeachingClassID); work.TeacherRepository.Delete(TeacherID); work.Save(); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Delete(int id) { TimeTable theTable = new TimeTable(); TeachingSubject theSubject = work.TeachingSubjectRepository.GetByID(id); theTable.Day = theSubject.TeachingDay.theDay.ToString(); theTable.EndTimeHour = theSubject.TeachingDay.EndTimeHour; theTable.EndTimeMinute = theSubject.TeachingDay.EndTimeMinute; theTable.StratTimeHour = theSubject.TeachingDay.StratTimeHour; theTable.StratTimeMinute = theSubject.TeachingDay.StratTimeMinute; theTable.Subject = theSubject.SubjectName; theTable.Teacher = theSubject.TeachingDay.TeachingClass.Teacher.TeacherName; theTable.Class = theSubject.TeachingDay.TeachingClass.ClassName; theTable.TeachingSubjectID = theSubject.TeachingSubjectID; return(View(theTable)); }
public ActionResult Create(TimeTable model) { List <Level> theLevels = work.LevelRepository.Get().ToList(); List <SelectListItem> theItem = new List <SelectListItem>(); theItem.Add(new SelectListItem() { Text = "None", Value = "" }); foreach (var level in theLevels) { theItem.Add(new SelectListItem() { Text = level.LevelName + ":" + level.Type, Value = level.LevelName + ":" + level.Type }); } ViewData["Level"] = theItem; List <Subject> theSubject = work.SubjectRepository.Get().ToList(); List <SelectListItem> theSubjectsItem = new List <SelectListItem>(); theSubjectsItem.Add(new SelectListItem() { Text = "None", Value = "" }); foreach (var sub in theSubject) { theSubjectsItem.Add(new SelectListItem() { Text = sub.Name, Value = sub.Name }); } ViewData["Subject"] = theSubjectsItem; List <PrimarySchoolStaff> staff = work.PrimarySchoolStaffRepository.Get().OrderBy(a => a.LastName).ToList(); List <SelectListItem> theStaffItem = new List <SelectListItem>(); theStaffItem.Add(new SelectListItem() { Text = "None", Value = "" }); foreach (var s in staff) { theStaffItem.Add(new SelectListItem() { Text = s.LastName + " " + s.Middle + " " + s.FirstName, Value = s.LastName + " " + s.Middle + " " + s.FirstName }); } ViewData["Staff"] = theStaffItem; try { string v = model.Subject + ";" + model.Class + ";" + model.StratTimeHour + ";" + model.StratTimeMinute + ";" + model.Teacher + ";" + model.Day; string theReturnedValue = TimeTableCollisionAviodance.CheckDaySlotAvialability(v); string theReturnedValueSubject = TimeTableCollisionAviodance.CheckSubjectSlotAvialability(v); string theReturnedValueStaff = TimeTableCollisionAviodance.CheckStaffSlotAvialability(v); if (!(string.IsNullOrEmpty(theReturnedValue))) { // ModelState.AddModelError("", "Create a Class Subjects First for Class " + theStudent.PresentLevel); // return View(theReg); ModelState.AddModelError("", theReturnedValue); return(View(model)); } int start = Convert.ToInt32(model.StratTimeHour); int finish = Convert.ToInt32(model.EndTimeHour); if (finish < start) { ModelState.AddModelError("", "Start Time of a Subject must be greater than its finish time!"); return(View(model)); } if (!(string.IsNullOrEmpty(theReturnedValueSubject))) { ModelState.AddModelError("", theReturnedValueSubject); return(View(model)); } if (!(string.IsNullOrEmpty(theReturnedValueStaff))) { ModelState.AddModelError("", theReturnedValueStaff); return(View(model)); } //var theValue = $('#Subject').val() + ";" + $('#Class').val() + ";" + $('#StratTimeHour').val() + ";" + $('#StratTimeMinute').val() + ";" + $('#Teacher').val() + ";" + $('#Day').val(); TeachingSubject ts = new TeachingSubject(); ts.SubjectName = model.Subject; TeachingDay td = new TeachingDay(); td.StratTimeHour = model.StratTimeHour; td.StratTimeMinute = model.StratTimeMinute; td.EndTimeHour = model.EndTimeHour; td.EndTimeMinute = model.EndTimeMinute; if (model.Day == "SUNDAY") { td.theDay = Day.SUNDAY; } if (model.Day == "MONDAY") { td.theDay = Day.MONDAY; } if (model.Day == "TUESDAY") { td.theDay = Day.TUESDAY; } if (model.Day == "WEDNESDAY") { td.theDay = Day.WEDNESDAY; } if (model.Day == "THURSDAY") { td.theDay = Day.THURSDAY; } if (model.Day == "FRIDAY") { td.theDay = Day.FRIDAY; } if (model.Day == "SATURDAY") { td.theDay = Day.SATURDAY; } TeachingClass tc = new TeachingClass(); tc.ClassName = model.Class; List <TeachingSubject> tss = new List <TeachingSubject>(); tss.Add(ts); td.TeachingSubject = tss; List <TeachingDay> tdd = new List <TeachingDay>(); tdd.Add(td); tc.TheTeachingDay = tdd;; List <TeachingClass> tt = new List <TeachingClass>(); tt.Add(tc); Teacher t = new Teacher(); // work.PrimarySchoolStaffRepository.ge t.TeacherName = model.Teacher; string[] theTeacher = model.Teacher.Split(' '); string firstName = theTeacher[2]; string lastName = theTeacher[0]; List <PrimarySchoolStaff> priStaff = work.PrimarySchoolStaffRepository.Get(a => a.LastName.Equals(lastName) && a.FirstName.Equals(firstName)).ToList(); t.ThePersonID = priStaff[0].PersonID; t.TheTeachingClass = tt; work.TeacherRepository.Insert(t); work.Save(); // td.theDay = model.Day; // TODO: Add insert logic here return(RedirectToAction("Create")); // return RedirectToAction("Index"); } catch { return(View()); } }
/// <summary> /// CreateGeneralUserAsync /// </summary> /// <param name="toBeCreatedgeneralUser"></param> /// <returns></returns> public async Task <IGeneralUser> CreateGeneralUserAsync(IGeneralUser toBeCreatedgeneralUser) { var strategy = _dbContext.Database.CreateExecutionStrategy(); IGeneralUser IGeneralUser = default; await strategy.Execute(async() => { using (TransactionScope transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // First try to create AppUser var userIdentity = _mapper.Map <IGeneralUser, AppUser>(toBeCreatedgeneralUser); var createAppUserResult = await _userManager.CreateAsync(userIdentity, toBeCreatedgeneralUser.Password); if (!createAppUserResult.Succeeded) { throw new ArgumentException(createAppUserResult.Errors.FirstOrDefault()?.Description); } // Validate related id's var newGeneralUser = new GeneralUser { Identity = userIdentity, TechLevel = toBeCreatedgeneralUser.TechLevel, Location = toBeCreatedgeneralUser.Location, Locale = toBeCreatedgeneralUser.Locale, Gender = toBeCreatedgeneralUser.Gender }; // TODO: simplify mapping mechanism so that these relational objects don't need to be saved separately // Validate and save city City city = _dbContext.Cities.SingleOrDefault(c => c.Id == toBeCreatedgeneralUser.City.Id); newGeneralUser.City = city ?? throw new ArgumentException($"City ID: {toBeCreatedgeneralUser.City.Id} could not be found."); // Validate and save TeachingAgeGroups if (toBeCreatedgeneralUser.TeachingAgeGroups != null) { foreach (ITeachingAgeGroup iTeachingAgeGroup in toBeCreatedgeneralUser.TeachingAgeGroups) { TeachingAgeGroup teachingAgeGroup = _dbContext.TeachingAgeGroups.SingleOrDefault(t => t.Id == iTeachingAgeGroup.Id); if (teachingAgeGroup == default) { throw new ArgumentException($"TeachingGroup ID: {iTeachingAgeGroup.Id} could not be found."); } GeneralUserTeachingAgeGroup newGeneralUserTeachingAgeGroup = new GeneralUserTeachingAgeGroup { TeachingAgeGroupId = iTeachingAgeGroup.Id }; newGeneralUser.GeneralUserTeachingAgeGroups.Add(newGeneralUserTeachingAgeGroup); } } // Validate and Save TeachingSubjects if (toBeCreatedgeneralUser.TeachingSubjects != null) { foreach (ITeachingSubject iTeachingSubject in toBeCreatedgeneralUser.TeachingSubjects) { TeachingSubject teachingSubject = _dbContext.TeachingSubjects.SingleOrDefault(t => t.Id == iTeachingSubject.Id); if (teachingSubject == default) { throw new ArgumentException($"TeachingSubject ID: {iTeachingSubject.Id} could not be found."); } GeneralUserTeachingSubject newGeneralUserTeachingSubject = new GeneralUserTeachingSubject { TeachingSubjectId = iTeachingSubject.Id }; newGeneralUser.GeneralUserTeachingSubjects.Add(newGeneralUserTeachingSubject); } } // Validate and Save TeachingLevels if (toBeCreatedgeneralUser.TeachingLevels != null) { foreach (ITeachingLevel iTeachingLevel in toBeCreatedgeneralUser.TeachingLevels) { TeachingLevel teachingLevel = _dbContext.TeachingLevels.SingleOrDefault(t => t.Id == iTeachingLevel.Id); if (teachingLevel == default) { throw new ArgumentException($"TeachingLevel ID: {iTeachingLevel.Id} could not be found."); } GeneralUserTeachingLevel newGeneralUserTeachingLevel = new GeneralUserTeachingLevel { TeachingLevelId = iTeachingLevel.Id }; newGeneralUser.GeneralUserTeachingLevels.Add(newGeneralUserTeachingLevel); } } // Validate and Save Interests if (toBeCreatedgeneralUser.Interests != null) { foreach (IInterest iInterest in toBeCreatedgeneralUser.Interests) { Interest interest = _dbContext.Interests.SingleOrDefault(i => i.Id == iInterest.Id); if (interest == default) { throw new ArgumentException($"Interest ID: {iInterest.Id} could not be found."); } GeneralUserInterest newGeneralUserInterest = new GeneralUserInterest { InterestId = iInterest.Id }; newGeneralUser.GeneralUserInterests.Add(newGeneralUserInterest); } } await _dbContext.GeneralUsers.AddAsync(newGeneralUser); await _dbContext.SaveChangesAsync(); IGeneralUser = await Task.FromResult(_mapper.Map <GeneralUser, IGeneralUser>(newGeneralUser)); // Upload profile image to S3 only after succesfully AppUser creation if (toBeCreatedgeneralUser.ProfileImage != null) { IImage profileImage = await _awsS3Service.UploadProfileImageByUserAsync(toBeCreatedgeneralUser.ProfileImage, userIdentity.Id.ToString()); IGeneralUser.ProfileImage = profileImage; } // Commit transaction if all commands succeed, transaction will auto-rollback // when disposed if either commands fails transaction.Complete(); } }); return(IGeneralUser); }