make json import

This commit is contained in:
nek0 2020-04-27 11:26:45 +02:00
parent 49fb7383a1
commit f87067ffb6
9 changed files with 1087 additions and 9 deletions

2
.vscode/launch.json vendored
View File

@ -11,7 +11,7 @@
"preLaunchTask": "build", "preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path. // If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/Bücherwurm.dll", "program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/Bücherwurm.dll",
"args": [], "args": ["books.json"],
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole", "console": "internalConsole",

View File

@ -216,5 +216,10 @@ namespace Bücherwurm
Console.WriteLine("Ausleihe nicht bekannt oder bereits urückgegeben."); Console.WriteLine("Ausleihe nicht bekannt oder bereits urückgegeben.");
} }
} }
public void MakeImport(string JsonText)
{
Catalogue.Import(JsonText);
}
} }
} }

26
Book.cs
View File

@ -1,23 +1,34 @@
using System.Text.Json.Serialization;
namespace Bücherwurm namespace Bücherwurm
{ {
class Book class Book
{ {
public int BookId {get;} [JsonIgnoreAttribute]
public int BookId {get; set;}
[JsonPropertyName("title")]
public string Title {get; set;} public string Title {get; set;}
[JsonPropertyName("author")]
public string Author {get; set;} public string Author {get; set;}
[JsonPropertyName("country")]
public string Country {get; set;} public string Country {get; set;}
[JsonPropertyName("imageLink")]
public string ImageLink {get; set;} public string ImageLink {get; set;}
[JsonPropertyName("link")]
public string Link {get; set;} public string Link {get; set;}
[JsonPropertyName("language")]
public string Language {get; set;} public string Language {get; set;}
[JsonPropertyName("pages")]
public int Pages {get; set;} public int Pages {get; set;}
[JsonPropertyName("year")]
public int Year {get; set;} public int Year {get; set;}
public Book(int Id, string Title, string Author, public Book(int Id, string Title, string Author,
@ -34,5 +45,18 @@ namespace Bücherwurm
this.Pages = Pages; this.Pages = Pages;
this.Year = Year; this.Year = Year;
} }
public Book()
{
}
public void OverwriteNullId(int Id)
{
if (BookId == 0)
{
BookId = Id;
}
}
} }
} }

32
BookImport.cs Normal file
View File

@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Bücherwurm
{
public class BookIdConvert : JsonConverter<int>
{
public int NextId {get; set;}
public override int Read(ref Utf8JsonReader reader,
System.Type typeToConvert,
JsonSerializerOptions options)
{
var ret = this.NextId;
this.NextId = this.NextId++;
return ret;
}
public override void Write(Utf8JsonWriter writer,
int value,
JsonSerializerOptions options)
{
}
public BookIdConvert(int InitId)
{
NextId = InitId;
}
}
}

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.Json;
namespace Bücherwurm namespace Bücherwurm
{ {
@ -12,12 +13,18 @@ namespace Bücherwurm
public Catalogue() public Catalogue()
{ {
Books = new List<Book>(); Books = new List<Book>();
NextId = 0; NextId = 1;
} }
public void Import(string JsonString) public void Import(string JsonString)
{ {
throw new NotImplementedException(); var intermediateBooks = JsonSerializer.Deserialize<List<Book>>(JsonString);
foreach (var ibook in intermediateBooks)
{
ibook.OverwriteNullId(NextId);
NextId = NextId + 1;
}
Books = intermediateBooks;
} }
public void Add(string Title, string Author, public void Add(string Title, string Author,

View File

@ -10,19 +10,19 @@ namespace Bücherwurm
public Inventory(){ public Inventory(){
InventoryList = new List<Item>(); InventoryList = new List<Item>();
NextId = 0; NextId = 1;
} }
public void Add(Book Book) public void Add(Book Book)
{ {
InventoryList.Add(new Item(NextId, Book.BookId)); InventoryList.Add(new Item(NextId, Book.BookId));
NextId = NextId++; NextId = NextId + 1;
} }
public void Add(int BookId) public void Add(int BookId)
{ {
InventoryList.Add(new Item(NextId, BookId)); InventoryList.Add(new Item(NextId, BookId));
NextId = NextId++; NextId = NextId + 1;
} }
public void Remove(int Id) public void Remove(int Id)

View File

@ -14,14 +14,14 @@ namespace Bücherwurm
{ {
Lendings = new List<Lending>(); Lendings = new List<Lending>();
ActiveLendings = new List<int>(); ActiveLendings = new List<int>();
NextId = 0; NextId = 1;
} }
public void Lend(int[] ItemIds, string Customer) public void Lend(int[] ItemIds, string Customer)
{ {
Lendings.Add(new Lending(NextId, ItemIds, Customer)); Lendings.Add(new Lending(NextId, ItemIds, Customer));
ActiveLendings.Add(NextId); ActiveLendings.Add(NextId);
NextId = NextId++; NextId = NextId + 1;
} }
public void Return(int LendID) public void Return(int LendID)

View File

@ -1,4 +1,5 @@
using System; using System;
using System.IO;
namespace Bücherwurm namespace Bücherwurm
{ {
@ -8,6 +9,13 @@ namespace Bücherwurm
public static void Main(string[] args) public static void Main(string[] args)
{ {
Admin = new Administration(); Admin = new Administration();
if (args.Length == 1)
{
Console.WriteLine("Versuche JSON Katalog von {0} zu importieren...", args[0]);
string jsonString = File.ReadAllText(args[0]);
Admin.MakeImport(jsonString);
Console.WriteLine("Erfolg!");
}
var Continue = true; var Continue = true;
do do
{ {

1002
books.json Normal file

File diff suppressed because it is too large Load Diff