public IEnumerable <Univercity> Get() { using (WebApiDemoContext contexts = new WebApiDemoContext()) { return(contexts.Univercity.ToList()); } }
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 IEnumerable <Student> Get() { using (WebApiDemoContext contexts = new WebApiDemoContext()) { // return all student return(contexts.Student.ToList()); } }
public string Post([FromBody] EmployeeDto employee) { var context = new WebApiDemoContext(); context.Employees.Add(employee); context.SaveChanges(); return("Employee #" + employee.EmployeeNumber + " is created successfully."); }
//HttpGet // api/employee?dept=IT public IEnumerable <EmployeeDto> Get(string dept) { var context = new WebApiDemoContext(); return(context .Employees .Where(e => e.Department == dept) //filter .OrderBy(e => e.Firstname)); //sort }
public IHttpActionResult Get(string field, string value, string sortField, string sortDir) { var context = new WebApiDemoContext(); IQueryable <Product> query = context.Products; if (field.ToUpper() == "CATEGORY") { query = query.Where(p => p.Category == value); } else if (field.ToUpper() == "DESCRIPTION") { query = query.Where(p => p.Description.Contains(value)); } else if (field.ToUpper() == "NUMBER") { query = query.Where(p => p.ProductNumber == value); } else { throw new Exception("Field is not available for filtering..."); } if (sortField.ToUpper() == "NAME") { if (sortDir.ToUpper() == "ASC") { query = query.OrderBy(p => p.Name); } else { query = query.OrderByDescending(p => p.Name); } } else if (sortField.ToUpper() == "PRICE") { if (sortDir.ToUpper() == "ASC") { query = query.OrderBy(p => p.UnitPrice); } else { query = query.OrderByDescending(p => p.UnitPrice); } } else { query = query.OrderBy(p => p.ProductId); } var responseData = query.Select(p => new { p.ProductId, p.Name, p.Category, p.Description }).ToList(); return(Ok(responseData)); }
// api/employee/4 public EmployeeDto Get(int id) { var context = new WebApiDemoContext(); var employee = context.Employees.FirstOrDefault(e => e.EmployeeId == id); if (employee == null) { throw new Exception("Employee id not found."); } return(employee); }
public HttpResponseMessage Get(int id) { using (WebApiDemoContext contexts = new WebApiDemoContext()) { var context = contexts.Univercity.FirstOrDefault(e => e.UnivercityId == id); if (context != null) { return(Request.CreateResponse(HttpStatusCode.OK, context)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Univercity with id" + id.ToString() + "not found")); } } }
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)); } }
public List <EmployeeDto> Get() { var context = new WebApiDemoContext(); return(context.Employees.ToList()); }
public ProductsController(WebApiDemoContext context) { _context = context; }
public IHttpActionResult Get() { var context = new WebApiDemoContext(); return(Ok(context.Products.ToList())); }