Added inventary data

This commit is contained in:
Naumann 2020-05-28 15:50:14 +02:00
commit f16154cf0a
4 changed files with 102 additions and 89 deletions

View File

@ -11,9 +11,9 @@ namespace BuecherwurmAPI.Controllers
[ApiController] [ApiController]
public class InventarController : ControllerBase public class InventarController : ControllerBase
{ {
private readonly IInventarRepo _repository; private readonly IItemRepo _repository;
public InventarController(IInventarRepo repository) public InventarController(IItemRepo repository)
{ {
_repository = repository; _repository = repository;
} }
@ -32,7 +32,7 @@ namespace BuecherwurmAPI.Controllers
return Ok(new Item return Ok(new Item
{ {
Id = item.Id, Id = item.Id,
BookId = item.BookId, BookId = book.ProductId,
}); });
} }
@ -60,7 +60,7 @@ namespace BuecherwurmAPI.Controllers
{ {
return NotFound(); return NotFound();
} }
_repository.DeleteItem(item); _repository.DeleteItems(item);
return NoContent(); return NoContent();
} }

View File

@ -1,84 +1,84 @@
using System; 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 AutoMapper;
using BuecherwurmAPI.Data; using BuecherwurmAPI.Data;
using BuecherwurmAPI.DTOs; 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;
using BuecherwurmAPI.Models; using BuecherwurmAPI.Models;
namespace BuecherwurmAPI.Controllers namespace BuecherwurmAPI.Controllers
{ {
[Route("api/leihvorgang")] [Route("api/leihvorgang")]
[ApiController] [ApiController]
public class LendController : ControllerBase public class LendController : ControllerBase
{ {
private readonly ILendRepo _repository; private readonly ILendRepo _repository;
private readonly IMapper _mapper; private readonly IMapper _mapper;
public LendController(ILendRepo repository, IMapper mapper) public LendController(ILendRepo repository, IMapper mapper)
{ {
_repository = repository; _repository = repository;
_mapper = mapper; _mapper = mapper;
} }
//GET api/leihvorgang //GET api/leihvorgang
[HttpGet] [HttpGet]
public ActionResult<IEnumerable<Lend>> LendsGet() public ActionResult<IEnumerable<Lend>> LendsGet()
{ {
return Ok(_repository.GetAllLends()); return Ok(_repository.GetAllLends());
} }
//POST api/leihvorgang //POST api/leihvorgang
[HttpPost] [HttpPost]
public ActionResult<LendReadDTO> LendsPost(Lend lend) public ActionResult<LendReadDTO> LendsPost(Lend lend)
{ {
/* /*
Internally a lend is stored with an id Internally a lend is stored with an id
but the client shouldn't be allowed to set or change it but the client shouldn't be allowed to set or change it
therefore the package 'AutoMapper' is used to prevent errors therefore the package 'AutoMapper' is used to prevent errors
that could happen when doing this task manually. that could happen when doing this task manually.
It takes the information from the client and maps it to the It takes the information from the client and maps it to the
corresponding internal object which then will be returned. corresponding internal object which then will be returned.
Furthermore it could be used to keep some attributes secret. Furthermore it could be used to keep some attributes secret.
Another nice effect of this is that the implementation could be changed 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. while the interface could be retained by some minor changes in the code.
DTO stands for Data Transfer Object DTO stands for Data Transfer Object
*/ */
var item = new Lend var item = new Lend
{ {
Id = 256, Id = 256,
Customer = lend.Customer, Customer = lend.Customer,
Returned = lend.Returned, Returned = lend.Returned,
ItemId = lend.ItemId, ItemId = lend.ItemId,
ReturnDate = lend.ReturnDate ReturnDate = lend.ReturnDate
}; };
return Ok(item); return Ok(item);
//return Ok(_mapper.Map<LendReadDTO>(item)); //return Ok(_mapper.Map<LendReadDTO>(item));
} }
//GET api/leihvorgang/{id} //GET api/leihvorgang/{id}
[HttpGet("{id}")] [HttpGet("{id}")]
public ActionResult<Lend> LendById(int id) public ActionResult<Lend> LendById(int id)
{ {
var lend = _repository.GetLendById(id); var lend = _repository.GetLendById(id);
return Ok(lend); return Ok(lend);
} }
//PATCH api/leihvorgang/{id} //PATCH api/leihvorgang/{id}
[HttpPatch("{id}")] [HttpPatch("{id}")]
public ActionResult LendPatchById(int id, JsonPatchDocument<Lend> patchDocument) public ActionResult LendPatchById(int id, JsonPatchDocument<Lend> patchDocument)
{ {
var lend = _repository.GetLendById(id); var lend = _repository.GetLendById(id);
if (lend == null) if (lend == null)
{ {
return NotFound(); return NotFound();
} }
return Ok(); return Ok();
} }
} }
} }

13
Data/IItemRepo.cs Normal file
View File

@ -0,0 +1,13 @@
using System.Collections.Generic;
using BuecherwurmAPI.Models;
namespace BuecherwurmAPI.Data
{
public interface IItemRepo
{
IEnumerable<Item> GetAllItems();
Item GetItemById(int id);
NewItem(Item item);
DeleteItem(int id);
}
}

View File

@ -10,4 +10,4 @@ namespace BuecherwurmAPI.Models
public string Customer { get; set; } public string Customer { get; set; }
public bool Returned { get; set; } public bool Returned { get; set; }
} }
} }