Example #1
0
        public Shared.Logic.WebModel.IAuthor AddAuthor(Shared.Logic.WebModel.IAuthor author)
        {
            if (author == null)
            {
                return(null);
            }
            IAuthor dataAuthor = Mapper.MapEntities <Shared.Logic.WebModel.IAuthor, IAuthor>(author);

            if (_dataContext is null || dataAuthor is null)
            {
                return(null);
            }

            lock (_authorsLock)
            {
                if (_dataContext.Authors.Select(a => a.Id).Contains(author.Id))
                {
                    return(author);
                }

                lock (_booksLock)
                {
                    for (int i = 0; i < dataAuthor.Books.Count(); ++i)
                    {
                        IBook original = _dataContext.Books.FirstOrDefault(b => b.Id == dataAuthor.Books[i].Id);
                        if (original != null)
                        {
                            dataAuthor.Books[i] = original;
                        }
                    }
                    foreach (IBook book in dataAuthor.Books.Where(b => !_dataContext.Books.Select(a => a.Id).Contains(b.Id)))
                    {
                        _dataContext.AddBook(book);
                    }
                }
                _dataContext.AddAuthor(dataAuthor);
            }
            return(author);
        }