2020-06-02 12:23:21 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using BuecherwurmAPI.Models;
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
using Microsoft.VisualBasic.CompilerServices;
|
|
|
|
|
|
|
|
namespace BuecherwurmAPI.Models
|
|
|
|
{
|
2020-06-03 09:34:12 +00:00
|
|
|
public interface IItem
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class ItemModel : IItem
|
2020-06-02 12:23:21 +00:00
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
2020-06-03 09:34:12 +00:00
|
|
|
command.CommandText = "SELECT * FROM Items";
|
2020-06-02 12:23:21 +00:00
|
|
|
var dataReader = command.ExecuteReader();
|
|
|
|
|
|
|
|
while (dataReader.Read())
|
|
|
|
{
|
|
|
|
items.Add(new Item
|
|
|
|
{
|
2020-06-03 06:26:54 +00:00
|
|
|
ItemId = (long) dataReader["ItemId"],
|
|
|
|
BookId = (long) dataReader["BookId"]
|
2020-06-02 12:23:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2020-06-03 06:26:54 +00:00
|
|
|
public Item GetItemById(long itemId)
|
2020-06-02 12:23:21 +00:00
|
|
|
{
|
|
|
|
using (var command = _dbConnection.CreateCommand())
|
|
|
|
{
|
2020-06-03 06:26:54 +00:00
|
|
|
command.Parameters.Add(new SqliteParameter("@itemId", itemId));
|
2020-06-03 09:34:12 +00:00
|
|
|
command.CommandText = "SELECT * FROM Items WHERE ItemId = @itemId";
|
2020-06-02 12:23:21 +00:00
|
|
|
var dataReader = command.ExecuteReader();
|
|
|
|
|
|
|
|
while (dataReader.Read())
|
|
|
|
{
|
|
|
|
var item = new Item
|
|
|
|
{
|
2020-06-03 06:26:54 +00:00
|
|
|
ItemId = (long) dataReader["ItemId"],
|
|
|
|
BookId = (long) dataReader["BookId"]
|
2020-06-02 12:23:21 +00:00
|
|
|
};
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2020-06-02 14:01:17 +00:00
|
|
|
public Item NewItem(ItemPost itemPost)
|
2020-06-02 12:23:21 +00:00
|
|
|
{
|
|
|
|
using (var command = _dbConnection.CreateCommand())
|
|
|
|
{
|
|
|
|
//hier soll jetz der NewItem Command folgen. ein POST mit EingabeInfo {id} = "BookId"
|
2020-06-03 06:26:54 +00:00
|
|
|
command.CommandText = "INSERT INTO Items (BookId) VALUES (@bookId)";
|
2020-06-02 14:01:17 +00:00
|
|
|
command.CommandType = System.Data.CommandType.Text;
|
2020-06-03 06:26:54 +00:00
|
|
|
command.Parameters.Add(new SqliteParameter("@bookId", itemPost.BookId));
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
command.CommandText = "select last_insert_rowid()";
|
2020-06-03 07:33:07 +00:00
|
|
|
var LastRowId64 = (long)command.ExecuteScalar();
|
2020-06-03 06:26:54 +00:00
|
|
|
var item = new Item{
|
|
|
|
ItemId = LastRowId64,
|
|
|
|
BookId = itemPost.BookId
|
|
|
|
};
|
2020-06-02 12:23:21 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 06:26:54 +00:00
|
|
|
public void DeleteItem(long itemId)
|
2020-06-02 12:23:21 +00:00
|
|
|
{
|
|
|
|
using (var command = _dbConnection.CreateCommand())
|
|
|
|
{
|
2020-06-03 06:26:54 +00:00
|
|
|
command.CommandText = "DELETE FROM Items WHERE ItemId = @itemId";
|
|
|
|
command.Parameters.Add(new SqliteParameter("@itemId", itemId));
|
|
|
|
command.ExecuteNonQuery();
|
2020-06-02 12:23:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|