LendController um DTO ergänzt
This commit is contained in:
parent
4afdb44e92
commit
d155f8fb5b
5 changed files with 61 additions and 5 deletions
|
@ -4,8 +4,9 @@
|
|||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
|
|
@ -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<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}
|
||||
|
|
12
DTOs/LendRead.cs
Normal file
12
DTOs/LendRead.cs
Normal 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
14
Profiles/LendProfile.cs
Normal 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>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<ILendRepo, MockLendRepo>();
|
||||
|
||||
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
|
Loading…
Reference in a new issue