Squashed commit of the following:

commit d155f8fb5b
Author: Jonas Schönbach <jonas.schoenbach@yahoo.de>
Date:   Wed May 27 15:47:14 2020 +0200

    LendController um DTO ergänzt

commit 4afdb44e92
Author: Jonas Schönbach <jonas.schoenbach@yahoo.de>
Date:   Wed May 27 14:31:46 2020 +0200

    LendController um weitere Methoden ergänzt

commit 1c8ec4b517
Author: Jonas Schönbach <jonas.schoenbach@yahoo.de>
Date:   Wed May 27 11:22:29 2020 +0200

    flexiblere Umsetzung des LendControllers durch Depedency-Injection

commit d07c82f749
Author: Jonas Schönbach <jonas.schoenbach@yahoo.de>
Date:   Wed May 27 10:42:15 2020 +0200

    Überflüssige Dateien entfernt

commit e22296617c
Author: Jonas Schönbach <jonas.schoenbach@yahoo.de>
Date:   Wed May 27 10:38:00 2020 +0200

    Überflüssige Dateien entfernt

commit 774c7be7a9
Author: Jonas Schönbach <jonas.schoenbach@yahoo.de>
Date:   Wed May 27 10:33:53 2020 +0200

    .gitignore-Datei berichtigt

commit f00ecd5034
Author: Jonas Schönbach <jonas.schoenbach@yahoo.de>
Date:   Wed May 27 10:30:19 2020 +0200

    .gitignore-Datei hinzugefügt

commit fbe1e58a47
Author: Jonas Schönbach <jonas.schoenbach@yahoo.de>
Date:   Wed May 27 10:08:52 2020 +0200

    Erster Zwischenstand für die Ausleihe-Schnittstelle (mit Mock-Data)
    api/leihvorgang  -- gibt Liste aller Leihvorgänge zurück
    api/leihvorgang/{id} -- gibt bestimmten Leihvorgang zurück
This commit is contained in:
Jonas Schönbach 2020-05-28 10:55:56 +02:00
parent 09bb449838
commit d64c4fed67
12 changed files with 180 additions and 56 deletions

4
.gitignore vendored
View file

@ -586,6 +586,7 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
<<<<<<< HEAD
### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
@ -780,4 +781,7 @@ $RECYCLE.BIN/
# Backup folder for Package Reference Convert tool in Visual Studio 2017
# End of https://www.gitignore.io/api/git,rider,linux,csharp,windows,dotnetcore,aspnetcore,visualstudio,visualstudiocode
=======
# End of https://www.gitignore.io/api/git,rider,linux,csharp,aspnetcore
>>>>>>> ControllerJonas

View file

@ -4,5 +4,10 @@
<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>

View file

@ -0,0 +1,84 @@
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();
}
}
}

View file

@ -1,39 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace BuecherwurmAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}

12
DTOs/LendRead.cs Normal file
View file

@ -0,0 +1,12 @@
using System;
namespace BuecherwurmAPI.DTOs
{
public class LendReadDTO
{
public int ItemId { get; set;}
public DateTime ReturnDate { get; set; }
public string Customer { get; set; }
public bool Returned { get; set; }
}
}

11
Data/ILendRepo.cs Normal file
View file

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

26
Data/MockLendRepo.cs Normal file
View file

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using BuecherwurmAPI.Models;
namespace BuecherwurmAPI.Data
{
public class MockLendRepo : ILendRepo
{
public IEnumerable<Lend> GetAllLends()
{
var lends = new List<Lend>
{
new Lend{Id = 1, Customer = "Nek0", ItemId = 1337, Returned = false, ReturnDate = DateTime.Now},
new Lend{Id = 2, Customer = "Shrubbery", ItemId = 1975, Returned = false, ReturnDate = DateTime.Now},
new Lend{Id = 3, Customer = "Felix", ItemId = 42, Returned = true, ReturnDate = DateTime.Now}
};
return lends;
}
public Lend GetLendById(int id)
{
return new Lend{Id = 1, Customer = "Nek0", ItemId = 1337, Returned = false, ReturnDate = DateTime.Now};
}
}
}

13
Models/Lend.cs Normal file
View file

@ -0,0 +1,13 @@
using System;
namespace BuecherwurmAPI.Models
{
public class Lend
{
public int Id { get; set; }
public int ItemId { get; set;}
public DateTime ReturnDate { get; set; }
public string Customer { get; set; }
public bool Returned { get; set; }
}
}

14
Profiles/LendProfile.cs Normal file
View file

@ -0,0 +1,14 @@
using AutoMapper;
using BuecherwurmAPI.DTOs;
using BuecherwurmAPI.Models;
namespace BuecherwurmAPI.Profiles
{
public class LendProfile : Profile
{
public LendProfile()
{
CreateMap<Lend, LendReadDTO>();
}
}
}

View file

@ -12,7 +12,7 @@
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"launchUrl": "api",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@ -20,7 +20,7 @@
"BuecherwurmAPI": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"launchUrl": "api",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"

View file

@ -2,6 +2,8 @@ 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;
@ -26,6 +28,13 @@ namespace BuecherwurmAPI
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.

View file

@ -1,15 +0,0 @@
using System;
namespace BuecherwurmAPI
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}
}