Buecherwurm/Catalogue.cs

87 lines
2.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
}
2020-04-28 13:11:31 +00:00
public void ImportBooks(string JsonString)
2020-04-23 12:47:50 +00:00
{
2020-04-27 14:42:56 +00:00
var IntermediateBooks = JsonSerializer.Deserialize<List<Book>>(JsonString);
2020-04-27 14:14:20 +00:00
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-28 13:22:27 +00:00
Products.AddRange(new List<IProduct>(IntermediateBooks));
2020-04-28 13:58:27 +00:00
var eBooks = new List<EBook>();
foreach (var book in IntermediateBooks)
{
var ebook = new EBook(book);
ebook.OverwriteNullId(NextId);
eBooks.Add(ebook);
NextId += 1;
}
Products.AddRange(new List<IProduct>(eBooks));
}
2020-04-28 13:11:31 +00:00
2020-04-28 13:58:27 +00:00
public void ImportMagazines(string JsonString)
2020-04-28 13:11:31 +00:00
{
2020-04-28 14:07:03 +00:00
var IntermediateMagazines = JsonSerializer.Deserialize<List<Magazine>>(JsonString);
foreach (var Ibook in IntermediateMagazines)
2020-04-28 13:11:31 +00:00
{
Ibook.OverwriteNullId(NextId);
NextId += 1;
}
2020-04-28 14:07:03 +00:00
Products.AddRange(new List<IProduct>(IntermediateMagazines));
var ePapers = new List<EPaper>();
foreach (var mag in IntermediateMagazines)
{
var epaper = new EPaper(mag);
epaper.OverwriteNullId(NextId);
NextId += 1;
}
Products.AddRange(new List<IProduct>(ePapers));
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
2020-04-28 13:11:31 +00:00
public List<IProduct> GetProducts()
2020-04-24 13:54:17 +00:00
{
2020-04-28 13:11:31 +00:00
return Products;
2020-04-24 13:54:17 +00:00
}
2020-04-28 08:54:22 +00:00
public IProduct LookupProduct(int prodId)
2020-04-24 13:54:17 +00:00
{
2020-04-28 08:54:22 +00:00
return Products.Find(book => book.ProductId == prodId);
2020-04-24 13:54:17 +00:00
}
2020-04-23 12:47:50 +00:00
}
}