Buecherwurm/Catalogue.cs

59 lines
1.5 KiB
C#
Raw Normal View History

2020-04-23 13:44:38 +00:00
using System;
2020-04-23 12:47:50 +00:00
using System.Collections.Generic;
2020-04-27 14:14:20 +00:00
using System.Linq;
2020-04-27 09:26:45 +00:00
using System.Text.Json;
2020-04-23 12:47:50 +00:00
namespace Bücherwurm
{
class Catalogue
{
2020-04-27 14:14:20 +00:00
private List<IProduct> Products {get; set;}
2020-04-23 12:47:50 +00:00
2020-04-23 13:44:38 +00:00
private int NextId {get; set;}
2020-04-23 12:47:50 +00:00
public Catalogue()
{
2020-04-27 14:14:20 +00:00
Products = new List<IProduct>();
2020-04-27 09:26:45 +00:00
NextId = 1;
2020-04-23 12:47:50 +00:00
}
public void Import(string JsonString)
{
2020-04-27 14:14:20 +00:00
var IntermediateBooks = JsonSerializer.Deserialize<List<IProduct>>(JsonString);
foreach (var Ibook in IntermediateBooks)
2020-04-27 09:26:45 +00:00
{
2020-04-27 14:14:20 +00:00
Ibook.OverwriteNullId(NextId);
NextId += 1;
2020-04-27 09:26:45 +00:00
}
2020-04-27 14:14:20 +00:00
Products = IntermediateBooks;
2020-04-23 12:47:50 +00:00
}
2020-04-23 13:44:38 +00:00
2020-04-27 14:14:20 +00:00
public void AddBook(string Title, string Author,
2020-04-23 13:44:38 +00:00
string Country, string ILink, string Link,
string Language, int Pages, int Year)
{
2020-04-27 14:14:20 +00:00
Products.Add(new Book(NextId, Title, Author, Country, ILink, Link, Language, Pages, Year));
2020-04-23 13:44:38 +00:00
NextId = NextId++;
}
2020-04-27 14:14:20 +00:00
public void Add(IProduct product)
2020-04-23 13:44:38 +00:00
{
2020-04-27 14:14:20 +00:00
Products.Add(product);
}
public void Remove(int bookID)
{
Products.RemoveAll(book => book.ProductId == bookID);
2020-04-23 13:44:38 +00:00
}
2020-04-24 13:54:17 +00:00
public List<Book> GetBooks()
{
2020-04-27 14:14:20 +00:00
return Products.FindAll(prod => prod.Category == "book").Cast<Book>().ToList();
2020-04-24 13:54:17 +00:00
}
2020-04-27 14:14:20 +00:00
public Book LookupBook(int bookId)
2020-04-24 13:54:17 +00:00
{
2020-04-27 14:14:20 +00:00
return (Book)Products.Find(book => book.ProductId == bookId && book.Category == "book");
2020-04-24 13:54:17 +00:00
}
2020-04-23 12:47:50 +00:00
}
}