Compare commits
2 commits
22e87cd58a
...
41a53631dd
Author | SHA1 | Date | |
---|---|---|---|
|
41a53631dd | ||
|
904f6c9413 |
|
@ -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>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
11
Data/IRepository.cs
Normal file
11
Data/IRepository.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
51
Data/Repository.cs
Normal file
51
Data/Repository.cs
Normal 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
BIN
LongWormMemory.db
Normal file
Binary file not shown.
|
@ -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; }
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue