BuecherwurmAPI/Controllers/LendController.cs
2020-06-04 13:43:01 +02:00

79 lines
No EOL
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using BuecherwurmAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.Extensions.Logging;
using Microsoft.Data.Sqlite;
namespace BuecherwurmAPI.Controllers
{
[Route("api/leihvorgang")]
[ApiController]
public class LendController : ControllerBase
{
private readonly LendModel _repository;
//private readonly IMapper _mapper;
public LendController(ILend repo)
{
_repository = (LendModel)repo;
//_mapper = mapper;
}
//GET api/leihvorgang
[HttpGet]
public ActionResult<IEnumerable<Lend>> LendsGet()
{
return Ok(_repository.GetAllLends());
}
//POST api/leihvorgang
[HttpPost]
public ActionResult<LendPost> LendsPost(LendPost lend)
{
var newId = _repository.AddLend(lend);
if (newId > 0)
{
var item = new Lend
{
Id = newId,
Customer = lend.Customer,
Returned = false,
ItemId = lend.ItemId,
ReturnDate = lend.ReturnDate
};
return Ok(item);
}
else
{
return BadRequest();
}
//return Ok(_mapper.Map<LendReadDTO>(item));
}
//GET api/leihvorgang/{id}
[HttpGet("{id}")]
public ActionResult<Lend> LendById(long id)
{
var lend = _repository.GetLendById(id);
if (!_repository.IdExits(Tables.Table.Lends, id))
{
return NotFound();
}
return Ok(lend);
}
//PUT api/leihvorgang/{id}
[HttpPut("{id}")]
public ActionResult LendPatchById(long id, LendPost lend)
{
var lendPatch = _repository.insertLendReturningId(id, lend);
return Ok(lendPatch);
}
}
}