Exemple #1
0
        public void Test_Lazy_Initialization()
        {
            Book   book   = bookDao.FindById(1);
            Author author = authorDao.FindById(1);

            // check if both obtained instances are proxies

            Assert.IsTrue(book is BookProxy);
            Assert.IsTrue(author is AuthorProxy);

            // check if both proxies have uninitialized collections

            BookProxy   bookProxy   = (BookProxy)book;
            AuthorProxy authorProxy = (AuthorProxy)author;

            Assert.IsFalse(bookProxy.AuthorsAreFetchedOrSet);
            Assert.IsFalse(authorProxy.BooksAreFetchedOrSet);

            // initialize collection of authors

            Console.WriteLine();

            foreach (Author a in book.Authors)
            {
                Console.WriteLine($"First Name: {a.FirstName}, LastName: {a.LastName}");
            }

            Console.WriteLine();

            foreach (Book b in author.Books)
            {
                Console.WriteLine($"Id: {b.Id}, Title: {b.Title}");
            }

            Console.WriteLine();

            // check if object inside collection is also proxy

            var authors = new List <Author>(book.Authors);
            var books   = new List <Book>(author.Books);

            Author a1 = authors[1];
            Book   b1 = books[0];

            Assert.IsTrue(a1 is AuthorProxy);
            Assert.IsTrue(b1 is BookProxy);

            authorProxy = (AuthorProxy)a1;
            bookProxy   = (BookProxy)b1;

            Assert.IsFalse(authorProxy.BooksAreFetchedOrSet);
            Assert.IsFalse(bookProxy.AuthorsAreFetchedOrSet);
        }
        public async Task UpdateBookAsync(int id, BookProxy book)
        {
            Validate(book);

            var entity = await GetByIdAsync(id);

            await _authorModel.ValidateExistingAuthorAsync(book.AuthorId);

            await ValidateUniqueness(book.Title, id);

            MapToEntity(book, entity);

            await _repository.SaveChangesAsync();
        }
        public async Task <IdResponse> CreateBookAsync(BookProxy book)
        {
            Validate(book);

            await _authorModel.ValidateExistingAuthorAsync(book.AuthorId);

            await ValidateUniqueness(book.Title, null);

            var entity = new Book();

            MapToEntity(book, entity);

            var id = await _repository.AddAsync(entity);

            return(new IdResponse(id));
        }
Exemple #4
0
        private BookProxy MapEntityToProxy(Entities.Book e)
        {
            var ret = new BookProxy();

            ret.Id     = e.Id;
            ret.Title  = e.Title;
            ret.Author = e.AuthorName;

            if (FeatureAddTableAtuthor.Get())
            {
                ret.Author = e.Author?.Name;
            }

            ret.Language = e.Language;

            return(ret);
        }
        private void Validate(BookProxy book)
        {
            if (book == null)
            {
                ThrowHttp.BadRequest(ErrorMessage.BOOK_REQUIRED);
            }

            if (string.IsNullOrWhiteSpace(book.Title))
            {
                ThrowHttp.BadRequest(ErrorMessage.TITLE_REQUIRED);
            }

            var dateError = _dateValidator.Validate(book.PublishDate);

            if (dateError != null)
            {
                ThrowHttp.BadRequest(dateError);
            }
        }
Exemple #6
0
        public async Task <BookEntity> AddBook(BookProxy payload)
        {
            var userId = Guid.NewGuid();

            var model = new BookEntity()
            {
                Id    = Guid.NewGuid(),
                Name  = payload.Name,
                Code  = payload.Code,
                Count = payload.Count
            };

            try
            {
                await _repository.AddAndSaveAsync(model);
            }
            catch (Exception ex)
            {
                return(model);
            }

            return(model);
        }
 private void MapToEntity(BookProxy proxy, Book entity)
 {
     entity.Title       = proxy.Title;
     entity.PublishDate = DateTime.Parse(proxy.PublishDate);
     entity.AuthorId    = proxy.AuthorId;
 }
Exemple #8
0
 public IActionResult Create(BookProxy payload)
 {
     return(RunDefault(() => Ok(_business.AddBook(payload))));
 }
        public async Task <ActionResult> UpdateAsync(int id, BookProxy book)
        {
            await _model.UpdateBookAsync(id, book);

            return(Ok());
        }
        public async Task <ActionResult> CreateAsync(BookProxy book)
        {
            var response = await _model.CreateBookAsync(book);

            return(StatusCode(HttpStatusCode.CREATED, response));
        }