You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
6.1 KiB
187 lines
6.1 KiB
using System;
|
|
using Model;
|
|
|
|
namespace Stub;
|
|
|
|
public class EmptyStubDataManager : IDataManager
|
|
{
|
|
protected readonly List<Book> books = new();
|
|
|
|
protected readonly List<Person> persons = new();
|
|
|
|
protected long bookId;
|
|
|
|
protected long personId;
|
|
|
|
public Task<bool> 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<Book> 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<Person> CreatePerson(string firstName, string lastName)
|
|
{
|
|
Person person = new Person(personId, firstName, lastName);
|
|
persons.Add(person);
|
|
personId++;
|
|
return Task.FromResult(person);
|
|
}
|
|
|
|
public Task<bool> 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<bool> 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<Book?> GetBookById(long id)
|
|
{
|
|
return Task.FromResult(books.SingleOrDefault(b => b.Id == id));
|
|
}
|
|
|
|
public Task<IEnumerable<Book>> GetBooks(int index, int count)
|
|
{
|
|
var foundBooks = books.Skip(index*count).Take(count);
|
|
return Task.FromResult(foundBooks);
|
|
}
|
|
|
|
public Task<IEnumerable<Book>> GetBooksBorrowedBy(Person? person, int index, int count)
|
|
{
|
|
if(person == null) return Task.FromResult(Enumerable.Empty<Book>());
|
|
|
|
var foundBooks = books.Where(b => person.Equals(b.Borrower))
|
|
.Skip(index*count).Take(count);
|
|
return Task.FromResult(foundBooks);
|
|
}
|
|
|
|
public Task<IEnumerable<Book>> GetBooksByAuthor(string? author, int index, int count)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(author)) return Task.FromResult(Enumerable.Empty<Book>());
|
|
|
|
var foundBooks = books.Where(b => b.Author.Contains(author, StringComparison.InvariantCultureIgnoreCase))
|
|
.Skip(index*count).Take(count);
|
|
return Task.FromResult(foundBooks);
|
|
}
|
|
|
|
public Task<IEnumerable<Book>> GetBooksByIsbn(string? isbn, int index, int count)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(isbn)) return Task.FromResult(Enumerable.Empty<Book>());
|
|
|
|
var foundBooks = books.Where(b => b.Isbn.Equals(isbn))
|
|
.Skip(index*count).Take(count);
|
|
return Task.FromResult(foundBooks);
|
|
}
|
|
|
|
public Task<IEnumerable<Book>> GetBooksByTitle(string? title, int index, int count)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(title)) return Task.FromResult(Enumerable.Empty<Book>());
|
|
|
|
var foundBooks = books.Where(b => b.Title.Contains(title, StringComparison.InvariantCultureIgnoreCase))
|
|
.Skip(index*count).Take(count);
|
|
return Task.FromResult(foundBooks);
|
|
}
|
|
|
|
public Task<Person?> GetPersonById(long id)
|
|
{
|
|
return Task.FromResult(persons.SingleOrDefault(p => p.Id == id));
|
|
}
|
|
|
|
public Task<IEnumerable<Person>> GetPersons(int index, int count)
|
|
{
|
|
var foundPersons = persons.Skip(index*count).Take(count);
|
|
return Task.FromResult(foundPersons);
|
|
}
|
|
|
|
public Task<IEnumerable<Person>> GetPersonsByName(string? name, int index, int count)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(name)) return Task.FromResult(Enumerable.Empty<Person>());
|
|
|
|
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<bool> 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<Book?> 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<Person?> 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);
|
|
}
|
|
}
|