From 7bdb85d8427c186916d02fd49170f1b05c0be759 Mon Sep 17 00:00:00 2001 From: David Renner Date: Tue, 2 Jun 2020 14:23:21 +0200 Subject: [PATCH 1/7] update Item Models und Controller --- ...nventarController.cs => ItemController.cs} | 12 +- DTOs/ItemRead.cs | 9 ++ Models/ItemModel.cs | 112 ++++++++++++++++++ Models/Tables.cs | 1 + 4 files changed, 129 insertions(+), 5 deletions(-) rename Controllers/{InventarController.cs => ItemController.cs} (83%) create mode 100644 DTOs/ItemRead.cs create mode 100644 Models/ItemModel.cs diff --git a/Controllers/InventarController.cs b/Controllers/ItemController.cs similarity index 83% rename from Controllers/InventarController.cs rename to Controllers/ItemController.cs index 5e8870d..7e400b1 100644 --- a/Controllers/InventarController.cs +++ b/Controllers/ItemController.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using BuecherwurmAPI.Models; using Microsoft.AspNetCore.Mvc; using System.Linq; +using Microsoft.Data.Sqlite; //using Microsoft.EntityFrameworkCore; @@ -9,11 +10,11 @@ namespace BuecherwurmAPI.Controllers { [Route("inventar")] [ApiController] - public class InventarController : ControllerBase + public class ItemController : ControllerBase { private readonly IItemRepo _repository; - public InventarController(IItemRepo repository) + public ItemController(IItemRepo repository) { _repository = repository; } @@ -27,12 +28,13 @@ namespace BuecherwurmAPI.Controllers // POST Inventar [HttpPost] - public ActionResult> NewItem(Item item) + public ActionResult> NewItem(int bookId) { return Ok(new Item { - Id = item.Id, - BookId = item.BookId, + + ItemId = itemId, + BookId = bookId }); } diff --git a/DTOs/ItemRead.cs b/DTOs/ItemRead.cs new file mode 100644 index 0000000..e70a016 --- /dev/null +++ b/DTOs/ItemRead.cs @@ -0,0 +1,9 @@ +using System; + +namespace BuecherwurmAPI.DTOs +{ + public class ItemReadDTO + { + public int BookId { get; set;} + } +} \ No newline at end of file diff --git a/Models/ItemModel.cs b/Models/ItemModel.cs new file mode 100644 index 0000000..ff6d83e --- /dev/null +++ b/Models/ItemModel.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using BuecherwurmAPI.Models; +using Microsoft.Data.Sqlite; +using Microsoft.VisualBasic.CompilerServices; + +namespace BuecherwurmAPI.Models +{ + internal class ItemModel : IItemRepo + { + private SqliteConnection _dbConnection; + + public ItemModel() + { + var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; + _dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); + _dbConnection.Open(); + } + + public bool IdExits(string table, long id) + { + using (var command = _dbConnection.CreateCommand()) + { + command.Parameters.Add(new SqliteParameter("@Id", SqliteType.Integer)).Value = id; + + // Certain parts of the query can't be filled with variables for security reasons. + switch (table) + { + case Tables.Table.Items: + command.CommandText = @"SELECT EXISTS(SELECT 0 FROM Items WHERE Id = @Id)"; + break; + } + + var dr = command.ExecuteReader(); + + long result = 0; + while (dr.Read()) + { + result = (long) dr[0]; + } + return result != 0; + } + } + + public IEnumerable GetAllItems() + { + var items = new List(); + + // using automatically disposes the command after completion + using (var command = _dbConnection.CreateCommand()) + { + command.CommandText = @"SELECT * FROM Items"; + var dataReader = command.ExecuteReader(); + + while (dataReader.Read()) + { + var returned = (long) dataReader["Returned"] == 1; + + items.Add(new Item + { + ItemId = (int) dataReader["ItemId"], + BookId = (int) dataReader["BookId"] + }); + } + } + return items; + } + + public Item GetItemById(int id) + { + using (var command = _dbConnection.CreateCommand()) + { + command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; + command.CommandText = @"SELECT * FROM Items WHERE Id = @id"; + var dataReader = command.ExecuteReader(); + + while (dataReader.Read()) + { + var returned = (long) dataReader["Returned"] == 1; + + var item = new Item + { + ItemId = (int) dataReader["ItemId"], + BookId = (int) dataReader["BookId"] + }; + return item; + } + } + return null; + } + public Item NewItem(int bookId) + { + using (var command = _dbConnection.CreateCommand()) + { + //hier soll jetz der NewItem Command folgen. ein POST mit EingabeInfo {id} = "BookId" + command.CommandText = @"INSERT INTO Item(ItemId,BookId) OUTPUT INSERTED.ID VALUES (@ItemID,@BookId"; + return item; + } + } + + public void DeleteItem(Item item) + { + using (var command = _dbConnection.CreateCommand()) + { + command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = item; + //hier soll jetz der NewItem Command folgen. ein POST mit EingabeInfo {id} = "BookId" + + command.CommandText = @"SELECT * FROM Items WHERE ItemId = @id"; + } + } + } +} \ No newline at end of file diff --git a/Models/Tables.cs b/Models/Tables.cs index f3143cb..2bc61c3 100644 --- a/Models/Tables.cs +++ b/Models/Tables.cs @@ -6,6 +6,7 @@ namespace BuecherwurmAPI.Models { public const string Lends = "Lends"; public const string Katalog = "Katalog"; + public const string Items = "Items"; } } } \ No newline at end of file From eba2ba64fc2535d5df3268171e74694e450bb3ef Mon Sep 17 00:00:00 2001 From: David Renner Date: Tue, 2 Jun 2020 14:24:26 +0200 Subject: [PATCH 2/7] fast vergessen --- Models/Item.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Models/Item.cs b/Models/Item.cs index c6e6012..b0ba3ce 100644 --- a/Models/Item.cs +++ b/Models/Item.cs @@ -4,9 +4,8 @@ namespace BuecherwurmAPI.Models { public class Item { - [Key] - [Required] - public int Id { get; set; } + [Key, Required] + public int ItemId { get; set; } [Required] public int BookId { get; set; } } From bd0f852d9be96c1282702fefa0da0cea9aad98ce Mon Sep 17 00:00:00 2001 From: David Renner Date: Tue, 2 Jun 2020 16:01:17 +0200 Subject: [PATCH 3/7] =?UTF-8?q?zwischenstand=20-=20nicht=20funktionst?= =?UTF-8?q?=C3=BCchtig!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/ItemController.cs | 19 ++++++++++--------- Models/IItemRepo.cs | 4 ++-- Models/ItemModel.cs | 11 +++++++---- Models/ItemPost.cs | 11 +++++++++++ 4 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 Models/ItemPost.cs diff --git a/Controllers/ItemController.cs b/Controllers/ItemController.cs index 7e400b1..50618a5 100644 --- a/Controllers/ItemController.cs +++ b/Controllers/ItemController.cs @@ -28,7 +28,7 @@ namespace BuecherwurmAPI.Controllers // POST Inventar [HttpPost] - public ActionResult> NewItem(int bookId) + public ActionResult> NewItem(ItemPost item) { return Ok(new Item { @@ -40,7 +40,7 @@ namespace BuecherwurmAPI.Controllers // GET Inventar/{id} - [HttpGet("{id}", Name = "GetItemByID")] + [HttpGet("{itemId}")] public ActionResult> GetItemByID(int id) { var item = _repository.GetItemById(id); @@ -54,14 +54,15 @@ namespace BuecherwurmAPI.Controllers // DELETE inventory/{id} - [HttpDelete("id")] - public ActionResult> DeleteItem(int id) + + [HttpDelete("itemId")] + public ActionResult> DeleteItem(int itemId) { - var item = _repository.GetItemById(id); - if (item == null) - { - return NotFound(); - } + var item = _repository.GetItemById(itemId); + if(item == null) + { + return NotFound(); + } _repository.DeleteItem(item); return NoContent(); } diff --git a/Models/IItemRepo.cs b/Models/IItemRepo.cs index 46d7f62..f8a0b04 100644 --- a/Models/IItemRepo.cs +++ b/Models/IItemRepo.cs @@ -6,8 +6,8 @@ namespace BuecherwurmAPI.Models public interface IItemRepo { IEnumerable GetAllItems(); - Item GetItemById(int id); - void NewItem(Item item); + Item GetItemById(int itemId); + void NewItem(ItemPost item); void DeleteItem(Item item); } } \ No newline at end of file diff --git a/Models/ItemModel.cs b/Models/ItemModel.cs index ff6d83e..5ae273d 100644 --- a/Models/ItemModel.cs +++ b/Models/ItemModel.cs @@ -88,21 +88,24 @@ namespace BuecherwurmAPI.Models } return null; } - public Item NewItem(int bookId) + public Item NewItem(ItemPost itemPost) { using (var command = _dbConnection.CreateCommand()) { //hier soll jetz der NewItem Command folgen. ein POST mit EingabeInfo {id} = "BookId" - command.CommandText = @"INSERT INTO Item(ItemId,BookId) OUTPUT INSERTED.ID VALUES (@ItemID,@BookId"; + command.CommandText = "INSERT INTO Item(BOOKID) VALUES (@item)"; + command.CommandType = System.Data.CommandType.Text; + command.Parameters.Add(new SqliteParameter("@ITEMID", itemPost) + item; return item; } } - public void DeleteItem(Item item) + public void DeleteItem(int itemId) { using (var command = _dbConnection.CreateCommand()) { - command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = item; + command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = itemId; //hier soll jetz der NewItem Command folgen. ein POST mit EingabeInfo {id} = "BookId" command.CommandText = @"SELECT * FROM Items WHERE ItemId = @id"; diff --git a/Models/ItemPost.cs b/Models/ItemPost.cs new file mode 100644 index 0000000..981c119 --- /dev/null +++ b/Models/ItemPost.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations; + +namespace BuecherwurmAPI.Models +{ + public class ItemPost + { + [Required] + public int BookId { get; set; } + } + +} \ No newline at end of file From 098c780366c3b4aa4664b11d7c19aa1ec53041c5 Mon Sep 17 00:00:00 2001 From: David Renner Date: Wed, 3 Jun 2020 08:26:54 +0200 Subject: [PATCH 4/7] =?UTF-8?q?Funktionen=20zur=20"Item"=20Gruppe=20hinzug?= =?UTF-8?q?ef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/ItemController.cs | 14 +++++-------- Models/IItemRepo.cs | 6 +++--- Models/Item.cs | 4 ++-- Models/ItemModel.cs | 37 +++++++++++++++++++++-------------- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/Controllers/ItemController.cs b/Controllers/ItemController.cs index 50618a5..c85da0f 100644 --- a/Controllers/ItemController.cs +++ b/Controllers/ItemController.cs @@ -30,18 +30,14 @@ namespace BuecherwurmAPI.Controllers [HttpPost] public ActionResult> NewItem(ItemPost item) { - return Ok(new Item - { - - ItemId = itemId, - BookId = bookId - }); + var newItem = _repository.NewItem(item); + return Ok(newItem); } // GET Inventar/{id} [HttpGet("{itemId}")] - public ActionResult> GetItemByID(int id) + public ActionResult> GetItemByID(long id) { var item = _repository.GetItemById(id); if (item != null) @@ -56,14 +52,14 @@ namespace BuecherwurmAPI.Controllers // DELETE inventory/{id} [HttpDelete("itemId")] - public ActionResult> DeleteItem(int itemId) + public ActionResult> DeleteItem(long itemId) { var item = _repository.GetItemById(itemId); if(item == null) { return NotFound(); } - _repository.DeleteItem(item); + _repository.DeleteItem(itemId); return NoContent(); } diff --git a/Models/IItemRepo.cs b/Models/IItemRepo.cs index f8a0b04..7646320 100644 --- a/Models/IItemRepo.cs +++ b/Models/IItemRepo.cs @@ -6,8 +6,8 @@ namespace BuecherwurmAPI.Models public interface IItemRepo { IEnumerable GetAllItems(); - Item GetItemById(int itemId); - void NewItem(ItemPost item); - void DeleteItem(Item item); + Item GetItemById(long itemId); + Item NewItem(ItemPost item); + void DeleteItem(long itemId); } } \ No newline at end of file diff --git a/Models/Item.cs b/Models/Item.cs index b0ba3ce..a227f68 100644 --- a/Models/Item.cs +++ b/Models/Item.cs @@ -5,9 +5,9 @@ namespace BuecherwurmAPI.Models public class Item { [Key, Required] - public int ItemId { get; set; } + public long ItemId { get; set; } [Required] - public int BookId { get; set; } + public long BookId { get; set; } } } \ No newline at end of file diff --git a/Models/ItemModel.cs b/Models/ItemModel.cs index 5ae273d..37017b0 100644 --- a/Models/ItemModel.cs +++ b/Models/ItemModel.cs @@ -49,7 +49,7 @@ namespace BuecherwurmAPI.Models // using automatically disposes the command after completion using (var command = _dbConnection.CreateCommand()) { - command.CommandText = @"SELECT * FROM Items"; + command.CommandText = "SELECT FROM Items"; var dataReader = command.ExecuteReader(); while (dataReader.Read()) @@ -58,20 +58,20 @@ namespace BuecherwurmAPI.Models items.Add(new Item { - ItemId = (int) dataReader["ItemId"], - BookId = (int) dataReader["BookId"] + ItemId = (long) dataReader["ItemId"], + BookId = (long) dataReader["BookId"] }); } } return items; } - public Item GetItemById(int id) + public Item GetItemById(long itemId) { using (var command = _dbConnection.CreateCommand()) { - command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; - command.CommandText = @"SELECT * FROM Items WHERE Id = @id"; + command.Parameters.Add(new SqliteParameter("@itemId", itemId)); + command.CommandText = "SELECT FROM Items WHERE Id = @itemId"; var dataReader = command.ExecuteReader(); while (dataReader.Read()) @@ -80,8 +80,8 @@ namespace BuecherwurmAPI.Models var item = new Item { - ItemId = (int) dataReader["ItemId"], - BookId = (int) dataReader["BookId"] + ItemId = (long) dataReader["ItemId"], + BookId = (long) dataReader["BookId"] }; return item; } @@ -93,22 +93,29 @@ namespace BuecherwurmAPI.Models using (var command = _dbConnection.CreateCommand()) { //hier soll jetz der NewItem Command folgen. ein POST mit EingabeInfo {id} = "BookId" - command.CommandText = "INSERT INTO Item(BOOKID) VALUES (@item)"; + command.CommandText = "INSERT INTO Items (BookId) VALUES (@bookId)"; command.CommandType = System.Data.CommandType.Text; - command.Parameters.Add(new SqliteParameter("@ITEMID", itemPost) - item; + command.Parameters.Add(new SqliteParameter("@bookId", itemPost.BookId)); + command.ExecuteNonQuery(); + command.CommandText = "select last_insert_rowid()"; + Int64 LastRowId64 = (Int64)command.ExecuteScalar(); + long LastRowId = (long)LastRowId64; + var item = new Item{ + ItemId = LastRowId64, + BookId = itemPost.BookId + }; return item; } } - public void DeleteItem(int itemId) + public void DeleteItem(long itemId) { using (var command = _dbConnection.CreateCommand()) { command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = itemId; - //hier soll jetz der NewItem Command folgen. ein POST mit EingabeInfo {id} = "BookId" - - command.CommandText = @"SELECT * FROM Items WHERE ItemId = @id"; + command.CommandText = "DELETE FROM Items WHERE ItemId = @itemId"; + command.Parameters.Add(new SqliteParameter("@itemId", itemId)); + command.ExecuteNonQuery(); } } } From 8ffc0609dcfc2835f2c53788c0a715d0ec3aa95f Mon Sep 17 00:00:00 2001 From: David Renner Date: Wed, 3 Jun 2020 08:28:11 +0200 Subject: [PATCH 5/7] leere Zeilen entfernt --- Controllers/ItemController.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Controllers/ItemController.cs b/Controllers/ItemController.cs index c85da0f..db46e8c 100644 --- a/Controllers/ItemController.cs +++ b/Controllers/ItemController.cs @@ -45,10 +45,8 @@ namespace BuecherwurmAPI.Controllers return Ok(item); } return NoContent(); - } - // DELETE inventory/{id} [HttpDelete("itemId")] From a8b1761867a5e1f5e6ad483fbd2fff95a87c29a8 Mon Sep 17 00:00:00 2001 From: David Renner Date: Wed, 3 Jun 2020 09:20:55 +0200 Subject: [PATCH 6/7] merged item und itempost --- Models/Item.cs | 5 +++++ Models/ItemPost.cs | 11 ----------- 2 files changed, 5 insertions(+), 11 deletions(-) delete mode 100644 Models/ItemPost.cs diff --git a/Models/Item.cs b/Models/Item.cs index a227f68..23ecb63 100644 --- a/Models/Item.cs +++ b/Models/Item.cs @@ -9,5 +9,10 @@ namespace BuecherwurmAPI.Models [Required] public long BookId { get; set; } } + public class ItemPost + { + [Required] + public int BookId { get; set; } + } } \ No newline at end of file diff --git a/Models/ItemPost.cs b/Models/ItemPost.cs deleted file mode 100644 index 981c119..0000000 --- a/Models/ItemPost.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace BuecherwurmAPI.Models -{ - public class ItemPost - { - [Required] - public int BookId { get; set; } - } - -} \ No newline at end of file From de7bc0871df7215456bdae71fd6c1ea14c33213b Mon Sep 17 00:00:00 2001 From: nek0 Date: Wed, 3 Jun 2020 09:33:07 +0200 Subject: [PATCH 7/7] review --- Models/Item.cs | 1 - Models/ItemModel.cs | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/Models/Item.cs b/Models/Item.cs index 23ecb63..2726273 100644 --- a/Models/Item.cs +++ b/Models/Item.cs @@ -11,7 +11,6 @@ namespace BuecherwurmAPI.Models } public class ItemPost { - [Required] public int BookId { get; set; } } diff --git a/Models/ItemModel.cs b/Models/ItemModel.cs index 37017b0..f2ef1b1 100644 --- a/Models/ItemModel.cs +++ b/Models/ItemModel.cs @@ -98,8 +98,7 @@ namespace BuecherwurmAPI.Models command.Parameters.Add(new SqliteParameter("@bookId", itemPost.BookId)); command.ExecuteNonQuery(); command.CommandText = "select last_insert_rowid()"; - Int64 LastRowId64 = (Int64)command.ExecuteScalar(); - long LastRowId = (long)LastRowId64; + var LastRowId64 = (long)command.ExecuteScalar(); var item = new Item{ ItemId = LastRowId64, BookId = itemPost.BookId