public IHttpActionResult Create(CreateTeacherModel model) { IHttpActionResult httpActionResult; ErrorModel errors = new ErrorModel(); if (string.IsNullOrEmpty(model.MaGV)) { errors.Add("Mã giáo viên là trường bắt buộc"); } if (string.IsNullOrEmpty(model.TenGV)) { errors.Add("Tên giáo viên là trường bắt buộc"); } if (errors.Errors.Count == 0) { Teacher gv = new Teacher(); gv.MaGV = model.MaGV; gv.TenGV = model.TenGV; gv = _db.Teacher.Add(gv); this._db.SaveChanges(); TeacherModel viewModel = new TeacherModel(gv); httpActionResult = Ok(viewModel); } else { httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.BadRequest, errors); } return(httpActionResult); }
public IHttpActionResult CreateTeacher(CreateTeacherModel model) { IHttpActionResult httpActionResult; ErrorModel error = new ErrorModel(); if (string.IsNullOrEmpty(model.TeacherId)) { error.Add("Mã giáo viên không được để trống"); } if (string.IsNullOrEmpty(model.TeacherName)) { error.Add("Họ tên giáo viên không được để trống"); } if (error.Errors.Count == 0) { Teacher teacher = new Teacher(); teacher.TeacherId = model.TeacherId; teacher.TeacherName = model.TeacherName; teacher.TeacherAddress = model.TeacherAddress; teacher = _db.Teacher.Add(teacher); _db.SaveChanges(); TeacherModel vienModel = new TeacherModel(teacher); httpActionResult = Ok(vienModel); } else { httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.BadRequest, error); } return(httpActionResult); }