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(); } } }