Exemplo n.º 1
0
        public IHttpActionResult UpdateSupplier(SupplierModel model)
        {
            var entity = new Supplier
            {
                Address      = model.Address,
                City         = model.City,
                CompanyName  = model.CompanyName,
                ContactName  = model.ContactName,
                ContactTitle = model.ContactTitle,
                Country      = model.Country,
                Fax          = model.Fax,
                HomePage     = model.HomePage,
                Phone        = model.Phone,
                PostalCode   = model.PostalCode,
                Region       = model.Region
            };

            _repository.UpdateSupplier(entity);
            return(Ok <SupplierModel>(model));
        }
        public async Task <IHttpActionResult> Patch(SupplierModel model, int id)
        {
            try
            {
                if (ModelState.IsValid && model != null)
                {
                    model.SupplierID = id;
                    var supplier = await _repository.GetSuppliersAsync(id);

                    if (supplier != null)
                    {
                        _mapper.Map(model, supplier);
                        _repository.UpdateSupplier(supplier);
                        if (await _repository.SaveChangesAsync())
                        {
                            var newModel = _mapper.Map <SupplierModel>(supplier);
                            return(Ok(newModel));
                        }
                        else
                        {
                            return(Content(HttpStatusCode.Conflict, string.Format("Supplier with id {0} could not be updated", supplier.SupplierID)));
                        }
                    }
                    else
                    {
                        return(Content(HttpStatusCode.Conflict, string.Format("Supplier with id {0} could not be found", supplier.SupplierID)));
                    }
                }
                else
                {
                    return(Content(HttpStatusCode.BadRequest, "Incorrect input data"));
                }
            }
            catch
            {
                return(InternalServerError());
            }
        }