BuecherwurmAPI/Controllers/ItemController.cs

72 lines
1.7 KiB
C#

using System.Collections.Generic;
using BuecherwurmAPI.Models;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Microsoft.Data.Sqlite;
//using Microsoft.EntityFrameworkCore;
namespace BuecherwurmAPI.Controllers
{
[Route("api/inventar")]
[ApiController]
public class ItemController : ControllerBase
{
private readonly ItemModel _repository;
public ItemController(IItem repository)
{
_repository = (ItemModel)repository;
}
// GET api/inventar
[HttpGet]
public ActionResult<IEnumerable<Item>> GetAllItems()
{
var items = _repository.GetAllItems();
if (items != null)
{
return Ok(items);
}
return NoContent();
}
// POST api/inventar
[HttpPost]
public ActionResult<IEnumerable<Item>> NewItem(ItemPost item)
{
var newItem = _repository.NewItem(item);
return Ok(newItem);
}
// GET api/nventar/{id}
[HttpGet("{itemId}")]
public ActionResult<IEnumerable<Item>> GetItemByID(long itemId)
{
var item = _repository.GetItemById(itemId);
if (item != null)
{
return Ok(item);
}
return NotFound();
}
// DELETE api/inventar/{id}
[HttpDelete("{itemId}")]
public ActionResult<IEnumerable<Item>> DeleteItem(long itemId)
{
var item = _repository.GetItemById(itemId);
if(item == null)
{
return NotFound();
}
_repository.DeleteItem(itemId);
return NoContent();
}
}
}