예제 #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);
            }
        }
        protected DB.Employee GetCurrentEmployee(LibraryDataContext dataContext)
        {
            DB.Employee emp = dataContext.Persons.OfType <DB.Employee>().FirstOrDefault(e => e.Username == _username);

            if (emp == null)
            {
                throw new AccessException();
            }
            else
            {
                return(emp);
            }
        }
예제 #3
0
        public void ReturnBooks(Dictionary <int, bool> bookIds, int lendingId)
        {
            if (bookIds == null)
            {
                throw new ArgumentNullException("bookIds");
            }

            DateTime returnDate = DateTime.Now;

            using (var dataContext = GetDataContext())
            {
                DB.Employee currentEmployee = GetCurrentEmployee(dataContext);

                //set ReturnDate and ReturnEmployee according to argument
                dataContext.LentBooks
                .Where(x => bookIds.Keys.Contains(x.Id))
                .ForEach(lent =>
                {
                    if (bookIds[lent.Id])
                    {
                        lent.ReturnDate     = returnDate;
                        lent.ReturnEmployee = currentEmployee;
                    }
                    else
                    {
                        lent.ReturnDate     = null;
                        lent.ReturnEmployee = null;
                    }
                });

                //if all books returned, set ReturnDate of whole Lending
                var lending = dataContext.Lendings.FirstOrDefault(l => l.Id == lendingId);
                if (lending == null)
                {
                    throw new RecordNotFoundException();
                }

                if (lending.Books.All(x => x.ReturnDate.HasValue))
                {
                    lending.ReturnDate = returnDate;
                }
                else
                {
                    lending.ReturnDate = null;
                }

                dataContext.SaveChanges();
            }
        }
예제 #4
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;
            }
        }