diff --git a/BuecherwurmAPI.csproj b/BuecherwurmAPI.csproj
index fdec702..cfb35fd 100644
--- a/BuecherwurmAPI.csproj
+++ b/BuecherwurmAPI.csproj
@@ -4,8 +4,9 @@
netcoreapp3.1
-
-
+
+
+
diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs
index 559ef27..136b429 100644
--- a/Controllers/LendController.cs
+++ b/Controllers/LendController.cs
@@ -2,7 +2,9 @@
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;
@@ -15,10 +17,12 @@ namespace BuecherwurmAPI.Controllers
public class LendController : ControllerBase
{
private readonly ILendRepo _repository;
+ private readonly IMapper _mapper;
- public LendController(ILendRepo repository)
+ public LendController(ILendRepo repository, IMapper mapper)
{
_repository = repository;
+ _mapper = mapper;
}
//GET api/leihvorgang
@@ -30,9 +34,31 @@ namespace BuecherwurmAPI.Controllers
//POST api/leihvorgang
[HttpPost]
- public ActionResult LendsPost(Lend lend)
+ public ActionResult LendsPost(Lend lend)
{
- return Ok(new Lend{Id = lend.Id, Customer = lend.Customer, Returned = lend.Returned, ItemId = lend.ItemId, ReturnDate = lend.ReturnDate});
+ /*
+ 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}
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/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/Startup.cs b/Startup.cs
index 4e77ad5..d9a3ef4 100644
--- a/Startup.cs
+++ b/Startup.cs
@@ -2,6 +2,7 @@ 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;
@@ -32,6 +33,8 @@ namespace BuecherwurmAPI
// 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.