diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs index 40cdc93..7b7fb24 100644 --- a/Controllers/LendController.cs +++ b/Controllers/LendController.cs @@ -63,9 +63,13 @@ namespace BuecherwurmAPI.Controllers //GET api/leihvorgang/{id} [HttpGet("{id}")] - public ActionResult LendById(int id) + public ActionResult 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 patchDocument) { - var lend = _repository.GetLendById(id); - if (lend == null) - { - return NotFound(); - } return Ok(); } } diff --git a/Data/IRepository.cs b/Data/IRepository.cs index d8cc2dd..9952ee9 100644 --- a/Data/IRepository.cs +++ b/Data/IRepository.cs @@ -1,11 +1,13 @@ using System.Collections.Generic; using BuecherwurmAPI.Models; +using Microsoft.EntityFrameworkCore.Metadata.Conventions; namespace BuecherwurmAPI.Data { public interface IRepository { IEnumerable GetAllLends(); - Lend GetLendById(int id); + Lend GetLendById(long id); + bool IdExits(string table, long id); } } \ No newline at end of file diff --git a/Data/Repository.cs b/Data/Repository.cs index 162a540..40c4e41 100644 --- a/Data/Repository.cs +++ b/Data/Repository.cs @@ -2,13 +2,14 @@ using System; using System.Collections.Generic; using BuecherwurmAPI.Models; using Microsoft.Data.Sqlite; +using Microsoft.VisualBasic.CompilerServices; namespace BuecherwurmAPI.Data { internal class Repository : IRepository { private SqliteConnection _dbConnection; - + public Repository() { var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; @@ -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 GetAllLends() { var lends = new List(); @@ -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; } } } \ No newline at end of file diff --git a/Data/Tables.cs b/Data/Tables.cs new file mode 100644 index 0000000..ba45809 --- /dev/null +++ b/Data/Tables.cs @@ -0,0 +1,10 @@ +namespace BuecherwurmAPI.Data +{ + public static class Tables + { + public struct Table + { + public const string Lends = "Lends"; + } + } +} \ No newline at end of file