Esempio n. 1
0
        public async Task<HttpResponseMessage> Post(Author model)
        {
            var author = await _repository.AddAsync(model);

            var response = Request.CreateResponse(HttpStatusCode.Created, author);
            return response;
        }
Esempio n. 2
0
        public async Task<HttpResponseMessage> Put(int id, Author model)
        {
            model.Id = id;
            var author = await _repository.UpdateAsync(model);

            var response = Request.CreateResponse(HttpStatusCode.OK, author);
            return response;
        }
Esempio n. 3
0
        public void AddAuthorTest()
        {
            var author = new Author
            {
                FirstName = "Bill",
                LastName = "Gates"
            };

            var result = _db.AddAuthorAsync(author).Result;

            Assert.AreNotSame(author, result);
            Assert.IsTrue(result.Id > 0);
            Assert.AreEqual(author.FirstName, result.FirstName);
            Assert.AreEqual(author.LastName, result.LastName);
        }
Esempio n. 4
0
        public void CloneTest()
        {
            var author = new Author
            {
                Id = 5,
                FirstName = "Jeff",
                LastName = "Richter"
            };

            var result = (Author)author.Clone();

            Assert.AreEqual(author.Id, result.Id);
            Assert.AreEqual(author.FirstName, result.FirstName);
            Assert.AreEqual(author.LastName, result.LastName);
        }
Esempio n. 5
0
        public void AddBookWithAuthorIdsTest()
        {
            var author1 = new Author
            {
                FirstName = "Jeff",
                LastName = "Richter"
            };
            author1 = _db.AddAuthorAsync(author1).Result;

            var author2 = new Author
            {
                FirstName = "Bill",
                LastName = "Gates"
            };
            author2 = _db.AddAuthorAsync(author2).Result;

            var book = new Book
            {
                Title = "CLR via C#",
                Pages = 900,
                Publisher = "IT Books",
                Published = DateTime.Now.Date.AddYears(-5),
                Isbn = Guid.NewGuid().ToString(),
                Image = "https://www.google.ru/images/srpr/logo11w.png",
                Authors = new List<Author>
                {
                    new Author { Id = author1.Id },
                    new Author { Id = author2.Id }
                }
            };

            var result = _db.AddBookAsync(book).Result;

            Assert.AreEqual(2, result.Authors.Count);

            var auth1 = result.Authors.FirstOrDefault(a => a.Id == author1.Id);
            var auth2 = result.Authors.FirstOrDefault(a => a.Id == author2.Id);
            Assert.IsNotNull(auth1);
            Assert.IsNotNull(auth2);
            Assert.AreEqual(author1.FirstName, auth1.FirstName);
            Assert.AreEqual(author1.LastName, auth1.LastName);
            Assert.AreEqual(author2.FirstName, auth2.FirstName);
            Assert.AreEqual(author2.LastName, auth2.LastName);
        }
Esempio n. 6
0
        public void AuthorsByNameUpdateTest()
        {
            var author1 = new Author
            {
                FirstName = "Bill",
                LastName = "Gates"
            };

            var author2 = new Author
            {
                FirstName = "Elon",
                LastName = "Musk"
            };

            var author3 = new Author
            {
                FirstName = "Steve",
                LastName = "Jobs"
            };

            author1 = _db.AddAuthorAsync(author1).Result;
            _db.AddAuthorAsync(author2).Wait();
            _db.AddAuthorAsync(author3).Wait();

            author1.FirstName = "Satya";
            author1.LastName = "Nadella";
            _db.UpdateAuthorAsync(author1).Wait();

            var authors = _db.GetAuthorsByNameAsync().Result;

            Assert.AreEqual(3, authors.Count);
            Assert.AreEqual(author3.LastName, authors[0].LastName);
            Assert.AreEqual(author2.LastName, authors[1].LastName);
            Assert.AreEqual(author1.LastName, authors[2].LastName);
        }
Esempio n. 7
0
        public void AddBookWithAuthorTest()
        {
            var author = new Author
            {
                FirstName = "Jeff",
                LastName = "Richter"
            };
            author = _db.AddAuthorAsync(author).Result;

            var book = new Book
            {
                Title = "CLR via C#",
                Pages = 900,
                Publisher = "IT Books",
                Published = DateTime.Now.Date.AddYears(-5),
                Isbn = Guid.NewGuid().ToString(),
                Image = "https://www.google.ru/images/srpr/logo11w.png",
                Authors = new List<Author>
                {
                    author
                }
            };

            var result = _db.AddBookAsync(book).Result;

            Assert.AreEqual(1, result.Authors.Count);
            Assert.AreNotSame(author, result.Authors[0]);
            Assert.AreEqual(author.LastName, result.Authors[0].LastName);
        }
Esempio n. 8
0
        public void UpdateBookWithUnexistingAuthorTest()
        {
            var author = new Author
            {
                FirstName = "Jeff",
                LastName = "Richter"
            };
            author = _db.AddAuthorAsync(author).Result;

            var book = new Book
            {
                Title = "CLR via C#",
                Pages = 900,
                Publisher = "IT Books",
                Published = DateTime.Now.Date.AddYears(-5),
                Isbn = Guid.NewGuid().ToString(),
                Image = "https://www.google.ru/images/srpr/logo11w.png",
                Authors = new List<Author>
                {
                    author
                }
            };

            book = _db.AddBookAsync(book).Result;

            book.Authors[0].Id++;

            try
            {
                _db.UpdateBookAsync(book).Wait();
            }
            catch (Exception)
            {
                return;
            }

            Assert.Fail();
        }
Esempio n. 9
0
        public void UpdateBookTest()
        {
            var author1 = new Author
            {
                FirstName = "Jeff",
                LastName = "Richter"
            };
            author1 = _db.AddAuthorAsync(author1).Result;

            var author2 = new Author
            {
                FirstName = "Bill",
                LastName = "Gates"
            };
            author2 = _db.AddAuthorAsync(author2).Result;

            var book = new Book
            {
                Title = "CLR via C#",
                Pages = 900,
                Publisher = "IT Books",
                Published = DateTime.Now.Date.AddYears(-5),
                Isbn = Guid.NewGuid().ToString(),
                Image = "https://www.google.ru/images/srpr/logo11w.png",
                Authors = new List<Author>
                {
                    author1
                }
            };

            book = _db.AddBookAsync(book).Result;

            book.Title += "2";
            book.Pages += 20;
            book.Publisher += "2";
            book.Published = book.Published.HasValue ? book.Published.Value.AddYears(2) : (DateTime?) null;
            book.Isbn += "2";
            book.Image += "2";
            book.Authors = new List<Author> { author2 };

            var result = _db.UpdateBookAsync(book).Result;

            Assert.AreNotSame(book, result);
            Assert.AreEqual(book.Id, result.Id);
            Assert.AreEqual(book.Title, result.Title);
            Assert.AreEqual(book.Pages, result.Pages);
            Assert.AreEqual(book.Publisher, result.Publisher);
            Assert.AreEqual(book.Isbn, result.Isbn);
            Assert.AreEqual(book.Image, result.Image);
            Assert.AreEqual(1, result.Authors.Count);
            Assert.IsTrue(result.Authors.Any(a => a.Id == author2.Id ));
        }
Esempio n. 10
0
        public void UpdateAuthorWithUnexistingIdTest()
        {
            var author = new Author
            {
                FirstName = "Bill",
                LastName = "Gates"
            };

            var result = _db.AddAuthorAsync(author).Result;
            result.Id++;

            try
            {
                _db.UpdateAuthorAsync(result).Wait();
            }
            catch (Exception)
            {
                return;
            }

            Assert.Fail();
        }
Esempio n. 11
0
        public void UpdateAuthorTest()
        {
            var author = new Author
            {
                FirstName = "Bill",
                LastName = "Gates"
            };

            author = _db.AddAuthorAsync(author).Result;
            author.FirstName = "Elon";
            author.LastName = "Musk";

            var result = _db.UpdateAuthorAsync(author).Result;

            Assert.AreEqual(author.Id, result.Id);
            Assert.AreNotSame(author, result);
            Assert.AreEqual(author.FirstName, result.FirstName);
            Assert.AreEqual(author.LastName, result.LastName);
        }
Esempio n. 12
0
        public void DeleteAuthorTest()
        {
            var author1 = new Author
            {
                FirstName = "Bill",
                LastName = "Gates"
            };

            var author2 = new Author
            {
                FirstName = "Elon",
                LastName = "Musk"
            };

            author1 = _db.AddAuthorAsync(author1).Result;
            author2 = _db.AddAuthorAsync(author2).Result;

            _db.DeleteAuthorAsync(author1.Id).Wait();

            var authors = _db.GetAuthorsByNameAsync().Result;

            Assert.AreEqual(1, authors.Count);
            Assert.AreEqual(author2.Id, authors[0].Id);
        }
Esempio n. 13
0
        public async Task<Author> UpdateAuthorAsync(Author author)
        {
            try
            {
                await _lock.WaitAsync(OneManyMode.Exclusive);

                if (!_authors.ContainsKey(author.Id))
                {
                    throw new NotFoundException(string.Format("Could not find author by id: {0}", author.Id));
                }

                author = Copy(author);
                var original = _authors[author.Id];

                // updating author
                original.FirstName = author.FirstName;
                original.LastName = author.LastName;

                // updating indexes
                UpdateAuthorIndexes();

                return Copy(original);
            }
            finally
            {
                _lock.Release();
            }
        }
Esempio n. 14
0
        public void AuthorsByNameDeleteTest()
        {
            var author1 = new Author
            {
                FirstName = "Bill",
                LastName = "Gates"
            };

            var author2 = new Author
            {
                FirstName = "Elon",
                LastName = "Musk"
            };

            var author3 = new Author
            {
                FirstName = "Steve",
                LastName = "Jobs"
            };

            author1 = _db.AddAuthorAsync(author1).Result;
            author2 = _db.AddAuthorAsync(author2).Result;
            author3 = _db.AddAuthorAsync(author3).Result;

            _db.DeleteAuthorAsync(author3.Id).Wait();

            var authors = _db.GetAuthorsByNameAsync().Result;

            Assert.AreEqual(2, authors.Count);
            Assert.AreEqual(author1.LastName, authors[0].LastName);
            Assert.AreEqual(author2.LastName, authors[1].LastName);
        }
Esempio n. 15
0
 public Task<Author> AddAsync(Author author)
 {
     return _db.AddAuthorAsync(author);
 }
Esempio n. 16
0
        public void AddTwoAuthorsTest()
        {
            var author1 = new Author
            {
                FirstName = "Bill",
                LastName = "Gates"
            };

            var author2 = new Author
            {
                FirstName = "Steve",
                LastName = "Jobs"
            };

            var result1 = _db.AddAuthorAsync(author1).Result;
            var result2 = _db.AddAuthorAsync(author2).Result;

            Assert.IsTrue(result1.Id > 0);
            Assert.IsTrue(result2.Id > 0);
            Assert.AreNotEqual(result1.Id, result2.Id);
        }
Esempio n. 17
0
 private static Author Copy(Author author)
 {
     return author != null ? (Author) author.Clone() : null;
 }
Esempio n. 18
0
        private void AddAuthorBookRelationship(Book book, Author author)
        {
            book = _books[book.Id];
            author = _authors[author.Id];

            if (_authorBooks.ContainsKey(author))
            {
                _authorBooks[author].Add(book);
            }
            else
            {
                _authorBooks.Add(author, new List<Book> {book});
            }
        }
Esempio n. 19
0
        public void Initialize()
        {
            var author1 = new Author
            {
                FirstName = "Jeffrey",
                LastName = "Richter"
            };
            author1 = _db.AddAuthorAsync(author1).Result;

            var book1 = new Book
            {
                Title = "CLR via C# (4th Edition)",
                Pages = 896,
                Publisher = "Microsoft Press",
                Published = new DateTime(2012, 11, 25),
                Isbn = "978-0-735-66745-7",
                Image = "http://ecx.images-amazon.com/images/I/41zZ5aN3ypL.jpg",
                Authors = new List<Author>
                {
                    author1
                }
            };

            _db.AddBookAsync(book1).Wait();

            var author2 = new Author
            {
                FirstName = "Mark",
                LastName = "Lutz"
            };
            author2 = _db.AddAuthorAsync(author2).Result;

            var book2 = new Book
            {
                Title = "Learning Python, 5th Edition",
                Pages = 1600,
                Publisher = "O'Reilly Media",
                Published = new DateTime(2013, 7, 6),
                Isbn = "978-1-449-35573-9",
                Image = "http://ecx.images-amazon.com/images/I/515iBchIIzL._SX379_BO1,204,203,200_.jpg",
                Authors = new List<Author>
                {
                    author2
                }
            };

            _db.AddBookAsync(book2).Wait();

            var author3 = new Author
            {
                FirstName = "Mike",
                LastName = "Cantelon"
            };

            author3 = _db.AddAuthorAsync(author3).Result;

            var book3 = new Book
            {
                Title = "Node.js in Action",
                Pages = 416,
                Publisher = "Manning Publications",
                Published = new DateTime(2013, 11, 28),
                Isbn = "978-1-617-29057-2",
                Image = "http://ecx.images-amazon.com/images/I/51twwFigyiL._SX397_BO1,204,203,200_.jpg",
                Authors = new List<Author>
                {
                    author3
                }
            };

            _db.AddBookAsync(book3).Wait();
        }
Esempio n. 20
0
        private void DeleteAuthorBookRelationship(Book book, Author author)
        {
            book = _books[book.Id];
            author = _authors[author.Id];

            var authorBooks = _authorBooks[author];
            authorBooks.Remove(book);
            if (!authorBooks.Any())
            {
                // no more books
                _authorBooks.Remove(author);
            }
        }
Esempio n. 21
0
 public Task<Author> UpdateAsync(Author author)
 {
     return _db.UpdateAuthorAsync(author);
 }
Esempio n. 22
0
        public async Task<Author> AddAuthorAsync(Author author)
        {
            try
            {
                await _lock.WaitAsync(OneManyMode.Exclusive);

                author = Copy(author);

                // set id
                author.Id = _authorIds++;

                // add to storage
                _authors.Add(author.Id, author);

                // update indexes
                _authorsByName.Add(author);
                UpdateAuthorIndexes();
            }
            finally
            {
                _lock.Release();
            }

            return author;
        }