79 lines
2.1 KiB
C#
79 lines
2.1 KiB
C#
using System.Collections.Generic;
|
|
using BuecherwurmAPI.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Linq;
|
|
//using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BuecherwurmAPI.Controllers
|
|
{
|
|
[Route("api/katalog") ]
|
|
[ApiController]
|
|
public class KatalogController :ControllerBase
|
|
{
|
|
private readonly KatalogModel _repository;
|
|
|
|
public KatalogController (ICatalogue repository)
|
|
{
|
|
_repository= (KatalogModel)repository;
|
|
}
|
|
// GET Katalog
|
|
[HttpGet]
|
|
public ActionResult<IEnumerable<IProduct>> GetAllProducts()
|
|
{
|
|
var Products =_repository.GetAllProducts();
|
|
return Ok(Products);
|
|
}
|
|
|
|
// POST katalog/buch
|
|
[HttpPost("buch")]
|
|
public ActionResult<Book> AddProduct(BookPost book)
|
|
{
|
|
var id = _repository.AddProduct(book);
|
|
return Ok(_repository.GetProductById(id));
|
|
}
|
|
|
|
// POST katalog/magazin
|
|
[HttpPost("magazin")]
|
|
public ActionResult<Magazin> AddProduct(MagazinPost book)
|
|
{
|
|
var id = _repository.AddProduct(book);
|
|
return Ok(_repository.GetProductById(id));
|
|
}
|
|
|
|
// GET katalog/{id}
|
|
[HttpGet("{id}")]
|
|
public ActionResult <IProduct> GetProductByID(long id)
|
|
{
|
|
var book = _repository.GetProductById(id);
|
|
if (book != null)
|
|
{
|
|
return Ok(book);
|
|
}
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
// PUT Katalog/{id}
|
|
[HttpPut("{id}")]
|
|
public ActionResult<IProduct> EditProduct(long id, IProduct book)
|
|
{
|
|
_repository.EditProduct(id, book);
|
|
return Ok(book);
|
|
}
|
|
|
|
// DELETE katalog/{id}
|
|
[HttpDelete("{id}")]
|
|
public ActionResult<IProduct> DeleteProduct (long id)
|
|
{
|
|
var book = _repository.GetProductById(id);
|
|
if(book == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
_repository.DeleteProduct(id);
|
|
return NoContent();
|
|
}
|
|
|
|
}
|
|
|
|
} |