// DELETE api/services/4 public HttpResponseMessage Delete(int id) { var service = new Service { ServiceId = id }; _db.Services.Attach(service); var status = _db.Services.Remove(service); _db.SaveChanges(); if (status != null) return new HttpResponseMessage(HttpStatusCode.OK); throw new HttpResponseException(HttpStatusCode.NotFound); }
// POST api/services public HttpResponseMessage Post([FromBody] ServiceModel value) { var servType = _db.ServiceTypes.FirstOrDefault(st => st.ServiceTypeName == value.ServiceTypeName); var service = new Service { ServiceName = value.ServiceName, Price = value.Price, ServiceTypeId=value.ServiceTypeId, Description=value.Description }; var serv = _db.Services.Add(service); _db.SaveChanges(); if (serv != null) { var msg = new HttpResponseMessage(HttpStatusCode.Created); msg.Headers.Location = new Uri(Request.RequestUri + "/" + service.ServiceId.ToString()); return msg; } else { var msg = new HttpResponseMessage(HttpStatusCode.BadRequest); return msg; } }
// PUT api/services/4 public HttpResponseMessage Put(int id, [FromBody] ServiceModel values) { var serviceType=_db.ServiceTypes.FirstOrDefault(st=>st.ServiceTypeName==values.ServiceTypeName); var serv = new Service { ServiceId = values.ServiceId, ServiceName = values.ServiceName, Price = values.Price, ServiceTypeId = serviceType.ServiceTypeId }; var status = _db.Services.Attach(serv); var entry = _db.Entry(serv); entry.Property(e => e.ServiceName).IsModified = true; entry.Property(e => e.Price).IsModified = true; entry.Property(e => e.ServiceTypeId).IsModified = true; _db.SaveChanges(); if (status != null) return new HttpResponseMessage(HttpStatusCode.OK); throw new HttpResponseException(HttpStatusCode.NotFound); }