Funktionen zur "Item" Gruppe hinzugefügt

This commit is contained in:
David Renner 2020-06-03 08:26:54 +02:00
parent bd0f852d9b
commit 098c780366
4 changed files with 32 additions and 29 deletions

View File

@ -30,18 +30,14 @@ namespace BuecherwurmAPI.Controllers
[HttpPost] [HttpPost]
public ActionResult<IEnumerable<Item>> NewItem(ItemPost item) public ActionResult<IEnumerable<Item>> NewItem(ItemPost item)
{ {
return Ok(new Item var newItem = _repository.NewItem(item);
{ return Ok(newItem);
ItemId = itemId,
BookId = bookId
});
} }
// GET Inventar/{id} // GET Inventar/{id}
[HttpGet("{itemId}")] [HttpGet("{itemId}")]
public ActionResult<IEnumerable<Item>> GetItemByID(int id) public ActionResult<IEnumerable<Item>> GetItemByID(long id)
{ {
var item = _repository.GetItemById(id); var item = _repository.GetItemById(id);
if (item != null) if (item != null)
@ -56,14 +52,14 @@ namespace BuecherwurmAPI.Controllers
// DELETE inventory/{id} // DELETE inventory/{id}
[HttpDelete("itemId")] [HttpDelete("itemId")]
public ActionResult<IEnumerable<Item>> DeleteItem(int itemId) public ActionResult<IEnumerable<Item>> DeleteItem(long itemId)
{ {
var item = _repository.GetItemById(itemId); var item = _repository.GetItemById(itemId);
if(item == null) if(item == null)
{ {
return NotFound(); return NotFound();
} }
_repository.DeleteItem(item); _repository.DeleteItem(itemId);
return NoContent(); return NoContent();
} }

View File

@ -6,8 +6,8 @@ namespace BuecherwurmAPI.Models
public interface IItemRepo public interface IItemRepo
{ {
IEnumerable<Item> GetAllItems(); IEnumerable<Item> GetAllItems();
Item GetItemById(int itemId); Item GetItemById(long itemId);
void NewItem(ItemPost item); Item NewItem(ItemPost item);
void DeleteItem(Item item); void DeleteItem(long itemId);
} }
} }

View File

@ -5,9 +5,9 @@ namespace BuecherwurmAPI.Models
public class Item public class Item
{ {
[Key, Required] [Key, Required]
public int ItemId { get; set; } public long ItemId { get; set; }
[Required] [Required]
public int BookId { get; set; } public long BookId { get; set; }
} }
} }

View File

@ -49,7 +49,7 @@ namespace BuecherwurmAPI.Models
// using automatically disposes the command after completion // using automatically disposes the command after completion
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
command.CommandText = @"SELECT * FROM Items"; command.CommandText = "SELECT FROM Items";
var dataReader = command.ExecuteReader(); var dataReader = command.ExecuteReader();
while (dataReader.Read()) while (dataReader.Read())
@ -58,20 +58,20 @@ namespace BuecherwurmAPI.Models
items.Add(new Item items.Add(new Item
{ {
ItemId = (int) dataReader["ItemId"], ItemId = (long) dataReader["ItemId"],
BookId = (int) dataReader["BookId"] BookId = (long) dataReader["BookId"]
}); });
} }
} }
return items; return items;
} }
public Item GetItemById(int id) public Item GetItemById(long itemId)
{ {
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; command.Parameters.Add(new SqliteParameter("@itemId", itemId));
command.CommandText = @"SELECT * FROM Items WHERE Id = @id"; command.CommandText = "SELECT FROM Items WHERE Id = @itemId";
var dataReader = command.ExecuteReader(); var dataReader = command.ExecuteReader();
while (dataReader.Read()) while (dataReader.Read())
@ -80,8 +80,8 @@ namespace BuecherwurmAPI.Models
var item = new Item var item = new Item
{ {
ItemId = (int) dataReader["ItemId"], ItemId = (long) dataReader["ItemId"],
BookId = (int) dataReader["BookId"] BookId = (long) dataReader["BookId"]
}; };
return item; return item;
} }
@ -93,22 +93,29 @@ namespace BuecherwurmAPI.Models
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
//hier soll jetz der NewItem Command folgen. ein POST mit EingabeInfo {id} = "BookId" //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.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new SqliteParameter("@ITEMID", itemPost) command.Parameters.Add(new SqliteParameter("@bookId", itemPost.BookId));
item; 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; return item;
} }
} }
public void DeleteItem(int itemId) public void DeleteItem(long itemId)
{ {
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = itemId; 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 = "DELETE FROM Items WHERE ItemId = @itemId";
command.Parameters.Add(new SqliteParameter("@itemId", itemId));
command.CommandText = @"SELECT * FROM Items WHERE ItemId = @id"; command.ExecuteNonQuery();
} }
} }
} }