public HttpResponseMessage Create(HttpRequestMessage request, SpecificationsVM specificationVM)
        {
            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 (_specificationsRepository.SpecificationExists(specificationVM.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
                    {
                        Specifications newSpecification = new Specifications();
                        newSpecification = AutoMapper.Map <SpecificationsVM, Specifications>(specificationVM);

                        _specificationsRepository.Insert(newSpecification);
                        _specificationsRepository.Commit();

                        // Update view model
                        specificationVM = AutoMapper.Map <Specifications, SpecificationsVM>(newSpecification);
                        response = request.CreateResponse <SpecificationsVM>(HttpStatusCode.Created, specificationVM);
                    }
                }
                return response;
            }));
        }
        public HttpResponseMessage Delete(HttpRequestMessage request, SpecificationsVM specificationVM)
        {
            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
                {
                    specificationVM.IsDeleted = true;
                    Specifications _specification = _specificationsRepository.GetSingleBySpecificationsID(specificationVM.ID);
                    _specification.UpdateSpecifications(specificationVM);

                    _specificationsRepository.Commit();

                    response = request.CreateResponse(HttpStatusCode.OK);
                }

                return response;
            }));
        }
예제 #3
0
 public static void UpdateSpecifications(this Specifications specifications, SpecificationsVM specificationsVm)
 {
     specifications.Name        = specificationsVm.Name;
     specifications.Description = specificationsVm.Description;
     specifications.ModifiedBy  = specificationsVm.ModifiedBy;
     specifications.ModifiedOn  = specificationsVm.ModifiedOn;
     specifications.IsDeleted   = specificationsVm.IsDeleted;
 }