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}
|
||||
[HttpGet("{id}")]
|
||||
public ActionResult<Lend> LendById(int id)
|
||||
public ActionResult<Lend> LendById(long id)
|
||||
{
|
||||
var lend = _repository.GetLendById(id);
|
||||
if (!_repository.IdExits(Tables.Table.Lends, id))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return Ok(lend);
|
||||
}
|
||||
|
||||
|
@ -73,11 +77,6 @@ namespace BuecherwurmAPI.Controllers
|
|||
[HttpPatch("{id}")]
|
||||
public ActionResult LendPatchById(int id, JsonPatchDocument<Lend> patchDocument)
|
||||
{
|
||||
var lend = _repository.GetLendById(id);
|
||||
if (lend == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
using System.Collections.Generic;
|
||||
using BuecherwurmAPI.Models;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
|
||||
|
||||
namespace BuecherwurmAPI.Data
|
||||
{
|
||||
public interface IRepository
|
||||
{
|
||||
IEnumerable<Lend> GetAllLends();
|
||||
Lend GetLendById(int id);
|
||||
Lend GetLendById(long id);
|
||||
bool IdExits(string table, long id);
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using BuecherwurmAPI.Models;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.VisualBasic.CompilerServices;
|
||||
|
||||
namespace BuecherwurmAPI.Data
|
||||
{
|
||||
|
@ -16,6 +17,33 @@ namespace BuecherwurmAPI.Data
|
|||
_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()
|
||||
{
|
||||
var lends = new List<Lend>();
|
||||
|
@ -28,14 +56,14 @@ namespace BuecherwurmAPI.Data
|
|||
|
||||
while (dataReader.Read())
|
||||
{
|
||||
var returned = (long) dataReader["Returned"] == 0;
|
||||
var returned = (long) dataReader["Returned"] == 1;
|
||||
|
||||
lends.Add(new Lend
|
||||
{
|
||||
Id = (long) dataReader["Id"],
|
||||
Customer = (string) dataReader["Customer"],
|
||||
ItemId = (long) dataReader["ItemId"],
|
||||
Returned = !returned,
|
||||
Returned = returned,
|
||||
ReturnDate = DateTime.Parse((string)dataReader["ReturnDate"])
|
||||
});
|
||||
}
|
||||
|
@ -43,9 +71,30 @@ namespace BuecherwurmAPI.Data
|
|||
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