From 7bdb85d8427c186916d02fd49170f1b05c0be759 Mon Sep 17 00:00:00 2001 From: David Renner Date: Tue, 2 Jun 2020 14:23:21 +0200 Subject: [PATCH 01/11] 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 02/11] 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 03/11] =?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 04/11] =?UTF-8?q?Funktionen=20zur=20"Item"=20Gruppe=20hinz?= =?UTF-8?q?ugef=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 05/11] 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 06/11] 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 07/11] 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 From 1d492652306a05677cda7ea232e559c761427406 Mon Sep 17 00:00:00 2001 From: nek0 Date: Wed, 3 Jun 2020 10:45:54 +0200 Subject: [PATCH 08/11] make items queryable --- Controllers/ItemController.cs | 16 ++++++++-------- Controllers/LendController.cs | 4 ++-- DTOs/LendRead.cs | 1 - LongWormMemory.db | Bin 12288 -> 12288 bytes Models/ItemModel.cs | 2 +- Models/LendModel.cs | 6 ++++-- Startup.cs | 1 + 7 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Controllers/ItemController.cs b/Controllers/ItemController.cs index 33d1135..83506c0 100644 --- a/Controllers/ItemController.cs +++ b/Controllers/ItemController.cs @@ -8,17 +8,17 @@ using Microsoft.Data.Sqlite; namespace BuecherwurmAPI.Controllers { - [Route("inventar")] + [Route("api/inventar")] [ApiController] public class ItemController : ControllerBase { - private readonly IItemRepo _repository; + private readonly ItemModel _repository; - public ItemController(IItemRepo repository) + public ItemController(IModel repository) { - _repository = repository; + _repository = (ItemModel)repository; } - // GET Inventar + // GET api/inventar [HttpGet] public ActionResult> GetAllItems() { @@ -26,7 +26,7 @@ namespace BuecherwurmAPI.Controllers return Ok(items); } - // POST Inventar + // POST api/inventar [HttpPost] public ActionResult> NewItem(ItemPost item) @@ -36,7 +36,7 @@ namespace BuecherwurmAPI.Controllers } - // GET Inventar/{id} + // GET api/nventar/{id} [HttpGet("{itemId}")] public ActionResult> GetItemByID(long id) { @@ -48,7 +48,7 @@ namespace BuecherwurmAPI.Controllers return NoContent(); } - // DELETE inventory/{id} + // DELETE api/inventar/{id} [HttpDelete("itemId")] public ActionResult> DeleteItem(long itemId) diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs index b18ff9b..6ec0b63 100644 --- a/Controllers/LendController.cs +++ b/Controllers/LendController.cs @@ -19,9 +19,9 @@ namespace BuecherwurmAPI.Controllers private readonly LendModel _repository; private readonly IMapper _mapper; - public LendController(SqliteConnection connection, IMapper mapper) + public LendController(IModel repo, IMapper mapper) { - _repository = new LendModel(connection); + _repository = (LendModel)repo; _mapper = mapper; } diff --git a/DTOs/LendRead.cs b/DTOs/LendRead.cs index 5813213..0d39ee9 100644 --- a/DTOs/LendRead.cs +++ b/DTOs/LendRead.cs @@ -7,6 +7,5 @@ namespace BuecherwurmAPI.DTOs public int ItemId { get; set;} public DateTime ReturnDate { get; set; } public string Customer { get; set; } - public bool Returned { get; set; } } } \ No newline at end of file diff --git a/LongWormMemory.db b/LongWormMemory.db index 4b1deb5d1a8e635edc1c9c4ffcc9d800974024f1..c66591815d402591565ca39ff11d5f3689865e2c 100644 GIT binary patch delta 96 zcmZojXh@hK&B#7c##xY^LC-{imw|zSk>8Mk-;mE`Go!#Y-py`&`viGd_&r$oFY?dk v&*JykETCY)ucW}j#=vPT&0%C(); + services.AddScoped(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); } From f970ccc07e42333f2fbec5670e5a7a953a3464fe Mon Sep 17 00:00:00 2001 From: nek0 Date: Wed, 3 Jun 2020 10:55:39 +0200 Subject: [PATCH 09/11] removed obsolete DTOs --- Controllers/LendController.cs | 15 +-------------- DTOs/ItemRead.cs | 9 --------- DTOs/LendRead.cs | 11 ----------- Models/Item.cs | 1 - Models/Lend.cs | 2 +- Models/LendModel.cs | 2 +- 6 files changed, 3 insertions(+), 37 deletions(-) delete mode 100644 DTOs/ItemRead.cs delete mode 100644 DTOs/LendRead.cs diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs index 6ec0b63..24f20b6 100644 --- a/Controllers/LendController.cs +++ b/Controllers/LendController.cs @@ -34,21 +34,8 @@ namespace BuecherwurmAPI.Controllers //POST api/leihvorgang [HttpPost] - public ActionResult LendsPost(LendSeed lend) + public ActionResult LendsPost(LendPost lend) { - /* - Internally a lend is stored with an id - but the client shouldn't be allowed to set or change it - therefore the package 'AutoMapper' is used to prevent errors - that could happen when doing this task manually. - It takes the information from the client and maps it to the - corresponding internal object which then will be returned. - Furthermore it could be used to keep some attributes secret. - Another nice effect of this is that the implementation could be changed - while the interface could be retained by some minor changes in the code. - - DTO stands for Data Transfer Object - */ var newId = _repository.insertLendReturningId(lend); if (newId > 0) { diff --git a/DTOs/ItemRead.cs b/DTOs/ItemRead.cs deleted file mode 100644 index e70a016..0000000 --- a/DTOs/ItemRead.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace BuecherwurmAPI.DTOs -{ - public class ItemReadDTO - { - public int BookId { get; set;} - } -} \ No newline at end of file diff --git a/DTOs/LendRead.cs b/DTOs/LendRead.cs deleted file mode 100644 index 0d39ee9..0000000 --- a/DTOs/LendRead.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace BuecherwurmAPI.DTOs -{ - public class LendReadDTO - { - public int ItemId { get; set;} - public DateTime ReturnDate { get; set; } - public string Customer { get; set; } - } -} \ No newline at end of file diff --git a/Models/Item.cs b/Models/Item.cs index 2726273..dbff417 100644 --- a/Models/Item.cs +++ b/Models/Item.cs @@ -13,5 +13,4 @@ namespace BuecherwurmAPI.Models { public int BookId { get; set; } } - } \ No newline at end of file diff --git a/Models/Lend.cs b/Models/Lend.cs index 6c258f2..50ba663 100644 --- a/Models/Lend.cs +++ b/Models/Lend.cs @@ -17,7 +17,7 @@ namespace BuecherwurmAPI.Models public bool Returned { get; set; } } - public class LendSeed + public class LendPost { public long ItemId {get; set;} diff --git a/Models/LendModel.cs b/Models/LendModel.cs index 80076f4..1a0f5c1 100644 --- a/Models/LendModel.cs +++ b/Models/LendModel.cs @@ -95,7 +95,7 @@ namespace BuecherwurmAPI.Models return null; } - public long insertLendReturningId(LendSeed lend) + public long insertLendReturningId(LendPost lend) { using (var command = _dbConnection.CreateCommand()) { From 56e1cc14522b0786f9621b7e7cf58e0aa4bebd00 Mon Sep 17 00:00:00 2001 From: nek0 Date: Wed, 3 Jun 2020 11:34:12 +0200 Subject: [PATCH 10/11] reviewed, debugged and meowed with items --- Controllers/ItemController.cs | 4 ++-- Controllers/LendController.cs | 7 +++---- LongWormMemory.db | Bin 12288 -> 12288 bytes Models/Interfaces.cs | 9 --------- Models/ItemModel.cs | 14 ++++++++------ Models/LendModel.cs | 10 ++++++++-- Profiles/LendProfile.cs | 3 +-- Startup.cs | 4 ++-- 8 files changed, 24 insertions(+), 27 deletions(-) delete mode 100644 Models/Interfaces.cs diff --git a/Controllers/ItemController.cs b/Controllers/ItemController.cs index 83506c0..f4974e0 100644 --- a/Controllers/ItemController.cs +++ b/Controllers/ItemController.cs @@ -14,7 +14,7 @@ namespace BuecherwurmAPI.Controllers { private readonly ItemModel _repository; - public ItemController(IModel repository) + public ItemController(IItem repository) { _repository = (ItemModel)repository; } @@ -50,7 +50,7 @@ namespace BuecherwurmAPI.Controllers // DELETE api/inventar/{id} - [HttpDelete("itemId")] + [HttpDelete("{itemId}")] public ActionResult> DeleteItem(long itemId) { var item = _repository.GetItemById(itemId); diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs index 24f20b6..f599103 100644 --- a/Controllers/LendController.cs +++ b/Controllers/LendController.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Threading.Tasks; using AutoMapper; using BuecherwurmAPI.Models; -using BuecherwurmAPI.DTOs; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.JsonPatch; using Microsoft.Extensions.Logging; @@ -17,12 +16,12 @@ namespace BuecherwurmAPI.Controllers public class LendController : ControllerBase { private readonly LendModel _repository; - private readonly IMapper _mapper; + //private readonly IMapper _mapper; - public LendController(IModel repo, IMapper mapper) + public LendController(ILend repo) { _repository = (LendModel)repo; - _mapper = mapper; + //_mapper = mapper; } //GET api/leihvorgang diff --git a/LongWormMemory.db b/LongWormMemory.db index c66591815d402591565ca39ff11d5f3689865e2c..d001dc19b76d9dab88aa3f056c23004368ba02ac 100644 GIT binary patch delta 265 zcmZojXh@hKEy&Bjz`zW|Fc2|O#~3K6XQBX-VdC4zz;DR6Z!@F7HQvc?d=>SDvh3pG z;*5=sC5cHnsh%aNxy4|D$vMc?F~n6N#L>yeRRJudpaG&hQxrV?LR{TlgB1MyLlpc% zeSCBjobvOtu}TC4dHOmAMJjl^MnZLKYBu`FfbDE5FG-wi%YT>)(); + CreateMap(); } } } \ No newline at end of file diff --git a/Startup.cs b/Startup.cs index 33779c4..1fc2f81 100644 --- a/Startup.cs +++ b/Startup.cs @@ -32,8 +32,8 @@ namespace BuecherwurmAPI // Adds a service that is created once per connection. // It takes an interface and a specific implementation. // That allows to swap the implementation easily. - services.AddScoped(); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); } From ffd841b52626635d244f80e9049474e23f3acdfb Mon Sep 17 00:00:00 2001 From: nek0 Date: Wed, 3 Jun 2020 12:43:03 +0200 Subject: [PATCH 11/11] finalized catalogue --- Controllers/ItemController.cs | 6 ++--- Controllers/KatalogController.cs | 43 +++++++++++-------------------- LongWormMemory.db | Bin 12288 -> 16384 bytes Models/Book.cs | 18 +++++++++++-- Models/ItemModel.cs | 2 -- Models/KatalogModel.cs | 30 ++++++++++----------- Startup.cs | 1 + 7 files changed, 48 insertions(+), 52 deletions(-) diff --git a/Controllers/ItemController.cs b/Controllers/ItemController.cs index f4974e0..d089fb1 100644 --- a/Controllers/ItemController.cs +++ b/Controllers/ItemController.cs @@ -38,14 +38,14 @@ namespace BuecherwurmAPI.Controllers // GET api/nventar/{id} [HttpGet("{itemId}")] - public ActionResult> GetItemByID(long id) + public ActionResult> GetItemByID(long itemId) { - var item = _repository.GetItemById(id); + var item = _repository.GetItemById(itemId); if (item != null) { return Ok(item); } - return NoContent(); + return NotFound(); } // DELETE api/inventar/{id} diff --git a/Controllers/KatalogController.cs b/Controllers/KatalogController.cs index 63fb7be..0e4a68d 100644 --- a/Controllers/KatalogController.cs +++ b/Controllers/KatalogController.cs @@ -6,15 +6,15 @@ using System.Linq; namespace BuecherwurmAPI.Controllers { - [Route("katalog") ] + [Route("api/katalog") ] [ApiController] public class KatalogController :ControllerBase { - private readonly IBookRepo _repository; + private readonly KatalogModel _repository; - public KatalogController (IBookRepo repository) + public KatalogController (ICatalogue repository) { - _repository=repository; + _repository= (KatalogModel)repository; } // GET Katalog [HttpGet] @@ -26,9 +26,9 @@ namespace BuecherwurmAPI.Controllers // POST Katalog [HttpPost] - public ActionResult> AddBook(Book book) + public ActionResult AddBook(BookPost book) { - + var id = _repository.AddBook(book); return Ok(new Book { Name = book.Name, @@ -38,7 +38,7 @@ namespace BuecherwurmAPI.Controllers Language= book.Language, Pages= book.Pages, Year=book.Year, - ProductId =book.ProductId, + ProductId = id, Category= book.Category, ImageLink =book.ImageLink, LendTime =book.LendTime, @@ -48,8 +48,8 @@ namespace BuecherwurmAPI.Controllers // GET katalog/{id} - [HttpGet("{id}", Name ="GetBookByID")] - public ActionResult > GetBookByID(long id) + [HttpGet("{id}")] + public ActionResult GetBookByID(long id) { var book = _repository.GetBookById(id); if (book != null) @@ -61,29 +61,16 @@ namespace BuecherwurmAPI.Controllers } // PUT Katalog/{id} - [HttpPut("id")] - public ActionResult> EditBook(Book book) + [HttpPut("{id}")] + public ActionResult EditBook(long id, Book 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 =book.ProductId, - Category= book.Category, - ImageLink =book.ImageLink, - LendTime =book.LendTime, - LendType = book.LendType - }); + _repository.EditBook(id, book); + return Ok(book); } // DELETE katalog/{id} - [HttpDelete("id")] - public ActionResult> DeleteBook (long id) + [HttpDelete("{id}")] + public ActionResult DeleteBook (long id) { var book = _repository.GetBookById(id); if(book == null) diff --git a/LongWormMemory.db b/LongWormMemory.db index d001dc19b76d9dab88aa3f056c23004368ba02ac..2af348abd5cc5350f02eaa525bfd54ec7a21f55d 100644 GIT binary patch delta 492 zcmZ8dK}!Nb7@c*LFjziML%Hnl|SVa?*SN8J)`(=l;>Vav4*zsofPYt2Z3 z9hVJ>m^+|B3D=1rZGkp^Z7AmbeT`a%&cqitipG)&oDFj@+KtwoRHJpZ*=wpIA@m6# zS^)Hc7U&BtW|{OG%D^}TNeD#|sU?yyG5g+w>Fs5P5}E=u6|xZBa!D>0^Jm#u3`lKB zI%gEuScjVU=)&e4Q;vzLsAk|Xf7s{TIW3nBMm>djT%pWTOc7R%2t({?>mB}ASa!F+ Ok(E2lG!)0`6=FXYy_9DF delta 88 zcmZo@U~EX3AT7wtz`(!^#4r#sQO6i4sAr-8l40W8$G~sMw{Nqcz%^bb#yt}o%Qo-h fb7tfO$}#f)X5jw~l)246ae^2VGvnsJ^2`DN%KZ~L diff --git a/Models/Book.cs b/Models/Book.cs index bc90fd5..3e605de 100644 --- a/Models/Book.cs +++ b/Models/Book.cs @@ -11,8 +11,7 @@ namespace BuecherwurmAPI.Models public string Language {get; set;} public long Pages {get; set;} public long Year {get; set;} - [Key] - [Required] + [Key, Required] public long ProductId { get; set; } public CategoryEnum Category { get; set; } public string ImageLink { get; set; } @@ -20,4 +19,19 @@ namespace BuecherwurmAPI.Models public LendTypeEnum LendType {get; set;} } + public class BookPost + { + public string Name {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;} + public CategoryEnum Category { get; set; } + public string ImageLink { get; set; } + public long LendTime {get; set;} + public LendTypeEnum LendType {get; set;} + } + } \ No newline at end of file diff --git a/Models/ItemModel.cs b/Models/ItemModel.cs index 159b53b..b4ef5ed 100644 --- a/Models/ItemModel.cs +++ b/Models/ItemModel.cs @@ -59,8 +59,6 @@ namespace BuecherwurmAPI.Models while (dataReader.Read()) { - var returned = (long) dataReader["Returned"] == 1; - items.Add(new Item { ItemId = (long) dataReader["ItemId"], diff --git a/Models/KatalogModel.cs b/Models/KatalogModel.cs index e1dd357..64f2347 100644 --- a/Models/KatalogModel.cs +++ b/Models/KatalogModel.cs @@ -6,11 +6,15 @@ using Microsoft.VisualBasic.CompilerServices; namespace BuecherwurmAPI.Models { - internal class Katalog : IBookRepo + public interface ICatalogue + { + + } + internal class KatalogModel : ICatalogue { private SqliteConnection _dbConnection; - public Katalog() + public KatalogModel() { var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; _dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); @@ -84,22 +88,21 @@ namespace BuecherwurmAPI.Models } } - public long AddBook (Book book) + public long AddBook (BookPost book) { using (var command = _dbConnection.CreateCommand()) { // funktioniert das so? //muss ProduktId übergeben werden? - command.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) VALUES (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @ProductId, @Category, @ImageLink, @LendTime, @LendType )"; + command.CommandText = @"INSERT INTO Books (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("@Contry", book.Country)); + 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("@ProductId", book.ProductId)); command.Parameters.Add(new SqliteParameter("@Category", book.Category)); command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink)); command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime)); @@ -122,17 +125,16 @@ namespace BuecherwurmAPI.Models using (var command = _dbConnection.CreateCommand()) { // funktioniert das so? - command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; - command.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) VALUES (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @ProductId, @Category, @ImageLink, @LendTime, @LendType )"; + command.Parameters.Add(new SqliteParameter("@id", id)); + command.CommandText = @"UPDATE Books 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("@Contry", book.Country)); + 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("@ProductId", book.ProductId)); command.Parameters.Add(new SqliteParameter("@Category", book.Category)); command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink)); command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime)); @@ -140,13 +142,7 @@ namespace BuecherwurmAPI.Models var success = command.ExecuteNonQuery(); - if (success == 1) - { - command.CommandText = @"SELECT last_insert_rowid()"; - return (long)command.ExecuteScalar(); - } - - return -1; + return id; } } diff --git a/Startup.cs b/Startup.cs index 1fc2f81..e8d9d83 100644 --- a/Startup.cs +++ b/Startup.cs @@ -34,6 +34,7 @@ namespace BuecherwurmAPI // That allows to swap the implementation easily. services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); }