BuecherwurmAPI/Controllers/LendController.cs

85 lines
2.7 KiB
C#
Raw Normal View History

Squashed commit of the following: commit d155f8fb5b4af1b8ed569c4a535d9d94bce75b7f Author: Jonas Schönbach <jonas.schoenbach@yahoo.de> Date: Wed May 27 15:47:14 2020 +0200 LendController um DTO ergänzt commit 4afdb44e920592eab474f0bd7342b2b03d7f0252 Author: Jonas Schönbach <jonas.schoenbach@yahoo.de> Date: Wed May 27 14:31:46 2020 +0200 LendController um weitere Methoden ergänzt commit 1c8ec4b517d5f0d6dc202187a8d38f2b474654be Author: Jonas Schönbach <jonas.schoenbach@yahoo.de> Date: Wed May 27 11:22:29 2020 +0200 flexiblere Umsetzung des LendControllers durch Depedency-Injection commit d07c82f7490ed471ce3eb3833fec9a0c31efb23f Author: Jonas Schönbach <jonas.schoenbach@yahoo.de> Date: Wed May 27 10:42:15 2020 +0200 Überflüssige Dateien entfernt commit e22296617c15454aabfd187ce702423d5f9ee9e3 Author: Jonas Schönbach <jonas.schoenbach@yahoo.de> Date: Wed May 27 10:38:00 2020 +0200 Überflüssige Dateien entfernt commit 774c7be7a970e291d3ac659678db495252d331c2 Author: Jonas Schönbach <jonas.schoenbach@yahoo.de> Date: Wed May 27 10:33:53 2020 +0200 .gitignore-Datei berichtigt commit f00ecd503432f265bcc3a3c42b0eeba608b4c1c7 Author: Jonas Schönbach <jonas.schoenbach@yahoo.de> Date: Wed May 27 10:30:19 2020 +0200 .gitignore-Datei hinzugefügt commit fbe1e58a4733b356427655341a5d8e2296d80cf0 Author: Jonas Schönbach <jonas.schoenbach@yahoo.de> 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
2020-05-28 08:55:56 +00:00
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<IEnumerable<Lend>> LendsGet()
{
return Ok(_repository.GetAllLends());
}
//POST api/leihvorgang
[HttpPost]
public ActionResult<LendReadDTO> 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<LendReadDTO>(item));
}
//GET api/leihvorgang/{id}
[HttpGet("{id}")]
public ActionResult<Lend> LendById(int id)
{
var lend = _repository.GetLendById(id);
return Ok(lend);
}
//PATCH api/leihvorgang/{id}
[HttpPatch("{id}")]
public ActionResult LendPatchById(int id, JsonPatchDocument<Lend> patchDocument)
{
var lend = _repository.GetLendById(id);
if (lend == null)
{
return NotFound();
}
return Ok();
}
}
}