Browse Source

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

Amedeo
nek0 11 months ago
parent
commit
27da067015
  1. 51
      Controllers/KatalogController.cs
  2. BIN
      LongWormMemory.db
  3. 19
      Models/Book.cs
  4. 26
      Models/IProduct.cs
  5. 186
      Models/KatalogModel.cs
  6. 28
      Models/Magazin.cs
  7. 29
      Program.cs

51
Controllers/KatalogController.cs

@ -18,40 +18,33 @@ namespace BuecherwurmAPI.Controllers
}
// GET Katalog
[HttpGet]
public ActionResult<IEnumerable<Book>> GetAllBooks()
public ActionResult<IEnumerable<IProduct>> GetAllProducts()
{
var books =_repository.GetAllBooks();
return Ok(books);
var Products =_repository.GetAllProducts();
return Ok(Products);
}
// POST Katalog
[HttpPost]
public ActionResult<Book> AddBook(BookPost book)
// POST katalog/buch
[HttpPost("buch")]
public ActionResult<Book> AddProduct(BookPost book)
{
var id = _repository.AddBook(book);
return Ok(new Book
{
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
});
var id = _repository.AddProduct(book);
return Ok(_repository.GetProductById(id));
}
// POST katalog/magazin
[HttpPost("magazin")]
public ActionResult<Magazin> AddProduct(MagazinPost book)
{
var id = _repository.AddProduct(book);
return Ok(_repository.GetProductById(id));
}
// GET katalog/{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)
{
return Ok(book);
@ -62,22 +55,22 @@ namespace BuecherwurmAPI.Controllers
// PUT Katalog/{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);
}
// DELETE katalog/{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)
{
return NotFound();
}
_repository.DeleteBook(id);
_repository.DeleteProduct(id);
return NoContent();
}

BIN
LongWormMemory.db

19
Models/Book.cs

@ -2,26 +2,26 @@ using System.ComponentModel.DataAnnotations;
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 Country {get; set;}
public string Link {get; set;}
public string Language {get; set;}
public long Pages {get; set;}
public long Year {get; set;}
[Key, Required]
public long ProductId { get; set; }
public CategoryEnum Category { get; set; }
public string ImageLink { get; set; }
public long LendTime {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 Country {get; set;}
public string Link {get; set;}
@ -32,6 +32,13 @@ namespace BuecherwurmAPI.Models
public string ImageLink { get; set; }
public long LendTime {get; set;}
public LendTypeEnum LendType {get; set;}
public BookPost()
{
LendType = LendTypeEnum.Physical;
LendTime = 30;
Category = CategoryEnum.Book;
}
}
}

26
Models/IProduct.cs

@ -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;}
}
}

186
Models/KatalogModel.cs

@ -21,9 +21,9 @@ namespace BuecherwurmAPI.Models
_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())
{
@ -31,54 +31,90 @@ namespace BuecherwurmAPI.Models
var dataReader = command.ExecuteReader();
while (dataReader.Read())
{
books.Add(new Book
{
if (!dataReader["Author"].Equals(null))
{
Name = (string) dataReader["Name"],
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) dataReader["Category"],
ImageLink = (string) dataReader["ImageLink"],
LendTime = (long) dataReader["LendTime"],
LendType = (LendTypeEnum) dataReader["LendType"]
});
books.Add(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"],
LendTime = (long) dataReader["LendTime"],
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;
}
public Book GetBookById(long id)
public IProduct GetProductById(long id)
{
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 book = new Book
IProduct book;
dataReader.Read();
if (dataReader["Author"].Equals(null))
{
book = 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"]
};
}
else
{
book = new Book
{
Name = (string) dataReader["Name"],
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) dataReader["Category"],
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"],
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())
{
@ -88,25 +124,44 @@ namespace BuecherwurmAPI.Models
}
}
public long AddBook (BookPost book)
public long AddProduct (IProductPost book)
{
using (var command = _dbConnection.CreateCommand())
{
// funktioniert das so?
//muss ProduktId übergeben werden?
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.Parameters.Add(new SqliteParameter("@Name", book.Name));
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));
//muss ProduktId übergeben werden? -> ProductId wird weiter hinten mit 'NULL' befüllt, damit die ID automatisch vergeben wird.
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("@Title", book.Title));
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("@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();
@ -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())
{
// funktioniert das so?
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.Parameters.Add(new SqliteParameter("@Name", book.Name));
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.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("@Title", book.Title));
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("@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();

28
Models/Magazin.cs

@ -1,15 +1,35 @@
using System.ComponentModel.DataAnnotations;
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 int LendTime {get; set;}
public string Name {get; set;}
public long LendTime {get; set;}
public string Title {get; set;}
public long Run {get; set;}
public string Audience {get; set;}
public string Topic {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;
}
}
}

29
Program.cs

@ -8,6 +8,8 @@ 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
{
@ -25,12 +27,33 @@ namespace BuecherwurmAPI
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()
{
File.Create("LongWormMemory.db");
//File.Create("LongWormMemory.db");
File.WriteAllText("LongWormMemory.db", "");
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
var connection = new SqliteConnection(connectionBuilder.ConnectionString);
connection.Open();
@ -40,7 +63,7 @@ namespace BuecherwurmAPI
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, 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();
}
}

Loading…
Cancel
Save