public IHttpActionResult Post(EducationEntityModel entityModel) { if (!ModelState.IsValid) { return(BadRequest()); } //first check the school, it should exist var school = _schoolService.Get(entityModel.SchoolId); if (school == null) { return(RespondFailure("The school must be provided to save education", "post_education")); } var education = new Education() { Name = entityModel.Name, Description = entityModel.Description, EducationType = entityModel.EducationType, FromDate = entityModel.FromDate, ToDate = entityModel.ToDate, SchoolId = entityModel.SchoolId, UserId = ApplicationContext.Current.CurrentUser.Id }; _educationService.Insert(education); return(GetResponseModel(education)); }
public IHttpActionResult Put(EducationEntityModel entityModel) { if (!ModelState.IsValid) { return(BadRequest()); } //get the education var education = _educationService.Get(entityModel.Id); if (education == null || !ApplicationContext.Current.CurrentUser.CanEditResource(education)) { return(Unauthorized()); } //first check the school, it should exist var school = _schoolService.Get(entityModel.SchoolId); if (school == null) { return(RespondFailure("The school must be provided to save education", "put_education")); } education.Name = entityModel.Name; education.Description = entityModel.Description; education.FromDate = entityModel.FromDate; education.ToDate = entityModel.ToDate; education.EducationType = entityModel.EducationType; education.SchoolId = entityModel.SchoolId; _educationService.Update(education); return(GetResponseModel(education)); }
public void Post_invalid_model_fails() { using (var controller = ResolveController <EducationController>()) { var model = new EducationEntityModel(); controller.Validate(model); var response = controller.Post(model); Assert.IsInstanceOf <BadRequestErrorMessageResult>(response); } }
public static EducationEntityModel ToEntityModel(this Education education) { var model = new EducationEntityModel() { Name = education.Name, Description = education.Description, FromDate = education.FromDate, ToDate = education.ToDate, EducationType = education.EducationType, SchoolId = education.SchoolId, Id = education.Id, School = education.School?.ToModel(null) }; return(model); }
public void Post_valid_model_succeeds() { using (var controller = ResolveController <EducationController>()) { var school = new School() { Name = "Test School", }; SaveEntity(school); var model = new EducationEntityModel() { Description = "Test description", Name = "Test Education", FromDate = DateTime.UtcNow, EducationType = EducationType.College, ToDate = null, SchoolId = school.Id }; controller.Validate(model); var response = controller.Post(model); Assert.IsTrue(response.GetValue <bool>("Success")); } }