public HttpResponseMessage Put(EmployeeViewModelEdition model) { try { Employee emp = _empRepository.GetById(model.Id); emp.Role = _roleRepository.GetById(model.IdRole); emp.Name = model.Name; emp.Email = model.Email; emp.BirthDate = Convert.ToDateTime(model.BirthDate); emp.Gender = (Gender)Enum.Parse(typeof(Gender), model.Gender); //foreach (var dep in emp.Dependents) //ToDo: refazer esta logica para considerar o length dos dependentes antigos e novose //{ // foreach (var name in model.Dependents) // { // dep.Name = name; // } //} _empRepository.Update(emp); return(Request.CreateResponse(HttpStatusCode.OK, "Employee data has been updated successfully.")); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public HttpResponseMessage GetById(Guid id) { try { var emp = _empRepository.GetById(id); var role = _roleRepository.GetById(emp.Role.Id); emp.Role = role; emp.IdRole = emp.Role.Id; var model = new EmployeeViewModelEdition() { Id = emp.Id, Name = emp.Name, Email = emp.Email, Gender = emp.Gender.ToString(), BirthDate = emp.BirthDate.ToString(), IdRole = emp.IdRole }; emp.Dependents = _depRepository.GetAll().Where(x => x.Employee.Id == emp.Id).ToList(); if (emp.Dependents.Any()) { model.Dependents = new List <string>(); foreach (var dep in emp.Dependents) { model.Dependents.Add(dep.Name); } } return(Request.CreateResponse(HttpStatusCode.OK, model)); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message)); } }