public HttpResponseMessage Create(HttpRequestMessage request, ThirdPartyInspectionVM thirdPartyInspectionVM) { return(CreateHttpResponse(request, () => { HttpResponseMessage response = null; if (!ModelState.IsValid) { response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState.Keys.SelectMany(k => ModelState[k].Errors).Select(m => m.ErrorMessage).ToArray()); } else { if (_thirdPartyInspectionsRepository.ThirdPartyInspectionExists(thirdPartyInspectionVM.Email, thirdPartyInspectionVM.Name)) { ModelState.AddModelError("Invalid user", "Email or Name already exists"); response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState.Keys.SelectMany(k => ModelState[k].Errors).Select(m => m.ErrorMessage).ToArray()); } else { ThirdPartyInspection newThirdPartyInspection = new ThirdPartyInspection(); newThirdPartyInspection = AutoMapper.Map <ThirdPartyInspectionVM, ThirdPartyInspection>(thirdPartyInspectionVM); _thirdPartyInspectionsRepository.Insert(newThirdPartyInspection); _thirdPartyInspectionsRepository.Commit(); // Update view model thirdPartyInspectionVM = AutoMapper.Map <ThirdPartyInspection, ThirdPartyInspectionVM>(newThirdPartyInspection); response = request.CreateResponse <ThirdPartyInspectionVM>(HttpStatusCode.Created, thirdPartyInspectionVM); } } return response; })); }
public HttpResponseMessage Delete(HttpRequestMessage request, ThirdPartyInspectionVM thirdPartyInspectionVM) { return(CreateHttpResponse(request, () => { HttpResponseMessage response = null; if (!ModelState.IsValid) { response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState.Keys.SelectMany(k => ModelState[k].Errors) .Select(m => m.ErrorMessage).ToArray()); } else { thirdPartyInspectionVM.IsDeleted = true; ThirdPartyInspection _thirdPartyInspection = _thirdPartyInspectionsRepository.GetSingleByThirdPartyInspectionID(thirdPartyInspectionVM.ID); _thirdPartyInspection.UpdateThirdPartyInspection(thirdPartyInspectionVM); _thirdPartyInspectionsRepository.Commit(); response = request.CreateResponse(HttpStatusCode.OK); } return response; })); }
public static void UpdateThirdPartyInspection(this ThirdPartyInspection thirdPartyInspection, ThirdPartyInspectionVM thirdPartyInspectionVm) { thirdPartyInspection.Name = thirdPartyInspectionVm.Name; thirdPartyInspection.Address = thirdPartyInspectionVm.Address; thirdPartyInspection.Landline = thirdPartyInspectionVm.Landline; thirdPartyInspection.Mobile = thirdPartyInspectionVm.Mobile; thirdPartyInspection.Email = thirdPartyInspectionVm.Email; thirdPartyInspection.ModifiedBy = thirdPartyInspectionVm.ModifiedBy; thirdPartyInspection.ModifiedOn = thirdPartyInspectionVm.ModifiedOn; thirdPartyInspection.AdditionalDetails = thirdPartyInspectionVm.AdditionalDetails; thirdPartyInspection.IsDeleted = thirdPartyInspectionVm.IsDeleted; }