public ActionResult SynCourse() { APIController api = new APIController(); string data = api.ReadData("https://sodaubai.vanlanguni.edu.vn/API/getCourses"); CourseModel course = JsonConvert.DeserializeObject <CourseModel>(data); foreach (var item in course.Courses) { var SynCourse = db.Courses.FirstOrDefault(x => x.Code == item.Code && x.Type1 == item.Type1 && x.Type2 == item.Type2 && x.Semester == course.Semester); if (SynCourse == null) { Course newCourse = new Course(); newCourse.Code = item.Code; newCourse.CourseName = item.Name; newCourse.Type1 = item.Type1; newCourse.Type2 = item.Type2; newCourse.Major = db.Majors.FirstOrDefault(x => x.Code == item.Major).ID; newCourse.Credit = item.Credit; newCourse.Lecturer = item.Lecturer; newCourse.Students = item.Students; newCourse.DayOfWeek = item.DayOfWeek; newCourse.TimeSpan = item.TimeSpan; newCourse.Periods = item.Periods; newCourse.Room = item.Room; newCourse.Semester = course.Semester; db.Courses.Add(newCourse); } else { SynCourse.Code = item.Code; SynCourse.CourseName = item.Name; SynCourse.Type1 = item.Type1; SynCourse.Type2 = item.Type2; SynCourse.Major = db.Majors.FirstOrDefault(x => x.Code == item.Major).ID; SynCourse.Credit = item.Credit; SynCourse.Lecturer = item.Lecturer; SynCourse.Students = item.Students; SynCourse.DayOfWeek = item.DayOfWeek; SynCourse.TimeSpan = item.TimeSpan; SynCourse.Periods = item.Periods; SynCourse.Room = item.Room; SynCourse.Semester = course.Semester; } } db.SaveChanges(); return(RedirectToAction("Index")); }
public void SynMajor() { APIController api = new APIController(); string data = api.ReadData("https://cntttest.vanlanguni.edu.vn:18081/SoDauBai/API/getMajors"); List <MajorsModel> major = JsonConvert.DeserializeObject <List <MajorsModel> >(data); foreach (var item in major) { if (db.Majors.FirstOrDefault(x => x.Code == item.code) == null) { Major newMajor = new Major(); newMajor.Code = item.code; newMajor.Name = item.name; db.Majors.Add(newMajor); db.SaveChanges(); } } }