public IHttpActionResult PutCustomer(int id, Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != customer.Id) { return(BadRequest()); } db.Entry(customer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public HttpResponseMessage Put(int id, [FromBody] Univercity univercity) { try { using (WebApiDemoContext contexts = new WebApiDemoContext()) { var context = contexts.Univercity.FirstOrDefault(e => e.UnivercityId == id); if (context == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Univercity with id = " + id.ToString() + "not found to delet")); } else { context.UnivercityId = univercity.UnivercityId; context.UnivercityName = univercity.UnivercityName; contexts.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, context)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public string Put(int id, [FromBody] EmployeeDto employee) { var context = new WebApiDemoContext(); EmployeeDto employeeUpdate = context .Employees .FirstOrDefault(e => e.EmployeeId == id); if (employeeUpdate == null) { return("Employee id " + id.ToString() + " not found."); } employeeUpdate.Firstname = employee.Firstname; employeeUpdate.Lastname = employee.Lastname; employeeUpdate.MiddleName = employee.MiddleName; employeeUpdate.SuffixName = employee.SuffixName; employeeUpdate.MobileNumber = employee.MobileNumber; employeeUpdate.LandlineNumber = employee.LandlineNumber; context.SaveChanges(); return("Employee number " + employee.EmployeeNumber + " with name " + employee.Firstname + " " + employee.Lastname + " successfully updated."); }
public string Post([FromBody] EmployeeDto employee) { var context = new WebApiDemoContext(); context.Employees.Add(employee); context.SaveChanges(); return("Employee #" + employee.EmployeeNumber + " is created successfully."); }
public HttpResponseMessage Post([FromBody] Univercity univercity) { try { using (WebApiDemoContext contexts = new WebApiDemoContext()) { contexts.Univercity.Add(univercity); contexts.SaveChanges(); var context = Request.CreateResponse(HttpStatusCode.Created, univercity); context.Headers.Location = new Uri(Request.RequestUri + univercity.UnivercityId.ToString); return(context); } } catch (Exception ex) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); } }
public HttpResponseMessage Get(int id) { try { using (WebApiDemoContext contexts = new WebApiDemoContext()) { contexts.Student.Add(student); contexts.SaveChanges(); var context = Request.CreateResponse(HttpStatusCode.Created, student); context.Headers.Location = new Uri(Request.RequestUri + student.StudentId.ToString); return(context); } } catch (Exception ex) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); } }
public HttpResponseMessage Delete(int id) { try { using (WebApiDemoContext contexts = new WebApiDemoContext()) { var context = contexts.Student.FirstOrDefault(e => e.StudentId == id); if (context == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Student with id = " + id.ToString() + "not found to delet")); } else { contexts.Student.Remove(context); contexts.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }