private void UpdateLentBooks(LibraryDataContext dataContext, DB.Lending currentEntity, Model.Lending modifiedEntity) { DB.Employee currentEmployee = GetCurrentEmployee(dataContext); var newListOfBooks = modifiedEntity.Books.ToList(); foreach (DB.LentBook book in currentEntity.Books.ToList()) { newListOfBooks.RemoveFirst(x => x.Id == book.Id); Model.LentBook newBook = modifiedEntity.Books.FirstOrDefault(x => x.Id == book.Id); if (newBook == null) //removed { currentEntity.Books.Remove(book); dataContext.LentBooks.Remove(book); } else //modified { UpdateSingleLentBook(book, newBook, currentEmployee); } } //add new LentBooks foreach (Model.LentBook book in newListOfBooks) { DB.LentBook toAdd = Mapper.Map <DB.LentBook>(book); toAdd.Lending = currentEntity; toAdd.ReturnEmployee = (book.ReturnDate != null) ? currentEmployee : null; currentEntity.Books.Add(toAdd); } }
private void UpdateSingleLentBook(DB.LentBook dbEntity, Model.LentBook modelEntity, DB.Employee currentEmployee) { dbEntity.EndDate = modelEntity.EndDate; dbEntity.ReturnDate = modelEntity.ReturnDate; if (modelEntity.ReturnDate.HasValue) { if (modelEntity.ReturnEmployeeId != null) { dbEntity.ReturnEmployeeId = modelEntity.ReturnEmployeeId; } else { dbEntity.ReturnEmployee = currentEmployee; } } else { dbEntity.ReturnEmployee = null; } }