BuecherwurmAPI/Controllers/ItemController.cs

68 lines
1.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using BuecherwurmAPI.Models;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
2020-06-02 12:23:21 +00:00
using Microsoft.Data.Sqlite;
//using Microsoft.EntityFrameworkCore;
2020-06-02 09:16:45 +00:00
namespace BuecherwurmAPI.Controllers
{
2020-06-03 08:45:54 +00:00
[Route("api/inventar")]
[ApiController]
2020-06-02 12:23:21 +00:00
public class ItemController : ControllerBase
{
2020-06-03 08:45:54 +00:00
private readonly ItemModel _repository;
2020-06-03 08:45:54 +00:00
public ItemController(IModel repository)
{
2020-06-03 08:45:54 +00:00
_repository = (ItemModel)repository;
}
2020-06-03 08:45:54 +00:00
// GET api/inventar
[HttpGet]
public ActionResult<IEnumerable<Item>> GetAllItems()
{
var items = _repository.GetAllItems();
return Ok(items);
}
2020-06-03 08:45:54 +00:00
// POST api/inventar
[HttpPost]
public ActionResult<IEnumerable<Item>> NewItem(ItemPost item)
{
var newItem = _repository.NewItem(item);
return Ok(newItem);
}
2020-06-03 07:26:07 +00:00
2020-06-03 08:45:54 +00:00
// GET api/nventar/{id}
[HttpGet("{itemId}")]
public ActionResult<IEnumerable<Item>> GetItemByID(long id)
{
var item = _repository.GetItemById(id);
if (item != null)
{
return Ok(item);
}
return NoContent();
}
2020-06-03 08:45:54 +00:00
// 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();
}
}
}