BuecherwurmAPI/Controllers/KatalogController.cs

86 lines
2.2 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<Book>> GetAllBooks()
{
var books =_repository.GetAllBooks();
return Ok(books);
}
// POST Katalog
[HttpPost]
2020-06-03 10:43:03 +00:00
public ActionResult<Book> AddBook(BookPost book)
2020-05-28 11:47:02 +00:00
{
2020-06-03 10:43:03 +00:00
var id = _repository.AddBook(book);
2020-05-28 11:47:02 +00:00
return Ok(new Book
{
Name = book.Name,
Author= book.Author,
Country= book.Country,
Link= book.Link,
Language= book.Language,
Pages= book.Pages,
Year=book.Year,
2020-06-03 10:43:03 +00:00
ProductId = id,
2020-05-28 11:47:02 +00:00
Category= book.Category,
ImageLink =book.ImageLink,
LendTime =book.LendTime,
LendType = book.LendType
});
}
// GET katalog/{id}
2020-06-03 10:43:03 +00:00
[HttpGet("{id}")]
public ActionResult <Book> GetBookByID(long id)
2020-05-28 11:47:02 +00:00
{
var book = _repository.GetBookById(id);
if (book != null)
{
return Ok(book);
}
return NoContent();
}
// PUT Katalog/{id}
2020-06-03 10:43:03 +00:00
[HttpPut("{id}")]
public ActionResult<Book> EditBook(long id, Book book)
2020-05-28 11:47:02 +00:00
{
2020-06-03 10:43:03 +00:00
_repository.EditBook(id, book);
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<Book> DeleteBook (long id)
2020-05-28 11:47:02 +00:00
{
var book = _repository.GetBookById(id);
if(book == null)
{
return NotFound();
}
2020-06-03 05:59:31 +00:00
_repository.DeleteBook(id);
2020-05-28 11:47:02 +00:00
return NoContent();
}
}
}