public void SaveStudent(StudentModel studentModel,string currentUser) { if (currentUser != null) { //Student Addresses var studentAddress = new Address { Address1 = studentModel.Address1, AddressLine2 = studentModel.Address2, CreatedBy = currentUser, CreatedDate = DateTime.Now, Mobile = studentModel.Mobile, }; if (Regex.IsMatch(studentModel.PostalCode, @"\d")) { studentAddress.PostCode = Convert.ToInt32(studentModel.PostalCode); // studentAddress. } if (studentModel.SuburbId != 0) studentAddress.SuburbID = studentModel.SuburbId; DataContext.Addresses.Add(studentAddress); DataContext.SaveChanges(); // Student Creation // last student var lastStudent = TblStudents.ToList().OrderByDescending(o => o.StudentId).FirstOrDefault(); var studentNumber = "0001"; if (lastStudent != null) { var year = Convert.ToInt32(lastStudent.StudentNumber.Substring(3, 4)); var number = Convert.ToInt32(lastStudent.StudentNumber.Substring(7, 4)); if (DateTime.Now.Year == year) studentNumber = (number + 1).ToString("0000"); } var student = new Student { FirstName = studentModel.FirstName, Email = studentModel.Email, LastName = studentModel.LastName, Gender = studentModel.Gender, Status = studentModel.Status, CreatedBy = currentUser, CreatedDate = DateTime.Now, StartDate = studentModel.StartDate, AddressId = studentAddress.AddressId, StudentNumber = "SID" + DateTime.Now.Year.ToString() + studentNumber, PickupLocation = studentModel.PickupLocation }; DataContext.Students.Add(student); DataContext.SaveChanges(); //License Information var studentLicense = new Student_License { StudentId = student.Id, ExpiryDate = Convert.ToDateTime(studentModel.ExpiryDate), StateId = studentModel.StateId, Class = studentModel.ClassName, LicenseNo = studentModel.LicenseNumber, License_passed_Date =studentModel.LicensePassedDate, Remarks = studentModel.Remarks, //International License details IsInternationalLicensed = studentModel.IsInternationalLicensed, Country = studentModel.Country }; DataContext.Student_License.Add(studentLicense); DataContext.SaveChanges(); if (!string.IsNullOrEmpty(studentModel.InstructorNumber)) { var instructor = DataContext.Instructors.SingleOrDefault( s => (s.InstructorNumber) == studentModel.InstructorNumber); // Booking Information if (instructor != null) { //Save instructor student var instructorStudent = new Instructor_Student { InstructorId = instructor.InstructorId, StudentId = student.Id, CreatedBy = currentUser, CreatedDate = DateTime.Now, Remarks = studentModel.Remarks, }; DataContext.Instructor_Student.Add(instructorStudent); DataContext.SaveChanges(); if (studentModel.BookingStartDate != null) { var studentBooking = new Booking { StudentId = student.Id, PackageId = studentModel.PackageId, CreatedDate = DateTime.Now, BookingDate = DateTime.Now, StartDate = studentModel.BookingStartDate, EndDate = studentModel.BookingEndDate, InstructorId = instructor.InstructorId, StartTime = studentModel.StartTime, EndTime = studentModel.StopTime, Discount = studentModel.PackageDisacount, PickupLocation = studentModel.PickupLocation, DropLocation = studentModel.DropLocation, CreatedBy = currentUser, Type = "Learning" }; DataContext.Bookings.Add(studentBooking); DataContext.SaveChanges(); } if (studentModel.DrivingTestPackageId != null) { // Driving Test Information var studentDrivingBooking = new Booking { StudentId = student.Id, PackageId = studentModel.DrivingTestPackageId, CreatedDate = DateTime.Now, BookingDate = studentModel.DrivingTestDate, InstructorId = instructor.InstructorId, StartTime = studentModel.DrivingTestStartTime, EndTime = studentModel.DrivingTestStopTime, Discount = studentModel.DrivingTestPackageDisacount, PickupLocation = studentModel.DrivingTestPickupLocation, DropLocation = studentModel.DrivingTestDropLocation, CreatedBy = currentUser, Type = "Driving" }; DataContext.Bookings.Add(studentDrivingBooking); DataContext.SaveChanges(); } } } } }
public void SaveInstructor(RegisterViewModel model) { // Save Address var instructorAddress = new Address { Address1 = model.AddressLine1, AddressLine2 = model.AddressLine2, Phone = model.Phone, Mobile = model.Mobile, PostCode = Convert.ToInt32(model.PostalCode), CreatedDate = DateTime.Now, CreatedBy = model.CreatedUser, }; if (model.SuburbId != 0) instructorAddress.SuburbID = model.SuburbId; DataContext.Addresses.Add(instructorAddress); SaveInDatabase(); // _instructorRepo. // Create Instructor account var instructor = new Instructor { Created_Date = DateTime.Now, InstructorId = model.InstructorId, Created_By = model.CreatedUser, InstructorNumber = "INS-" + model.LastInstructor, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Gender = model.Gender.ToString(), Mobile = model.Mobile, Phone = model.Phone, AddressId = instructorAddress.AddressId, Status = model.Status, Areas = model.AreaIds }; DataContext.Instructors.Add(instructor); SaveInDatabase(); foreach (var iarea in model.AreaNames.Split(',')) { // Instructor Area var instructorArea = new InstructorArea { InstructorID = instructor.InstructorId, AreaId = Convert.ToInt32(iarea) }; DataContext.InstructorAreas.Add(instructorArea); SaveInDatabase(); } }