Exemplo n.º 1
0
        //public IEnumerable<Employee> GetByDepartment(int department)
        //{
        //    var employees= list.Where(e => e.Department == department).ToList();
        //    if (!employees.Any()) throw new HttpResponseException(HttpStatusCode.NotFound);
        //    return employees;
        //}

        //public IEnumerable<Employee> GetByDepartmentAndLastName([FromUri]Filter filter)
        //{
        //    var employees = list.Where(e => e.Department == filter.Department && e.LastName==filter.Lastname).ToList();
        //    if (!employees.Any()) throw new HttpResponseException(HttpStatusCode.NotFound);
        //    return employees;
        //}
        public HttpResponseMessage Post(Employee employee)
        {
            //if (!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            
            var newId = list.Max(x => x.Id) + 1;
            employee.Id = newId;
            list.Add(employee);
            var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, employee);
            var uri = Url.Link("DefaultApi", new { id = employee.Id });
            response.Headers.Location = new Uri(uri);
            return response;
        }
Exemplo n.º 2
0
 public HttpResponseMessage Put(int id, Employee employee)
 {
     var index = list.ToList().FindIndex(x => x.Id == id);
     if (index >= 0)
     {
         employee.Id = id;
         list[index] = employee;
         return Request.CreateResponse(HttpStatusCode.NoContent);
     }
     return Request.CreateResponse(HttpStatusCode.NotFound);
 }