コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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;
            }
        }
コード例 #3
0
        public override int Add(Model.Lending entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            if (entity.ClientId == 0)
            {
                throw new ArgumentNullException("clientId");
            }
            if (entity.Books == null || entity.Books.Count() == 0)
            {
                throw new ArgumentNullException("books");
            }


            using (var dataContext = GetDataContext())
            {
                DB.Lending newLending = Mapper.Map <DB.Lending>(entity);
                newLending.Books             = new List <DB.LentBook>();
                newLending.LendingEmployeeId = GetCurrentEmployee(dataContext).Id;

                foreach (var lentBook in entity.Books)
                {
                    DB.LentBook book = Mapper.Map <DB.LentBook>(lentBook);
                    book.Lending          = newLending;
                    book.ReturnEmployeeId = (lentBook.ReturnDate != null) ? new int?(newLending.LendingEmployeeId) : null;

                    newLending.Books.Add(book);
                }

                var newRecord = dataContext.Lendings.Add(newLending);
                dataContext.SaveChanges();

                return(newRecord.Id);
            }
        }