Datenbankunterstützung hinzugefügt

This commit is contained in:
Jonas Schönbach 2020-05-28 16:14:36 +02:00
parent 904f6c9413
commit 41a53631dd
7 changed files with 75 additions and 5 deletions

View file

@ -7,6 +7,14 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" /> <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.4" />
</ItemGroup>
<ItemGroup>
<None Remove="LongWormMemory.db" />
<Resource Include="LongWormMemory.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup> </ItemGroup>

View file

@ -16,10 +16,10 @@ namespace BuecherwurmAPI.Controllers
[ApiController] [ApiController]
public class LendController : ControllerBase public class LendController : ControllerBase
{ {
private readonly ILendRepo _repository; private readonly IRepository _repository;
private readonly IMapper _mapper; private readonly IMapper _mapper;
public LendController(ILendRepo repository, IMapper mapper) public LendController(IRepository repository, IMapper mapper)
{ {
_repository = repository; _repository = repository;
_mapper = mapper; _mapper = mapper;

View file

@ -0,0 +1,11 @@
using System.Collections.Generic;
using BuecherwurmAPI.Models;
namespace BuecherwurmAPI.Data
{
public interface IRepository
{
IEnumerable<Lend> GetAllLends();
Lend GetLendById(int id);
}
}

View file

@ -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<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())
{
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};
}
}
}

BIN
LongWormMemory.db Normal file

Binary file not shown.

View file

@ -4,8 +4,8 @@ namespace BuecherwurmAPI.Models
{ {
public class Lend public class Lend
{ {
public int Id { get; set; } public long Id { get; set; }
public int ItemId { get; set;} public long ItemId { get; set;}
public DateTime ReturnDate { get; set; } public DateTime ReturnDate { get; set; }
public string Customer { get; set; } public string Customer { get; set; }
public bool Returned { get; set; } public bool Returned { get; set; }

View file

@ -32,7 +32,7 @@ namespace BuecherwurmAPI
// Adds a service that is created once per connection. // Adds a service that is created once per connection.
// It takes an interface and a specific implementation. // It takes an interface and a specific implementation.
// That allows to swap the implementation easily. // That allows to swap the implementation easily.
services.AddScoped<ILendRepo, MockLendRepo>(); services.AddScoped<IRepository, Repository>();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
} }