update Item Models und Controller

This commit is contained in:
David Renner 2020-06-02 14:23:21 +02:00
parent 7f94b25e57
commit 7bdb85d842
4 changed files with 129 additions and 5 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,12 +28,13 @@ namespace BuecherwurmAPI.Controllers
// POST Inventar // POST Inventar
[HttpPost] [HttpPost]
public ActionResult<IEnumerable<Item>> NewItem(Item item) public ActionResult<IEnumerable<Item>> NewItem(int bookId)
{ {
return Ok(new Item return Ok(new Item
{ {
Id = item.Id,
BookId = item.BookId, ItemId = itemId,
BookId = bookId
}); });
} }

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

112
Models/ItemModel.cs Normal file
View file

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

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