Compare commits
5 Commits
9738f3a239
...
af576b75d0
Author | SHA1 | Date |
---|---|---|
Jonas Schönbach | af576b75d0 | |
Jonas Schönbach | 6ce6c92210 | |
Jonas Schönbach | 89fcf8abd2 | |
Jonas Schönbach | 41a53631dd | |
Jonas Schönbach | 904f6c9413 |
|
@ -1,3 +1,40 @@
|
||||||
|
<<<<<<< HEAD
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
|
||||||
|
<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>
|
||||||
|
|
||||||
|
|
||||||
|
</Project>
|
||||||
|
||||||| 22e87cd
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
</Project>
|
||||||
|
=======
|
||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
|
@ -7,7 +44,16 @@
|
||||||
<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>
|
||||||
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
>>>>>>> 9738f3a239ed469853270f7334714b5cc40afe5c
|
||||||
|
|
|
@ -1,3 +1,173 @@
|
||||||
|
<<<<<<< HEAD
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
|
using BuecherwurmAPI.Data;
|
||||||
|
using BuecherwurmAPI.DTOs;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.JsonPatch;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using BuecherwurmAPI.Models;
|
||||||
|
|
||||||
|
namespace BuecherwurmAPI.Controllers
|
||||||
|
{
|
||||||
|
[Route("api/leihvorgang")]
|
||||||
|
[ApiController]
|
||||||
|
public class LendController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IRepository _repository;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
public LendController(IRepository repository, IMapper mapper)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
//GET api/leihvorgang
|
||||||
|
[HttpGet]
|
||||||
|
public ActionResult<IEnumerable<Lend>> LendsGet()
|
||||||
|
{
|
||||||
|
return Ok(_repository.GetAllLends());
|
||||||
|
}
|
||||||
|
|
||||||
|
//POST api/leihvorgang
|
||||||
|
[HttpPost]
|
||||||
|
public ActionResult<LendReadDTO> LendsPost(Lend lend)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
Internally a lend is stored with an id
|
||||||
|
but the client shouldn't be allowed to set or change it
|
||||||
|
therefore the package 'AutoMapper' is used to prevent errors
|
||||||
|
that could happen when doing this task manually.
|
||||||
|
It takes the information from the client and maps it to the
|
||||||
|
corresponding internal object which then will be returned.
|
||||||
|
Furthermore it could be used to keep some attributes secret.
|
||||||
|
Another nice effect of this is that the implementation could be changed
|
||||||
|
while the interface could be retained by some minor changes in the code.
|
||||||
|
|
||||||
|
DTO stands for Data Transfer Object
|
||||||
|
*/
|
||||||
|
var item = new Lend
|
||||||
|
{
|
||||||
|
Id = 256,
|
||||||
|
Customer = lend.Customer,
|
||||||
|
Returned = lend.Returned,
|
||||||
|
ItemId = lend.ItemId,
|
||||||
|
ReturnDate = lend.ReturnDate
|
||||||
|
};
|
||||||
|
return Ok(item);
|
||||||
|
//return Ok(_mapper.Map<LendReadDTO>(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
//GET api/leihvorgang/{id}
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public ActionResult<Lend> LendById(long id)
|
||||||
|
{
|
||||||
|
var lend = _repository.GetLendById(id);
|
||||||
|
if (!_repository.IdExits(Tables.Table.Lends, id))
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
return Ok(lend);
|
||||||
|
}
|
||||||
|
|
||||||
|
//PATCH api/leihvorgang/{id}
|
||||||
|
[HttpPatch("{id}")]
|
||||||
|
public ActionResult LendPatchById(int id, JsonPatchDocument<Lend> patchDocument)
|
||||||
|
{
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
||||||| 22e87cd
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
|
using BuecherwurmAPI.Data;
|
||||||
|
using BuecherwurmAPI.DTOs;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.JsonPatch;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using BuecherwurmAPI.Models;
|
||||||
|
|
||||||
|
namespace BuecherwurmAPI.Controllers
|
||||||
|
{
|
||||||
|
[Route("api/leihvorgang")]
|
||||||
|
[ApiController]
|
||||||
|
public class LendController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ILendRepo _repository;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
public LendController(ILendRepo repository, IMapper mapper)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
//GET api/leihvorgang
|
||||||
|
[HttpGet]
|
||||||
|
public ActionResult<IEnumerable<Lend>> LendsGet()
|
||||||
|
{
|
||||||
|
return Ok(_repository.GetAllLends());
|
||||||
|
}
|
||||||
|
|
||||||
|
//POST api/leihvorgang
|
||||||
|
[HttpPost]
|
||||||
|
public ActionResult<LendReadDTO> LendsPost(Lend lend)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
Internally a lend is stored with an id
|
||||||
|
but the client shouldn't be allowed to set or change it
|
||||||
|
therefore the package 'AutoMapper' is used to prevent errors
|
||||||
|
that could happen when doing this task manually.
|
||||||
|
It takes the information from the client and maps it to the
|
||||||
|
corresponding internal object which then will be returned.
|
||||||
|
Furthermore it could be used to keep some attributes secret.
|
||||||
|
Another nice effect of this is that the implementation could be changed
|
||||||
|
while the interface could be retained by some minor changes in the code.
|
||||||
|
|
||||||
|
DTO stands for Data Transfer Object
|
||||||
|
*/
|
||||||
|
var item = new Lend
|
||||||
|
{
|
||||||
|
Id = 256,
|
||||||
|
Customer = lend.Customer,
|
||||||
|
Returned = lend.Returned,
|
||||||
|
ItemId = lend.ItemId,
|
||||||
|
ReturnDate = lend.ReturnDate
|
||||||
|
};
|
||||||
|
return Ok(item);
|
||||||
|
//return Ok(_mapper.Map<LendReadDTO>(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
//GET api/leihvorgang/{id}
|
||||||
|
[HttpGet("{id}")]
|
||||||
|
public ActionResult<Lend> LendById(int id)
|
||||||
|
{
|
||||||
|
var lend = _repository.GetLendById(id);
|
||||||
|
return Ok(lend);
|
||||||
|
}
|
||||||
|
|
||||||
|
//PATCH api/leihvorgang/{id}
|
||||||
|
[HttpPatch("{id}")]
|
||||||
|
public ActionResult LendPatchById(int id, JsonPatchDocument<Lend> patchDocument)
|
||||||
|
{
|
||||||
|
var lend = _repository.GetLendById(id);
|
||||||
|
if (lend == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
=======
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -82,3 +252,4 @@ namespace BuecherwurmAPI.Controllers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
>>>>>>> 9738f3a239ed469853270f7334714b5cc40afe5c
|
||||||
|
|
|
@ -0,0 +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(long id);
|
||||||
|
bool IdExits(string table, long id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,98 @@
|
||||||
|
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"};
|
||||||
|
_dbConnection = new SqliteConnection(connectionBuilder.ConnectionString);
|
||||||
|
_dbConnection.Open();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IdExits(string table, long id)
|
||||||
|
{
|
||||||
|
using (var command = _dbConnection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.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:
|
||||||
|
command.CommandText = @"SELECT EXISTS(SELECT 0 FROM Lends WHERE Id = @Id)";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var dr = command.ExecuteReader();
|
||||||
|
|
||||||
|
long result = 0;
|
||||||
|
while (dr.Read())
|
||||||
|
{
|
||||||
|
result = (long) dr[0];
|
||||||
|
}
|
||||||
|
return result != 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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"] == 1;
|
||||||
|
|
||||||
|
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(long id)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
namespace BuecherwurmAPI.Data
|
||||||
|
{
|
||||||
|
public static class Tables
|
||||||
|
{
|
||||||
|
public struct Table
|
||||||
|
{
|
||||||
|
public const string Lends = "Lends";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
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; }
|
||||||
|
|
124
Startup.cs
124
Startup.cs
|
@ -1,3 +1,126 @@
|
||||||
|
<<<<<<< HEAD
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
|
using BuecherwurmAPI.Data;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.HttpsPolicy;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace BuecherwurmAPI
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
public Startup(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
Configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddControllers();
|
||||||
|
|
||||||
|
// 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<IRepository, Repository>();
|
||||||
|
|
||||||
|
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||||
|
}
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
if (env.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapControllers();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
||||||| 22e87cd
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using AutoMapper;
|
||||||
|
using BuecherwurmAPI.Data;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.HttpsPolicy;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace BuecherwurmAPI
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
public Startup(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
Configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddControllers();
|
||||||
|
|
||||||
|
// 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<ILendRepo, MockLendRepo>();
|
||||||
|
|
||||||
|
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||||
|
}
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
if (env.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseDeveloperExceptionPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.UseRouting();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapControllers();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
=======
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -58,3 +181,4 @@ namespace BuecherwurmAPI
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
>>>>>>> 9738f3a239ed469853270f7334714b5cc40afe5c
|
||||||
|
|
Loading…
Reference in New Issue