public HttpResponseMessage Update(HttpRequestMessage request, PatentDTO item) { if (item == null) { throw new ArgumentNullException("item"); } var dbObject = Mapper.Map <Patent>(item); dbObject.Employees = null; _publicationBaseRepository.Update(dbObject); _db.SaveChanges(); dbObject = (Patent)_publicationBaseRepository.Get(x => x.Id == dbObject.Id, null, x => x.Employees).SingleOrDefault(); foreach (var employee in dbObject.Employees.ToList()) { dbObject.Employees.Remove(employee); } foreach (var employee in item.Employees) { var employeeToAdd = _employeeRepository.GetById(employee.Id); if (employeeToAdd == null) { return(request.CreateErrorResponse( HttpStatusCode.PreconditionFailed, string.Format("Not found employee with id:{0}", employee.Id))); } dbObject.Employees.Add(employeeToAdd); } _db.SaveChanges(); var jobs = new PublicationsJobs(dbObject); jobs.Start(); var mapped = Mapper.Map <PatentDTO>(dbObject); return(request.CreateResponse(HttpStatusCode.NoContent, mapped)); }
public HttpResponseMessage Add(HttpRequestMessage request, PatentDTO item) { if (item == null) { throw new ArgumentNullException("item"); } var dbObject = Mapper.Map <Patent>(item); dbObject.Employees = new List <Employee>(); if (item.Employees != null) { foreach (var employee in item.Employees) { var employeeToAdd = _employeeRepository.GetById(employee.Id); if (employeeToAdd == null) { return(request.CreateErrorResponse( HttpStatusCode.PreconditionFailed, string.Format("Not found employee with id:{0}", employee.Id))); } dbObject.Employees.Add(employeeToAdd); } } _publicationBaseRepository.Insert(dbObject); _db.SaveChanges(); var jobs = new PublicationsJobs(dbObject); jobs.Start(); var mapped = Mapper.Map <PatentDTO>(dbObject); return(request.CreateResponse(HttpStatusCode.Created, mapped)); }