2020-05-28 14:14:36 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using BuecherwurmAPI.Models;
|
|
|
|
using Microsoft.Data.Sqlite;
|
2020-05-29 09:18:23 +00:00
|
|
|
using Microsoft.VisualBasic.CompilerServices;
|
2020-05-28 14:14:36 +00:00
|
|
|
|
2020-06-02 09:16:45 +00:00
|
|
|
namespace BuecherwurmAPI.Models
|
2020-05-28 14:14:36 +00:00
|
|
|
{
|
2020-06-03 09:34:12 +00:00
|
|
|
|
|
|
|
public interface ILend
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class LendModel : ILend
|
2020-05-28 14:14:36 +00:00
|
|
|
{
|
2020-06-03 09:34:12 +00:00
|
|
|
private SqliteConnection _dbConnection;
|
2020-05-29 09:18:23 +00:00
|
|
|
|
2020-06-03 08:45:54 +00:00
|
|
|
public LendModel()
|
2020-05-28 14:14:36 +00:00
|
|
|
{
|
2020-06-03 08:45:54 +00:00
|
|
|
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
|
|
|
|
_dbConnection = new SqliteConnection(connectionBuilder.ConnectionString);
|
|
|
|
_dbConnection.Open();
|
2020-05-28 14:14:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 09:18:23 +00:00
|
|
|
public bool IdExits(string table, long id)
|
|
|
|
{
|
|
|
|
using (var command = _dbConnection.CreateCommand())
|
|
|
|
{
|
2020-05-29 09:30:38 +00:00
|
|
|
command.Parameters.Add(new SqliteParameter("@Id", SqliteType.Integer)).Value = id;
|
2020-05-29 09:18:23 +00:00
|
|
|
|
|
|
|
// Certain parts of the query can't be filled with variables for security reasons.
|
|
|
|
switch (table)
|
|
|
|
{
|
|
|
|
case Tables.Table.Lends:
|
2020-05-29 09:30:38 +00:00
|
|
|
command.CommandText = @"SELECT EXISTS(SELECT 0 FROM Lends WHERE Id = @Id)";
|
2020-05-29 09:18:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-29 09:30:38 +00:00
|
|
|
var dr = command.ExecuteReader();
|
2020-05-29 09:18:23 +00:00
|
|
|
|
|
|
|
long result = 0;
|
|
|
|
while (dr.Read())
|
|
|
|
{
|
|
|
|
result = (long) dr[0];
|
|
|
|
}
|
|
|
|
return result != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:14:36 +00:00
|
|
|
public IEnumerable<Lend> GetAllLends()
|
|
|
|
{
|
|
|
|
var lends = new List<Lend>();
|
|
|
|
|
|
|
|
// using automatically disposes the command after completion
|
|
|
|
using (var command = _dbConnection.CreateCommand())
|
|
|
|
{
|
|
|
|
command.CommandText = @"SELECT * FROM Lends";
|
|
|
|
var dataReader = command.ExecuteReader();
|
|
|
|
|
|
|
|
while (dataReader.Read())
|
|
|
|
{
|
2020-05-29 09:18:23 +00:00
|
|
|
var returned = (long) dataReader["Returned"] == 1;
|
2020-05-28 14:14:36 +00:00
|
|
|
|
|
|
|
lends.Add(new Lend
|
|
|
|
{
|
|
|
|
Id = (long) dataReader["Id"],
|
|
|
|
Customer = (string) dataReader["Customer"],
|
|
|
|
ItemId = (long) dataReader["ItemId"],
|
2020-05-29 09:18:23 +00:00
|
|
|
Returned = returned,
|
2020-05-28 14:14:36 +00:00
|
|
|
ReturnDate = DateTime.Parse((string)dataReader["ReturnDate"])
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lends;
|
|
|
|
}
|
|
|
|
|
2020-05-29 09:18:23 +00:00
|
|
|
public Lend GetLendById(long id)
|
2020-05-28 14:14:36 +00:00
|
|
|
{
|
2020-05-29 09:18:23 +00:00
|
|
|
using (var command = _dbConnection.CreateCommand())
|
|
|
|
{
|
|
|
|
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id;
|
|
|
|
command.CommandText = @"SELECT * FROM Lends WHERE Id = @id";
|
|
|
|
var dataReader = command.ExecuteReader();
|
|
|
|
|
|
|
|
while (dataReader.Read())
|
|
|
|
{
|
|
|
|
var returned = (long) dataReader["Returned"] == 1;
|
|
|
|
|
|
|
|
var lend = new Lend
|
|
|
|
{
|
|
|
|
Id = (long) dataReader["Id"],
|
|
|
|
Customer = (string) dataReader["Customer"],
|
|
|
|
ItemId = (long) dataReader["ItemId"],
|
|
|
|
Returned = returned,
|
|
|
|
ReturnDate = DateTime.Parse((string) dataReader["ReturnDate"])
|
|
|
|
};
|
|
|
|
return lend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2020-05-28 14:14:36 +00:00
|
|
|
}
|
2020-06-03 07:26:07 +00:00
|
|
|
|
2020-06-03 08:55:39 +00:00
|
|
|
public long insertLendReturningId(LendPost lend)
|
2020-06-03 07:26:07 +00:00
|
|
|
{
|
|
|
|
using (var command = _dbConnection.CreateCommand())
|
|
|
|
{
|
|
|
|
command.CommandText = "INSERT INTO Lends VALUES (NULL, @itemId, @returnDate, @customer, false)";
|
|
|
|
command.Parameters.Add(new SqliteParameter("@itemId", lend.ItemId));
|
|
|
|
command.Parameters.Add(new SqliteParameter("@returnDate", lend.ReturnDate));
|
|
|
|
command.Parameters.Add(new SqliteParameter("@customer", lend.Customer));
|
|
|
|
|
|
|
|
var success = command.ExecuteNonQuery();
|
|
|
|
|
|
|
|
if (success == 1)
|
|
|
|
{
|
|
|
|
command.CommandText = @"SELECT last_insert_rowid()";
|
|
|
|
return (long)command.ExecuteScalar();
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 14:14:36 +00:00
|
|
|
}
|
|
|
|
}
|