made imports, added magazine, DB self-init and fix

This commit is contained in:
nek0 2020-06-04 14:04:56 +02:00
parent 27392c9799
commit 27da067015
7 changed files with 240 additions and 97 deletions

View file

@ -18,40 +18,33 @@ namespace BuecherwurmAPI.Controllers
} }
// GET Katalog // GET Katalog
[HttpGet] [HttpGet]
public ActionResult<IEnumerable<Book>> GetAllBooks() public ActionResult<IEnumerable<IProduct>> GetAllProducts()
{ {
var books =_repository.GetAllBooks(); var Products =_repository.GetAllProducts();
return Ok(books); return Ok(Products);
} }
// POST Katalog // POST katalog/buch
[HttpPost] [HttpPost("buch")]
public ActionResult<Book> AddBook(BookPost book) public ActionResult<Book> AddProduct(BookPost book)
{ {
var id = _repository.AddBook(book); var id = _repository.AddProduct(book);
return Ok(new Book return Ok(_repository.GetProductById(id));
{
Name = book.Name,
Author= book.Author,
Country= book.Country,
Link= book.Link,
Language= book.Language,
Pages= book.Pages,
Year=book.Year,
ProductId = id,
Category= book.Category,
ImageLink =book.ImageLink,
LendTime =book.LendTime,
LendType = book.LendType
});
} }
// POST katalog/magazin
[HttpPost("magazin")]
public ActionResult<Magazin> AddProduct(MagazinPost book)
{
var id = _repository.AddProduct(book);
return Ok(_repository.GetProductById(id));
}
// GET katalog/{id} // GET katalog/{id}
[HttpGet("{id}")] [HttpGet("{id}")]
public ActionResult <Book> GetBookByID(long id) public ActionResult <IProduct> GetProductByID(long id)
{ {
var book = _repository.GetBookById(id); var book = _repository.GetProductById(id);
if (book != null) if (book != null)
{ {
return Ok(book); return Ok(book);
@ -62,22 +55,22 @@ namespace BuecherwurmAPI.Controllers
// PUT Katalog/{id} // PUT Katalog/{id}
[HttpPut("{id}")] [HttpPut("{id}")]
public ActionResult<Book> EditBook(long id, Book book) public ActionResult<IProduct> EditProduct(long id, IProduct book)
{ {
_repository.EditBook(id, book); _repository.EditProduct(id, book);
return Ok(book); return Ok(book);
} }
// DELETE katalog/{id} // DELETE katalog/{id}
[HttpDelete("{id}")] [HttpDelete("{id}")]
public ActionResult<Book> DeleteBook (long id) public ActionResult<IProduct> DeleteProduct (long id)
{ {
var book = _repository.GetBookById(id); var book = _repository.GetProductById(id);
if(book == null) if(book == null)
{ {
return NotFound(); return NotFound();
} }
_repository.DeleteBook(id); _repository.DeleteProduct(id);
return NoContent(); return NoContent();
} }

Binary file not shown.

View file

@ -2,26 +2,26 @@ using System.ComponentModel.DataAnnotations;
namespace BuecherwurmAPI.Models namespace BuecherwurmAPI.Models
{ {
public class Book public class Book : IProduct
{ {
public string Name {get; set;} [Key, Required]
public long ProductId { get; set; }
public string Title {get; set;}
public string Author {get; set;} public string Author {get; set;}
public string Country {get; set;} public string Country {get; set;}
public string Link {get; set;} public string Link {get; set;}
public string Language {get; set;} public string Language {get; set;}
public long Pages {get; set;} public long Pages {get; set;}
public long Year {get; set;} public long Year {get; set;}
[Key, Required]
public long ProductId { get; set; }
public CategoryEnum Category { get; set; } public CategoryEnum Category { get; set; }
public string ImageLink { get; set; } public string ImageLink { get; set; }
public long LendTime {get; set;} public long LendTime {get; set;}
public LendTypeEnum LendType {get; set;} public LendTypeEnum LendType {get; set;}
} }
public class BookPost public class BookPost : IProductPost
{ {
public string Name {get; set;} public string Title {get; set;}
public string Author {get; set;} public string Author {get; set;}
public string Country {get; set;} public string Country {get; set;}
public string Link {get; set;} public string Link {get; set;}
@ -32,6 +32,13 @@ namespace BuecherwurmAPI.Models
public string ImageLink { get; set; } public string ImageLink { get; set; }
public long LendTime {get; set;} public long LendTime {get; set;}
public LendTypeEnum LendType {get; set;} public LendTypeEnum LendType {get; set;}
public BookPost()
{
LendType = LendTypeEnum.Physical;
LendTime = 30;
Category = CategoryEnum.Book;
}
} }
} }

26
Models/IProduct.cs Normal file
View file

@ -0,0 +1,26 @@
namespace BuecherwurmAPI.Models
{
public interface IProductPost
{
CategoryEnum Category {get; set;}
long LendTime {get; set;}
string Title {get; set;}
LendTypeEnum LendType {get; set;}
}
public interface IProduct
{
long ProductId {get; set;}
CategoryEnum Category {get; set;}
long LendTime {get; set;}
string Title {get; set;}
LendTypeEnum LendType {get; set;}
}
}

View file

@ -21,9 +21,9 @@ namespace BuecherwurmAPI.Models
_dbConnection.Open(); _dbConnection.Open();
} }
public IEnumerable<Book> GetAllBooks() public IEnumerable<IProduct> GetAllProducts()
{ {
var books= new List<Book>(); var books= new List<IProduct>();
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
@ -31,10 +31,12 @@ namespace BuecherwurmAPI.Models
var dataReader = command.ExecuteReader(); var dataReader = command.ExecuteReader();
while (dataReader.Read()) while (dataReader.Read())
{
if (!dataReader["Author"].Equals(null))
{ {
books.Add(new Book books.Add(new Book
{ {
Name = (string) dataReader["Name"], Title = (string) dataReader["Title"],
Author = (string) dataReader["Author"], Author = (string) dataReader["Author"],
Country = (string) dataReader["Country"], Country = (string) dataReader["Country"],
Link = (string) dataReader["Link"], Link = (string) dataReader["Link"],
@ -42,43 +44,77 @@ namespace BuecherwurmAPI.Models
Pages = (long) dataReader["Pages"], Pages = (long) dataReader["Pages"],
Year = (long) dataReader["Year"], Year = (long) dataReader["Year"],
ProductId = (long) dataReader["ProductId"], ProductId = (long) dataReader["ProductId"],
Category = (CategoryEnum) dataReader["Category"], Category = (CategoryEnum) Enum.Parse(typeof(CategoryEnum), dataReader["Category"].ToString()),
ImageLink = (string) dataReader["ImageLink"], ImageLink = (string) dataReader["ImageLink"],
LendTime = (long) dataReader["LendTime"], LendTime = (long) dataReader["LendTime"],
LendType = (LendTypeEnum) dataReader["LendType"] LendType = (LendTypeEnum) Enum.Parse(typeof(LendTypeEnum), dataReader["LendType"].ToString())
}); });
} }
else
{
books.Add(new Magazin
{
Title = (string) dataReader["Title"],
LendTime = (long) dataReader["LendTime"],
LendType = (LendTypeEnum) Enum.Parse(typeof(LendTypeEnum), dataReader["LendType"].ToString()),
Category = (CategoryEnum) Enum.Parse(typeof(CategoryEnum), dataReader["Category"].ToString()),
Run = (long) dataReader["Run"],
Audience = (string) dataReader["Audience"],
Topic = (string) dataReader["Topic"]
});
}
}
} }
return books; return books;
} }
public Book GetBookById(long id) public IProduct GetProductById(long id)
{ {
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; command.CommandText = "SELECT * FROM \"Products\" WHERE ProductId = @id";
command.CommandText = @"SELECT * FROM Products WHERE ProductId = @id"; command.Parameters.Add(new SqliteParameter("@id", id));
var dataReader = command.ExecuteReader(); var dataReader = command.ExecuteReader();
var book = new Book IProduct book;
dataReader.Read();
if (dataReader["Author"].Equals(null))
{ {
Name = (string) dataReader["Name"], book = new Magazin
Author = (string) dataReader["Author"], {
Country = (string) dataReader["Country"], Title = (string)dataReader["Title"],
Link = (string) dataReader["Link"], LendTime = (long)dataReader["LendTime"],
Language = (string) dataReader["Language"], LendType = (LendTypeEnum) Enum.Parse(typeof(LendTypeEnum), dataReader["LendType"].ToString()),
Pages = (long) dataReader["Pages"], Category = (CategoryEnum) Enum.Parse(typeof(CategoryEnum), dataReader["Category"].ToString()),
Year = (long) dataReader["Year"], Run = (long)dataReader["Run"],
ProductId = (long) dataReader["ProductId"], Audience = (string)dataReader["Audience"],
Category = (CategoryEnum) dataReader["Category"], Topic = (string)dataReader["Topic"]
};
}
else
{
book = new Book
{
Title = (string)dataReader["Title"],
Author = (string)dataReader["Author"],
Country = (string)dataReader["Country"],
Link = (string)dataReader["Link"],
Language = (string)dataReader["Language"],
Pages = (long)dataReader["Pages"],
Year = (long)dataReader["Year"],
ProductId = (long)dataReader["ProductId"],
Category = (CategoryEnum) Enum.Parse(typeof(CategoryEnum), dataReader["Category"].ToString()),
ImageLink = (string) dataReader["ImageLink"], ImageLink = (string) dataReader["ImageLink"],
LendTime = (long) dataReader["LendTime"], LendTime = (long) dataReader["LendTime"],
LendType = (LendTypeEnum) dataReader["LendType"] LendType = (LendTypeEnum) Enum.Parse(typeof(LendTypeEnum), dataReader["LendType"].ToString())
}; };
}
return book; return book;
} }
} }
public void DeleteBook(long id) public void DeleteProduct(long id)
{ {
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
@ -88,25 +124,44 @@ namespace BuecherwurmAPI.Models
} }
} }
public long AddBook (BookPost book) public long AddProduct (IProductPost book)
{ {
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
// funktioniert das so? // funktioniert das so?
//muss ProduktId übergeben werden? //muss ProduktId übergeben werden? -> ProductId wird weiter hinten mit 'NULL' befüllt, damit die ID automatisch vergeben wird.
command.CommandText = @"INSERT INTO Products (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) VALUES (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, NULL, @Category, @ImageLink, @LendTime, @LendType )"; command.CommandText = @"INSERT INTO Products (ProductId, Title, Author, Country, Link, Language, Pages, Year, Category, ImageLink, LendTime, LendType, Run, Audience, Topic) VALUES (NULL, @Title, @Author, @Country, @Link, @Language, @Pages, @Year, @Category, @ImageLink, @LendTime, @LendType, @Run, @Audience, @Topic)";
command.Parameters.Add(new SqliteParameter("@Name", book.Name)); command.Parameters.Add(new SqliteParameter("@Title", book.Title));
command.Parameters.Add(new SqliteParameter("@Author", book.Author));
command.Parameters.Add(new SqliteParameter("@Country", book.Country));
command.Parameters.Add(new SqliteParameter("@Link", book.Link));
command.Parameters.Add(new SqliteParameter("@Language", book.Language));
command.Parameters.Add(new SqliteParameter("@Pages", book.Pages));
command.Parameters.Add(new SqliteParameter("@Year", book.Year));
command.Parameters.Add(new SqliteParameter("@Category", book.Category)); command.Parameters.Add(new SqliteParameter("@Category", book.Category));
command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink));
command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime)); command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime));
command.Parameters.Add(new SqliteParameter("@LendType", book.LendType)); command.Parameters.Add(new SqliteParameter("@LendType", book.LendType));
if (book.GetType() == typeof(BookPost))
{
command.Parameters.Add(new SqliteParameter("@Author", ((BookPost)book).Author));
command.Parameters.Add(new SqliteParameter("@Country", ((BookPost)book).Country));
command.Parameters.Add(new SqliteParameter("@Link", ((BookPost)book).Link));
command.Parameters.Add(new SqliteParameter("@Language", ((BookPost)book).Language));
command.Parameters.Add(new SqliteParameter("@Pages", ((BookPost)book).Pages));
command.Parameters.Add(new SqliteParameter("@Year", ((BookPost)book).Year));
command.Parameters.Add(new SqliteParameter("@ImageLink", ((BookPost)book).ImageLink));
command.Parameters.Add(new SqliteParameter("@Run", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Audience", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Topic", DBNull.Value));
}
else
{
command.Parameters.Add(new SqliteParameter("@Author", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Country", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Link", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Language", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Pages", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Year", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@ImageLink", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Run", ((MagazinPost)book).Run));
command.Parameters.Add(new SqliteParameter("@Audience", ((MagazinPost)book).Audience));
command.Parameters.Add(new SqliteParameter("@Topic", ((MagazinPost)book).Topic));
}
var success = command.ExecuteNonQuery(); var success = command.ExecuteNonQuery();
@ -120,25 +175,44 @@ namespace BuecherwurmAPI.Models
} }
} }
public long EditBook (long id, Book book) public long EditProduct (long id, IProduct book)
{ {
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
// funktioniert das so? // funktioniert das so?
command.Parameters.Add(new SqliteParameter("@id", id)); command.Parameters.Add(new SqliteParameter("@id", id));
command.CommandText = @"UPDATE Products SET (Name, Author, Country, Link, Language, Pages, Year, Category, ImageLink, LendTime, LendType) = (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @Category, @ImageLink, @LendTime, @LendType ) WHERE ProductId = @id"; command.CommandText = @"UPDATE Products SET (Title, Author, Country, Link, Language, Pages, Year, Category, ImageLink, LendTime, LendType, Run, Audience, Topic) = (@Title, @Author, @Country, @Link, @Language, @Pages, @Year, @Category, @ImageLink, @LendTime, @LendType, @Run, @Audience, @Topic) WHERE ProductId = @id";
command.Parameters.Add(new SqliteParameter("@Name", book.Name)); command.Parameters.Add(new SqliteParameter("@Title", book.Title));
command.Parameters.Add(new SqliteParameter("@Author", book.Author));
command.Parameters.Add(new SqliteParameter("@Country", book.Country));
command.Parameters.Add(new SqliteParameter("@Link", book.Link));
command.Parameters.Add(new SqliteParameter("@Language", book.Language));
command.Parameters.Add(new SqliteParameter("@Pages", book.Pages));
command.Parameters.Add(new SqliteParameter("@Year", book.Year));
command.Parameters.Add(new SqliteParameter("@Category", book.Category)); command.Parameters.Add(new SqliteParameter("@Category", book.Category));
command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink));
command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime)); command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime));
command.Parameters.Add(new SqliteParameter("@LendType", book.LendType)); command.Parameters.Add(new SqliteParameter("@LendType", book.LendType));
if (book.GetType() == typeof(Book))
{
command.Parameters.Add(new SqliteParameter("@Author", ((BookPost)book).Author));
command.Parameters.Add(new SqliteParameter("@Country", ((BookPost)book).Country));
command.Parameters.Add(new SqliteParameter("@Link", ((BookPost)book).Link));
command.Parameters.Add(new SqliteParameter("@Language", ((BookPost)book).Language));
command.Parameters.Add(new SqliteParameter("@Pages", ((BookPost)book).Pages));
command.Parameters.Add(new SqliteParameter("@Year", ((BookPost)book).Year));
command.Parameters.Add(new SqliteParameter("@ImageLink", ((BookPost)book).ImageLink));
command.Parameters.Add(new SqliteParameter("@Run", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Audience", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Topic", DBNull.Value));
}
else
{
command.Parameters.Add(new SqliteParameter("@Author", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Country", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Link", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Language", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Pages", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Year", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@ImageLink", DBNull.Value));
command.Parameters.Add(new SqliteParameter("@Run", ((MagazinPost)book).Run));
command.Parameters.Add(new SqliteParameter("@Audience", ((MagazinPost)book).Audience));
command.Parameters.Add(new SqliteParameter("@Topic", ((MagazinPost)book).Topic));
}
var success = command.ExecuteNonQuery(); var success = command.ExecuteNonQuery();

View file

@ -1,16 +1,36 @@
using System.ComponentModel.DataAnnotations;
namespace BuecherwurmAPI.Models namespace BuecherwurmAPI.Models
{ {
public class Magazin public class Magazin : IProduct
{ {
public int ProductId {get; set;} public long ProductId {get; set;}
public CategoryEnum Category {get; set;} public CategoryEnum Category {get; set;}
public int LendTime {get; set;} public long LendTime {get; set;}
public string Name {get; set;} public string Title {get; set;}
public string Run {get; set;} public long Run {get; set;}
public string Audience {get; set;} public string Audience {get; set;}
public string Topic {get; set;} public string Topic {get; set;}
public LendTypeEnum LendType {get; set;} public LendTypeEnum LendType {get; set;}
} }
public class MagazinPost : IProductPost
{
public CategoryEnum Category {get; set;}
public long LendTime {get; set;}
public string Title {get; set;}
public string Run {get; set;}
public string Audience {get; set;}
public string Topic {get; set;}
public LendTypeEnum LendType {get; set;}
public MagazinPost()
{
LendType = LendTypeEnum.Physical;
LendTime = 2;
Category = CategoryEnum.Magazine;
}
}
} }

View file

@ -8,6 +8,8 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace BuecherwurmAPI namespace BuecherwurmAPI
{ {
@ -25,12 +27,33 @@ namespace BuecherwurmAPI
private static void MakeImports() private static void MakeImports()
{ {
throw new NotImplementedException(); 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);
//imports.AddRange(importedMags);
var model = new Models.KatalogModel();
foreach (var item in imports)
{
model.AddProduct(item);
}
} }
private static void CreateNewEmptyDB() private static void CreateNewEmptyDB()
{ {
File.Create("LongWormMemory.db"); //File.Create("LongWormMemory.db");
File.WriteAllText("LongWormMemory.db", "");
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
var connection = new SqliteConnection(connectionBuilder.ConnectionString); var connection = new SqliteConnection(connectionBuilder.ConnectionString);
connection.Open(); connection.Open();
@ -40,7 +63,7 @@ namespace BuecherwurmAPI
command.ExecuteNonQuery(); command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE Items (ItemId INTEGER NOT NULL PRIMARY KEY, BookId INTEGER NOT NULL)"; command.CommandText = "CREATE TABLE Items (ItemId INTEGER NOT NULL PRIMARY KEY, BookId INTEGER NOT NULL)";
command.ExecuteNonQuery(); command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE Products (ProductId INTEGER PRIMARY KEY, Name TEXT, Author TEXT, Country TEXT, Link TEXT, Language TEXT, Pages INTEGER, Year INTEGER, Category INTEGER, ImageLink TEXT, LendTime INTEGER, LendType INTEGER)"; 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(); command.ExecuteNonQuery();
} }
} }