public Company Update(Company updatedCompany) { var company = this.Get(updatedCompany.Id); company.Name = updatedCompany.Name; company.PIN = updatedCompany.PIN; company.Contact = updatedCompany.Contact; company.Description = updatedCompany.Description; return updatedCompany; }
// POST api/Companies public HttpResponseMessage PostCompany(Company company) { if (ModelState.IsValid) { _repository.Add(company); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, company); response.Headers.Location = new Uri(Request.RequestUri, "/api/companies/" + company.Id.ToString()); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
public void Post() { Company company = new Company { Id = 5, Name = "Testing1", PIN = "92727475465" }; HttpConfiguration config = new HttpConfiguration(); WebApiConfig.Register(config); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost"); request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; var repository = new SampleDataRepository(); var controller = new CompaniesController(repository) { Request = request }; HttpResponseMessage response = controller.PostCompany(company); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.IsNotNull(response.Headers.Location); Company newCompany = response.Content.ReadAsAsync<Company>().Result; Assert.IsNotNull(newCompany); }
public Company Update(Company Company) { _db.Entry(Company).State = EntityState.Modified; _db.SaveChanges(); return Company; }
public Company Add(Company Company) { _db.Companies.Add(Company); _db.SaveChanges(); return Company; }
// PUT api/Companies/5 public HttpResponseMessage PutCompany(int id, Company company) { if (ModelState.IsValid && id == company.Id) { try { _repository.Update(company); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
public Company Add(Company company) { companies.Add(company); return company; }