Struct für Tabellen hinzugefügt, GetLendById-Methode fertiggestellt und IdExits-Methode hinzugefügt
This commit is contained in:
parent
41a53631dd
commit
89fcf8abd2
4 changed files with 72 additions and 12 deletions
|
@ -63,9 +63,13 @@ namespace BuecherwurmAPI.Controllers
|
||||||
|
|
||||||
//GET api/leihvorgang/{id}
|
//GET api/leihvorgang/{id}
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public ActionResult<Lend> LendById(int id)
|
public ActionResult<Lend> LendById(long id)
|
||||||
{
|
{
|
||||||
var lend = _repository.GetLendById(id);
|
var lend = _repository.GetLendById(id);
|
||||||
|
if (!_repository.IdExits(Tables.Table.Lends, id))
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
return Ok(lend);
|
return Ok(lend);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,11 +77,6 @@ namespace BuecherwurmAPI.Controllers
|
||||||
[HttpPatch("{id}")]
|
[HttpPatch("{id}")]
|
||||||
public ActionResult LendPatchById(int id, JsonPatchDocument<Lend> patchDocument)
|
public ActionResult LendPatchById(int id, JsonPatchDocument<Lend> patchDocument)
|
||||||
{
|
{
|
||||||
var lend = _repository.GetLendById(id);
|
|
||||||
if (lend == null)
|
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using BuecherwurmAPI.Models;
|
using BuecherwurmAPI.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||||
|
|
||||||
namespace BuecherwurmAPI.Data
|
namespace BuecherwurmAPI.Data
|
||||||
{
|
{
|
||||||
public interface IRepository
|
public interface IRepository
|
||||||
{
|
{
|
||||||
IEnumerable<Lend> GetAllLends();
|
IEnumerable<Lend> GetAllLends();
|
||||||
Lend GetLendById(int id);
|
Lend GetLendById(long id);
|
||||||
|
bool IdExits(string table, long id);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,13 +2,14 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using BuecherwurmAPI.Models;
|
using BuecherwurmAPI.Models;
|
||||||
using Microsoft.Data.Sqlite;
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.VisualBasic.CompilerServices;
|
||||||
|
|
||||||
namespace BuecherwurmAPI.Data
|
namespace BuecherwurmAPI.Data
|
||||||
{
|
{
|
||||||
internal class Repository : IRepository
|
internal class Repository : IRepository
|
||||||
{
|
{
|
||||||
private SqliteConnection _dbConnection;
|
private SqliteConnection _dbConnection;
|
||||||
|
|
||||||
public Repository()
|
public Repository()
|
||||||
{
|
{
|
||||||
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
|
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
|
||||||
|
@ -16,6 +17,33 @@ namespace BuecherwurmAPI.Data
|
||||||
_dbConnection.Open();
|
_dbConnection.Open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IdExits(string table, long id)
|
||||||
|
{
|
||||||
|
using (var command = _dbConnection.CreateCommand())
|
||||||
|
{
|
||||||
|
var com = _dbConnection.CreateCommand();
|
||||||
|
|
||||||
|
com.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.Lends:
|
||||||
|
com.CommandText = @"SELECT EXISTS(SELECT 0 FROM Lends WHERE Id = @Id)";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var dr = com.ExecuteReader();
|
||||||
|
|
||||||
|
long result = 0;
|
||||||
|
while (dr.Read())
|
||||||
|
{
|
||||||
|
result = (long) dr[0];
|
||||||
|
}
|
||||||
|
return result != 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public IEnumerable<Lend> GetAllLends()
|
public IEnumerable<Lend> GetAllLends()
|
||||||
{
|
{
|
||||||
var lends = new List<Lend>();
|
var lends = new List<Lend>();
|
||||||
|
@ -28,14 +56,14 @@ namespace BuecherwurmAPI.Data
|
||||||
|
|
||||||
while (dataReader.Read())
|
while (dataReader.Read())
|
||||||
{
|
{
|
||||||
var returned = (long) dataReader["Returned"] == 0;
|
var returned = (long) dataReader["Returned"] == 1;
|
||||||
|
|
||||||
lends.Add(new Lend
|
lends.Add(new Lend
|
||||||
{
|
{
|
||||||
Id = (long) dataReader["Id"],
|
Id = (long) dataReader["Id"],
|
||||||
Customer = (string) dataReader["Customer"],
|
Customer = (string) dataReader["Customer"],
|
||||||
ItemId = (long) dataReader["ItemId"],
|
ItemId = (long) dataReader["ItemId"],
|
||||||
Returned = !returned,
|
Returned = returned,
|
||||||
ReturnDate = DateTime.Parse((string)dataReader["ReturnDate"])
|
ReturnDate = DateTime.Parse((string)dataReader["ReturnDate"])
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -43,9 +71,30 @@ namespace BuecherwurmAPI.Data
|
||||||
return lends;
|
return lends;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Lend GetLendById(int id)
|
public Lend GetLendById(long id)
|
||||||
{
|
{
|
||||||
return new Lend{Id = 1, Customer = "Nek0", ItemId = 1337, Returned = false, ReturnDate = DateTime.Now};
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
10
Data/Tables.cs
Normal file
10
Data/Tables.cs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
namespace BuecherwurmAPI.Data
|
||||||
|
{
|
||||||
|
public static class Tables
|
||||||
|
{
|
||||||
|
public struct Table
|
||||||
|
{
|
||||||
|
public const string Lends = "Lends";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue