From 1637f45d0e793aa4e188353ce1618101c6a52f38 Mon Sep 17 00:00:00 2001 From: Naumann Date: Tue, 2 Jun 2020 12:55:17 +0200 Subject: [PATCH] Added KatalogModel --- Controllers/KatalogController.cs | 10 ++-- Models/Book.cs | 8 +-- Models/IBookRepo.cs | 6 ++- Models/KatalogModel.cs | 93 ++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 Models/KatalogModel.cs diff --git a/Controllers/KatalogController.cs b/Controllers/KatalogController.cs index 6bb34e4..d7c4d52 100644 --- a/Controllers/KatalogController.cs +++ b/Controllers/KatalogController.cs @@ -26,7 +26,7 @@ namespace BuecherwurmAPI.Controllers // POST Katalog [HttpPost] - public ActionResult> NeuesBuch(Book book) + public ActionResult> AddBook(Book book) { return Ok(new Book @@ -49,7 +49,7 @@ namespace BuecherwurmAPI.Controllers // GET katalog/{id} [HttpGet("{id}", Name ="GetBookByID")] - public ActionResult > GetBookByID(int id) + public ActionResult > GetBookByID(long id) { var book = _repository.GetBookById(id); if (book != null) @@ -62,7 +62,7 @@ namespace BuecherwurmAPI.Controllers // PUT Katalog/{id} [HttpPut("id")] - public ActionResult> BuchBearbeiten(Book book) + public ActionResult> EditBook(Book book) { return Ok(new Book { @@ -83,14 +83,14 @@ namespace BuecherwurmAPI.Controllers // DELETE katalog/{id} [HttpDelete("id")] - public ActionResult> BuchEntfernen (int id) + public ActionResult> DeleteBook (long id) { var book = _repository.GetBookById(id); if(book == null) { return NotFound(); } - _repository.BuchEntfernen(book); + _repository.DeleteBook(book); return NoContent(); } diff --git a/Models/Book.cs b/Models/Book.cs index 5304bdc..bc90fd5 100644 --- a/Models/Book.cs +++ b/Models/Book.cs @@ -9,14 +9,14 @@ namespace BuecherwurmAPI.Models public string Country {get; set;} public string Link {get; set;} public string Language {get; set;} - public int Pages {get; set;} - public int Year {get; set;} + public long Pages {get; set;} + public long Year {get; set;} [Key] [Required] - public int ProductId { get; set; } + public long ProductId { get; set; } public CategoryEnum Category { get; set; } public string ImageLink { get; set; } - public int LendTime {get; set;} + public long LendTime {get; set;} public LendTypeEnum LendType {get; set;} } diff --git a/Models/IBookRepo.cs b/Models/IBookRepo.cs index 30fea50..9d4aca2 100644 --- a/Models/IBookRepo.cs +++ b/Models/IBookRepo.cs @@ -6,7 +6,9 @@ namespace BuecherwurmAPI.Models public interface IBookRepo { IEnumerable GetAllBooks(); - Book GetBookById(int id); - void BuchEntfernen(Book book); + Book GetBookById(long id); + void DeleteBook(Book book); + void AddBook (Book book); + void EditBook (Book book); } } \ No newline at end of file diff --git a/Models/KatalogModel.cs b/Models/KatalogModel.cs new file mode 100644 index 0000000..6fcb3c1 --- /dev/null +++ b/Models/KatalogModel.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using BuecherwurmAPI.Models; +using Microsoft.Data.Sqlite; +using Microsoft.VisualBasic.CompilerServices; + +namespace BuecherwurmAPI.Models +{ + internal class Katalog : IBookRepo + { + private SqliteConnection _dbConnection; + + public Katalog() + { + var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; + _dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); + _dbConnection.Open(); + } + + public IEnumerable GetAllBooks() + { + var books= new List(); + + using (var command = _dbConnection.CreateCommand()) + { + command.CommandText = @"SELECT * FROM Books"; + var dataReader = command.ExecuteReader(); + + while (dataReader.Read()) + { + books.Add(new Book + { + Name = (string) dataReader["Books"], + 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"] + }); + } + } + return books; + } + + public Book GetBookById(long id) + { + using (var command = _dbConnection.CreateCommand()) + { + command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; + command.CommandText = @"SELECT * FROM Books WHERE ProductId = @id"; + var dataReader = command.ExecuteReader(); + + var book = new Book + { + Name = (string) dataReader["Books"], + 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"] + }; + return book; + } + } + public void DeleteBook(Book book) + { + + } + + public void AddBook (Book book) + { + + } + + public void EditBook (Book book) + { + + } + + } +} \ No newline at end of file