API zum verleihen von Medien
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.

89 lines
3.5 KiB

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace BuecherwurmAPI
{
public class Program
{
public static void Main(string[] args)
{
if (!File.Exists("LongWormMemory.db"))
{
CreateNewEmptyDB();
MakeImports();
}
CreateHostBuilder(args).Build().Run();
}
private static void MakeImports()
{
var booksJson = File.ReadAllText("books.json");
//var magazinesJson = File.ReadAllText("magazines.json");
var catalogue = new Models.KatalogModel();
var items = new Models.ItemModel();
var config = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var importedBooks = JsonSerializer.Deserialize<List<Models.BookPost>>(booksJson, config);
//var importedMags = JsonSerializer.Deserialize<List<Models.BookPost>>(magazinesJson);
List<Models.IProductPost> imports = new List<Models.IProductPost>();
imports.AddRange(importedBooks);
foreach (var item in importedBooks)
{
item.LendType = LendTypeEnum.Virtual;
item.Category = Models.CategoryEnum.EBook;
imports.Add(item);
}
//imports.AddRange(importedMags);
foreach (var item in imports)
{
var id = catalogue.AddProduct(item);
items.NewItem(new Models.ItemPost{ BookId = (int)id});
if (item.LendType != LendTypeEnum.Virtual)
{
items.NewItem(new Models.ItemPost{ BookId = (int)id});
}
}
}
private static void CreateNewEmptyDB()
{
//File.Create("LongWormMemory.db");
File.WriteAllText("LongWormMemory.db", "");
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
var connection = new SqliteConnection(connectionBuilder.ConnectionString);
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = "CREATE TABLE Lends (Id INTEGER NOT NULL PRIMARY KEY, ItemId INTEGER NOT NULL, ReturnDate TEXT NOT NULL, Customer TEXT NOT NULL, Returned INTEGER NOT NULL)";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE Items (ItemId INTEGER NOT NULL PRIMARY KEY, BookId INTEGER NOT NULL)";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE Products (ProductId INTEGER PRIMARY KEY, Title TEXT, Author TEXT, Country TEXT, Link TEXT, Language TEXT, Pages INTEGER, Year INTEGER, Category INTEGER, ImageLink TEXT, LendTime INTEGER, LendType INTEGER, Run INTEGER, Audience TEXT, Topic TEXT)";
command.ExecuteNonQuery();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}