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]
public ActionResult<IEnumerable<Item>> 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<IEnumerable<Item>> GetItemByID(int id)
public ActionResult<IEnumerable<Item>> 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<IEnumerable<Item>> DeleteItem(int itemId)
public ActionResult<IEnumerable<Item>> DeleteItem(long itemId)
{
var item = _repository.GetItemById(itemId);
if(item == null)
{
return NotFound();
}
_repository.DeleteItem(item);
_repository.DeleteItem(itemId);
return NoContent();
}

View File

@ -6,8 +6,8 @@ namespace BuecherwurmAPI.Models
public interface IItemRepo
{
IEnumerable<Item> 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);
}
}

View File

@ -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; }
}
}

View File

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