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; } - } -}