Merge remote-tracking branch 'origin/David' into Amedeo

This commit is contained in:
nek0 2020-06-03 09:49:21 +02:00
commit d0672366dc
6 changed files with 158 additions and 25 deletions

View File

@ -2,6 +2,7 @@ using System.Collections.Generic;
using BuecherwurmAPI.Models; using BuecherwurmAPI.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Linq; using System.Linq;
using Microsoft.Data.Sqlite;
//using Microsoft.EntityFrameworkCore; //using Microsoft.EntityFrameworkCore;
@ -9,11 +10,11 @@ namespace BuecherwurmAPI.Controllers
{ {
[Route("inventar")] [Route("inventar")]
[ApiController] [ApiController]
public class InventarController : ControllerBase public class ItemController : ControllerBase
{ {
private readonly IItemRepo _repository; private readonly IItemRepo _repository;
public InventarController(IItemRepo repository) public ItemController(IItemRepo repository)
{ {
_repository = repository; _repository = repository;
} }
@ -27,19 +28,17 @@ namespace BuecherwurmAPI.Controllers
// POST Inventar // POST Inventar
[HttpPost] [HttpPost]
public ActionResult<IEnumerable<Item>> NewItem(int bookId)
public ActionResult<IEnumerable<Item>> NewItem(ItemPost item)
{ {
var itemid = insertItemReturningId(bookId); var newItem = _repository.NewItem(item);
return Ok(new Item return Ok(newItem);
{
Id = itemId,
BookId = bookId,
});
} }
// GET Inventar/{id} // GET Inventar/{id}
[HttpGet("{id}", Name = "GetItemByID")] [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)
@ -47,20 +46,19 @@ namespace BuecherwurmAPI.Controllers
return Ok(item); return Ok(item);
} }
return NoContent(); return NoContent();
} }
// DELETE inventory/{id} // DELETE inventory/{id}
[HttpDelete("id")]
public ActionResult<IEnumerable<Item>> DeleteItem(int id) [HttpDelete("itemId")]
public ActionResult<IEnumerable<Item>> DeleteItem(long itemId)
{ {
var item = _repository.GetItemById(id); 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();
} }

9
DTOs/ItemRead.cs Normal file
View File

@ -0,0 +1,9 @@
using System;
namespace BuecherwurmAPI.DTOs
{
public class ItemReadDTO
{
public int BookId { get; set;}
}
}

View File

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

View File

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

121
Models/ItemModel.cs Normal file
View File

@ -0,0 +1,121 @@
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<Item> GetAllItems()
{
var items = new List<Item>();
// 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 = (long) dataReader["ItemId"],
BookId = (long) dataReader["BookId"]
});
}
}
return items;
}
public Item GetItemById(long itemId)
{
using (var command = _dbConnection.CreateCommand())
{
command.Parameters.Add(new SqliteParameter("@itemId", itemId));
command.CommandText = "SELECT FROM Items WHERE Id = @itemId";
var dataReader = command.ExecuteReader();
while (dataReader.Read())
{
var returned = (long) dataReader["Returned"] == 1;
var item = new Item
{
ItemId = (long) dataReader["ItemId"],
BookId = (long) dataReader["BookId"]
};
return item;
}
}
return null;
}
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 Items (BookId) VALUES (@bookId)";
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new SqliteParameter("@bookId", itemPost.BookId));
command.ExecuteNonQuery();
command.CommandText = "select last_insert_rowid()";
var LastRowId64 = (long)command.ExecuteScalar();
var item = new Item{
ItemId = LastRowId64,
BookId = itemPost.BookId
};
return item;
}
}
public void DeleteItem(long itemId)
{
using (var command = _dbConnection.CreateCommand())
{
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = itemId;
command.CommandText = "DELETE FROM Items WHERE ItemId = @itemId";
command.Parameters.Add(new SqliteParameter("@itemId", itemId));
command.ExecuteNonQuery();
}
}
}
}

View File

@ -6,6 +6,7 @@ namespace BuecherwurmAPI.Models
{ {
public const string Lends = "Lends"; public const string Lends = "Lends";
public const string Katalog = "Katalog"; public const string Katalog = "Katalog";
public const string Items = "Items";
} }
} }
} }