LendController um DTO ergänzt

This commit is contained in:
Jonas Schönbach 2020-05-27 15:47:14 +02:00
parent 4afdb44e92
commit d155f8fb5b
5 changed files with 61 additions and 5 deletions

View file

@ -4,8 +4,9 @@
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" /> <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
</ItemGroup> </ItemGroup>

View file

@ -2,7 +2,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper;
using BuecherwurmAPI.Data; using BuecherwurmAPI.Data;
using BuecherwurmAPI.DTOs;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.JsonPatch; using Microsoft.AspNetCore.JsonPatch;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -15,10 +17,12 @@ namespace BuecherwurmAPI.Controllers
public class LendController : ControllerBase public class LendController : ControllerBase
{ {
private readonly ILendRepo _repository; private readonly ILendRepo _repository;
private readonly IMapper _mapper;
public LendController(ILendRepo repository) public LendController(ILendRepo repository, IMapper mapper)
{ {
_repository = repository; _repository = repository;
_mapper = mapper;
} }
//GET api/leihvorgang //GET api/leihvorgang
@ -30,9 +34,31 @@ namespace BuecherwurmAPI.Controllers
//POST api/leihvorgang //POST api/leihvorgang
[HttpPost] [HttpPost]
public ActionResult<Lend> LendsPost(Lend lend) public ActionResult<LendReadDTO> 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<LendReadDTO>(item));
} }
//GET api/leihvorgang/{id} //GET api/leihvorgang/{id}

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

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

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper;
using BuecherwurmAPI.Data; using BuecherwurmAPI.Data;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -32,6 +33,8 @@ namespace BuecherwurmAPI
// It takes an interface and a specific implementation. // It takes an interface and a specific implementation.
// That allows to swap the implementation easily. // That allows to swap the implementation easily.
services.AddScoped<ILendRepo, MockLendRepo>(); 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. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.