using System; using Model; namespace Stub; public class EmptyStubDataManager : IDataManager { protected readonly List books = new(); protected readonly List persons = new(); protected long bookId; protected long personId; public Task BorrowBook(Book book, Person person) { if(book == null || person == null) return Task.FromResult(false); var foundBook = GetBookById(book.Id).Result; var foundPerson = GetPersonById(person.Id).Result; if(foundBook == null || foundPerson == null) return Task.FromResult(false); if(foundBook.Borrower != null && !foundBook.Borrower.Equals(foundPerson)) { return Task.FromResult(false); } foundBook.Borrower = foundPerson; foundPerson.BorrowBook(foundBook); return Task.FromResult(true); } public Task CreateBook(string title, string author, string isbn) { Book book = new Book(bookId, title, author, isbn); books.Add(book); bookId++; return Task.FromResult(book); } public Task CreatePerson(string firstName, string lastName) { Person person = new Person(personId, firstName, lastName); persons.Add(person); personId++; return Task.FromResult(person); } public Task DeleteBook(long id) { Book? book = books.SingleOrDefault(b => b.Id == id); if(book == null) { return Task.FromResult(false); } book.Borrower?.ReturnBook(book); books.Remove(book); return Task.FromResult(true); } public Task DeletePerson(long id) { Person? person = persons.SingleOrDefault(p => p.Id == id); if(person == null) { return Task.FromResult(false); } foreach(var book in person.Books) { book.Borrower = null; } persons.Remove(person); return Task.FromResult(true); } public Task GetBookById(long id) { return Task.FromResult(books.SingleOrDefault(b => b.Id == id)); } public Task> GetBooks(int index, int count) { var foundBooks = books.Skip(index*count).Take(count); return Task.FromResult(foundBooks); } public Task> GetBooksBorrowedBy(Person? person, int index, int count) { if(person == null) return Task.FromResult(Enumerable.Empty()); var foundBooks = books.Where(b => person.Equals(b.Borrower)) .Skip(index*count).Take(count); return Task.FromResult(foundBooks); } public Task> GetBooksByAuthor(string? author, int index, int count) { if(string.IsNullOrWhiteSpace(author)) return Task.FromResult(Enumerable.Empty()); var foundBooks = books.Where(b => b.Author.Contains(author, StringComparison.InvariantCultureIgnoreCase)) .Skip(index*count).Take(count); return Task.FromResult(foundBooks); } public Task> GetBooksByIsbn(string? isbn, int index, int count) { if(string.IsNullOrWhiteSpace(isbn)) return Task.FromResult(Enumerable.Empty()); var foundBooks = books.Where(b => b.Isbn.Equals(isbn)) .Skip(index*count).Take(count); return Task.FromResult(foundBooks); } public Task> GetBooksByTitle(string? title, int index, int count) { if(string.IsNullOrWhiteSpace(title)) return Task.FromResult(Enumerable.Empty()); var foundBooks = books.Where(b => b.Title.Contains(title, StringComparison.InvariantCultureIgnoreCase)) .Skip(index*count).Take(count); return Task.FromResult(foundBooks); } public Task GetPersonById(long id) { return Task.FromResult(persons.SingleOrDefault(p => p.Id == id)); } public Task> GetPersons(int index, int count) { var foundPersons = persons.Skip(index*count).Take(count); return Task.FromResult(foundPersons); } public Task> GetPersonsByName(string? name, int index, int count) { if(string.IsNullOrWhiteSpace(name)) return Task.FromResult(Enumerable.Empty()); var foundPersons = persons.Where(b => b.FirstName.Contains(name, StringComparison.InvariantCultureIgnoreCase) || b.LastName.Contains(name, StringComparison.InvariantCultureIgnoreCase)) .Skip(index*count).Take(count); return Task.FromResult(foundPersons); } public Task ReturnBook(Book book, Person person) { var foundBook = GetBookById(book.Id).Result; var foundPerson = GetPersonById(person.Id).Result; if(foundBook == null || foundPerson == null) return Task.FromResult(false); if(!foundPerson.Books.Contains(foundBook) || !foundPerson.Equals(foundBook.Borrower)) return Task.FromResult(false); foundPerson.ReturnBook(foundBook); foundBook.Borrower = null; return Task.FromResult(true); } public Task UpdateBook(long id, Book book) { var foundBook = GetBookById(id).Result; if(foundBook == null) return Task.FromResult((Book?)null); foundBook.Author = book.Author; foundBook.Isbn = book.Isbn; foundBook.Title = book.Title; return Task.FromResult((Book?)foundBook); } public Task UpdatePerson(long id, Person person) { var foundPerson = GetPersonById(id).Result; if(foundPerson == null) return Task.FromResult((Person?)null); foundPerson.FirstName = person.FirstName; foundPerson.LastName = person.LastName; return Task.FromResult((Person?)foundPerson); } }