From 41a53631dddafd7b47840dc1db918daedb4bccd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=B6nbach?= Date: Thu, 28 May 2020 16:14:36 +0200 Subject: [PATCH] =?UTF-8?q?Datenbankunterst=C3=BCtzung=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BuecherwurmAPI.csproj | 8 ++++++ Controllers/LendController.cs | 4 +-- Data/IRepository.cs | 11 ++++++++ Data/Repository.cs | 51 ++++++++++++++++++++++++++++++++++ LongWormMemory.db | Bin 0 -> 12288 bytes Models/Lend.cs | 4 +-- Startup.cs | 2 +- 7 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 LongWormMemory.db diff --git a/BuecherwurmAPI.csproj b/BuecherwurmAPI.csproj index cfb35fd..f1755c4 100644 --- a/BuecherwurmAPI.csproj +++ b/BuecherwurmAPI.csproj @@ -7,6 +7,14 @@ + + + + + + + Always + diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs index 136b429..40cdc93 100644 --- a/Controllers/LendController.cs +++ b/Controllers/LendController.cs @@ -16,10 +16,10 @@ namespace BuecherwurmAPI.Controllers [ApiController] public class LendController : ControllerBase { - private readonly ILendRepo _repository; + private readonly IRepository _repository; private readonly IMapper _mapper; - public LendController(ILendRepo repository, IMapper mapper) + public LendController(IRepository repository, IMapper mapper) { _repository = repository; _mapper = mapper; diff --git a/Data/IRepository.cs b/Data/IRepository.cs index e69de29..d8cc2dd 100644 --- a/Data/IRepository.cs +++ b/Data/IRepository.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using BuecherwurmAPI.Models; + +namespace BuecherwurmAPI.Data +{ + public interface IRepository + { + IEnumerable GetAllLends(); + Lend GetLendById(int id); + } +} \ No newline at end of file diff --git a/Data/Repository.cs b/Data/Repository.cs index e69de29..162a540 100644 --- a/Data/Repository.cs +++ b/Data/Repository.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using BuecherwurmAPI.Models; +using Microsoft.Data.Sqlite; + +namespace BuecherwurmAPI.Data +{ + internal class Repository : IRepository + { + private SqliteConnection _dbConnection; + + public Repository() + { + var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; + _dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); + _dbConnection.Open(); + } + + public IEnumerable GetAllLends() + { + var lends = new List(); + + // using automatically disposes the command after completion + using (var command = _dbConnection.CreateCommand()) + { + command.CommandText = @"SELECT * FROM Lends"; + var dataReader = command.ExecuteReader(); + + while (dataReader.Read()) + { + var returned = (long) dataReader["Returned"] == 0; + + lends.Add(new Lend + { + Id = (long) dataReader["Id"], + Customer = (string) dataReader["Customer"], + ItemId = (long) dataReader["ItemId"], + Returned = !returned, + ReturnDate = DateTime.Parse((string)dataReader["ReturnDate"]) + }); + } + } + return lends; + } + + public Lend GetLendById(int id) + { + return new Lend{Id = 1, Customer = "Nek0", ItemId = 1337, Returned = false, ReturnDate = DateTime.Now}; + } + } +} \ No newline at end of file diff --git a/LongWormMemory.db b/LongWormMemory.db new file mode 100644 index 0000000000000000000000000000000000000000..4b1deb5d1a8e635edc1c9c4ffcc9d800974024f1 GIT binary patch literal 12288 zcmeI%y-vbV6bJBIia<%UE~bl#H=RH&DFwpEq6pOxYtd4pju5U9Zq^bgShw- zZukhkg)PR!p{9w0A>@CP)8X9y({p}nTW3bkW%PRN4BIZvk#)jxWS>$(NX#qVtB6;e zR~KGIz4|_WFYLrfX77E~dy5mZM9i+$!veU600bZa0SG_<0uX=z1Rwx`MHb**7kRqB z&Gr!-do(uVk|c3YcW%2gUoz zo0{i_vFrC;HvH|*V(!!#)!QzU1zW%LLO+7F>BJomnGsKj% zs&q0Pyd=61fB*y_009U<00Izz00bZa0SGLUfd6lv7yD81O?(#n;!K<^({|Ve0uX=z z1Rwwb2tWV=5P$##An@M;3Lhg}Qxdi{RZ|tUplBsKTd3rU6*Zs9m-1S%T-Z^yimD#6 oLGPXrEfOP<=2jy95}Ih;IMYstIS+&=oS)iAtdv7jCc9NX0aEQ`TL1t6 literal 0 HcmV?d00001 diff --git a/Models/Lend.cs b/Models/Lend.cs index b740853..4be118e 100644 --- a/Models/Lend.cs +++ b/Models/Lend.cs @@ -4,8 +4,8 @@ namespace BuecherwurmAPI.Models { public class Lend { - public int Id { get; set; } - public int ItemId { get; set;} + public long Id { get; set; } + public long ItemId { get; set;} public DateTime ReturnDate { get; set; } public string Customer { get; set; } public bool Returned { get; set; } diff --git a/Startup.cs b/Startup.cs index d9a3ef4..8e8c8dc 100644 --- a/Startup.cs +++ b/Startup.cs @@ -32,7 +32,7 @@ namespace BuecherwurmAPI // Adds a service that is created once per connection. // It takes an interface and a specific implementation. // That allows to swap the implementation easily. - services.AddScoped(); + services.AddScoped(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); }