BuecherwurmAPI/Controllers/KatalogController.cs

79 lines
2.1 KiB
C#
Raw Normal View History

2020-05-28 11:47:02 +00:00
using System.Collections.Generic;
using BuecherwurmAPI.Models;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
//using Microsoft.EntityFrameworkCore;
namespace BuecherwurmAPI.Controllers
{
2020-06-03 10:43:03 +00:00
[Route("api/katalog") ]
2020-05-28 11:47:02 +00:00
[ApiController]
public class KatalogController :ControllerBase
{
2020-06-03 10:43:03 +00:00
private readonly KatalogModel _repository;
2020-05-28 11:47:02 +00:00
2020-06-03 10:43:03 +00:00
public KatalogController (ICatalogue repository)
2020-05-28 11:47:02 +00:00
{
2020-06-03 10:43:03 +00:00
_repository= (KatalogModel)repository;
2020-05-28 11:47:02 +00:00
}
// GET Katalog
[HttpGet]
public ActionResult<IEnumerable<IProduct>> GetAllProducts()
2020-05-28 11:47:02 +00:00
{
var Products =_repository.GetAllProducts();
return Ok(Products);
2020-05-28 11:47:02 +00:00
}
// POST katalog/buch
[HttpPost("buch")]
public ActionResult<Book> AddProduct(BookPost book)
2020-05-28 11:47:02 +00:00
{
var id = _repository.AddProduct(book);
return Ok(_repository.GetProductById(id));
2020-05-28 11:47:02 +00:00
}
// POST katalog/magazin
[HttpPost("magazin")]
public ActionResult<Magazin> AddProduct(MagazinPost book)
{
var id = _repository.AddProduct(book);
return Ok(_repository.GetProductById(id));
}
2020-05-28 11:47:02 +00:00
// GET katalog/{id}
2020-06-03 10:43:03 +00:00
[HttpGet("{id}")]
public ActionResult <IProduct> GetProductByID(long id)
2020-05-28 11:47:02 +00:00
{
var book = _repository.GetProductById(id);
2020-05-28 11:47:02 +00:00
if (book != null)
{
return Ok(book);
}
return NoContent();
}
// PUT Katalog/{id}
2020-06-03 10:43:03 +00:00
[HttpPut("{id}")]
public ActionResult<IProduct> EditProduct(long id, IProduct book)
2020-05-28 11:47:02 +00:00
{
_repository.EditProduct(id, book);
2020-06-03 10:43:03 +00:00
return Ok(book);
2020-05-28 11:47:02 +00:00
}
// DELETE katalog/{id}
2020-06-03 10:43:03 +00:00
[HttpDelete("{id}")]
public ActionResult<IProduct> DeleteProduct (long id)
2020-05-28 11:47:02 +00:00
{
var book = _repository.GetProductById(id);
2020-05-28 11:47:02 +00:00
if(book == null)
{
return NotFound();
}
_repository.DeleteProduct(id);
2020-05-28 11:47:02 +00:00
return NoContent();
}
}
}