From d64c4fed672c0d2216333db29763fbc8142622d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=B6nbach?= Date: Thu, 28 May 2020 10:55:56 +0200 Subject: [PATCH] Squashed commit of the following: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit commit d155f8fb5b4af1b8ed569c4a535d9d94bce75b7f Author: Jonas Schönbach Date: Wed May 27 15:47:14 2020 +0200 LendController um DTO ergänzt commit 4afdb44e920592eab474f0bd7342b2b03d7f0252 Author: Jonas Schönbach Date: Wed May 27 14:31:46 2020 +0200 LendController um weitere Methoden ergänzt commit 1c8ec4b517d5f0d6dc202187a8d38f2b474654be Author: Jonas Schönbach Date: Wed May 27 11:22:29 2020 +0200 flexiblere Umsetzung des LendControllers durch Depedency-Injection commit d07c82f7490ed471ce3eb3833fec9a0c31efb23f Author: Jonas Schönbach Date: Wed May 27 10:42:15 2020 +0200 Überflüssige Dateien entfernt commit e22296617c15454aabfd187ce702423d5f9ee9e3 Author: Jonas Schönbach Date: Wed May 27 10:38:00 2020 +0200 Überflüssige Dateien entfernt commit 774c7be7a970e291d3ac659678db495252d331c2 Author: Jonas Schönbach Date: Wed May 27 10:33:53 2020 +0200 .gitignore-Datei berichtigt commit f00ecd503432f265bcc3a3c42b0eeba608b4c1c7 Author: Jonas Schönbach Date: Wed May 27 10:30:19 2020 +0200 .gitignore-Datei hinzugefügt commit fbe1e58a4733b356427655341a5d8e2296d80cf0 Author: Jonas Schönbach 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 --- .gitignore | 4 ++ BuecherwurmAPI.csproj | 5 ++ Controllers/LendController.cs | 84 ++++++++++++++++++++++++ Controllers/WeatherForecastController.cs | 39 ----------- DTOs/LendRead.cs | 12 ++++ Data/ILendRepo.cs | 11 ++++ Data/MockLendRepo.cs | 26 ++++++++ Models/Lend.cs | 13 ++++ Profiles/LendProfile.cs | 14 ++++ Properties/launchSettings.json | 4 +- Startup.cs | 9 +++ WeatherForecast.cs | 15 ----- 12 files changed, 180 insertions(+), 56 deletions(-) create mode 100644 Controllers/LendController.cs delete mode 100644 Controllers/WeatherForecastController.cs create mode 100644 DTOs/LendRead.cs create mode 100644 Data/ILendRepo.cs create mode 100644 Data/MockLendRepo.cs create mode 100644 Models/Lend.cs create mode 100644 Profiles/LendProfile.cs delete mode 100644 WeatherForecast.cs diff --git a/.gitignore b/.gitignore index ae426e7..ec1b020 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/BuecherwurmAPI.csproj b/BuecherwurmAPI.csproj index 400722f..cfb35fd 100644 --- a/BuecherwurmAPI.csproj +++ b/BuecherwurmAPI.csproj @@ -4,5 +4,10 @@ netcoreapp3.1 + + + + + diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs new file mode 100644 index 0000000..136b429 --- /dev/null +++ b/Controllers/LendController.cs @@ -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> LendsGet() + { + return Ok(_repository.GetAllLends()); + } + + //POST api/leihvorgang + [HttpPost] + public ActionResult 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(item)); + } + + //GET api/leihvorgang/{id} + [HttpGet("{id}")] + public ActionResult LendById(int id) + { + var lend = _repository.GetLendById(id); + return Ok(lend); + } + + //PATCH api/leihvorgang/{id} + [HttpPatch("{id}")] + public ActionResult LendPatchById(int id, JsonPatchDocument patchDocument) + { + var lend = _repository.GetLendById(id); + if (lend == null) + { + return NotFound(); + } + return Ok(); + } + } +} diff --git a/Controllers/WeatherForecastController.cs b/Controllers/WeatherForecastController.cs deleted file mode 100644 index 74bac7e..0000000 --- a/Controllers/WeatherForecastController.cs +++ /dev/null @@ -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 _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet] - public IEnumerable 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(); - } - } -} diff --git a/DTOs/LendRead.cs b/DTOs/LendRead.cs new file mode 100644 index 0000000..5813213 --- /dev/null +++ b/DTOs/LendRead.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Data/ILendRepo.cs b/Data/ILendRepo.cs new file mode 100644 index 0000000..d392ae6 --- /dev/null +++ b/Data/ILendRepo.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using BuecherwurmAPI.Models; + +namespace BuecherwurmAPI.Data +{ + public interface ILendRepo + { + IEnumerable GetAllLends(); + Lend GetLendById(int id); + } +} \ No newline at end of file diff --git a/Data/MockLendRepo.cs b/Data/MockLendRepo.cs new file mode 100644 index 0000000..717342c --- /dev/null +++ b/Data/MockLendRepo.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using BuecherwurmAPI.Models; + +namespace BuecherwurmAPI.Data +{ + public class MockLendRepo : ILendRepo + { + public IEnumerable GetAllLends() + { + var lends = new List + { + 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}; + } + } +} \ No newline at end of file diff --git a/Models/Lend.cs b/Models/Lend.cs new file mode 100644 index 0000000..b740853 --- /dev/null +++ b/Models/Lend.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Profiles/LendProfile.cs b/Profiles/LendProfile.cs new file mode 100644 index 0000000..074931c --- /dev/null +++ b/Profiles/LendProfile.cs @@ -0,0 +1,14 @@ +using AutoMapper; +using BuecherwurmAPI.DTOs; +using BuecherwurmAPI.Models; + +namespace BuecherwurmAPI.Profiles +{ + public class LendProfile : Profile + { + public LendProfile() + { + CreateMap(); + } + } +} \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index 106829e..fa2e929 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -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" diff --git a/Startup.cs b/Startup.cs index 1bdfe77..d9a3ef4 100644 --- a/Startup.cs +++ b/Startup.cs @@ -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(); + + services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/WeatherForecast.cs b/WeatherForecast.cs deleted file mode 100644 index 7046a72..0000000 --- a/WeatherForecast.cs +++ /dev/null @@ -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; } - } -}