finalized catalogue

This commit is contained in:
nek0 2020-06-03 12:43:03 +02:00
parent b79db9d800
commit ffd841b526
7 changed files with 48 additions and 52 deletions

View File

@ -38,14 +38,14 @@ namespace BuecherwurmAPI.Controllers
// GET api/nventar/{id}
[HttpGet("{itemId}")]
public ActionResult<IEnumerable<Item>> GetItemByID(long id)
public ActionResult<IEnumerable<Item>> GetItemByID(long itemId)
{
var item = _repository.GetItemById(id);
var item = _repository.GetItemById(itemId);
if (item != null)
{
return Ok(item);
}
return NoContent();
return NotFound();
}
// DELETE api/inventar/{id}

View File

@ -6,15 +6,15 @@ using System.Linq;
namespace BuecherwurmAPI.Controllers
{
[Route("katalog") ]
[Route("api/katalog") ]
[ApiController]
public class KatalogController :ControllerBase
{
private readonly IBookRepo _repository;
private readonly KatalogModel _repository;
public KatalogController (IBookRepo repository)
public KatalogController (ICatalogue repository)
{
_repository=repository;
_repository= (KatalogModel)repository;
}
// GET Katalog
[HttpGet]
@ -26,9 +26,9 @@ namespace BuecherwurmAPI.Controllers
// POST Katalog
[HttpPost]
public ActionResult<IEnumerable<Book>> AddBook(Book book)
public ActionResult<Book> AddBook(BookPost book)
{
var id = _repository.AddBook(book);
return Ok(new Book
{
Name = book.Name,
@ -38,7 +38,7 @@ namespace BuecherwurmAPI.Controllers
Language= book.Language,
Pages= book.Pages,
Year=book.Year,
ProductId =book.ProductId,
ProductId = id,
Category= book.Category,
ImageLink =book.ImageLink,
LendTime =book.LendTime,
@ -48,8 +48,8 @@ namespace BuecherwurmAPI.Controllers
// GET katalog/{id}
[HttpGet("{id}", Name ="GetBookByID")]
public ActionResult <IEnumerable<Book>> GetBookByID(long id)
[HttpGet("{id}")]
public ActionResult <Book> GetBookByID(long id)
{
var book = _repository.GetBookById(id);
if (book != null)
@ -61,29 +61,16 @@ namespace BuecherwurmAPI.Controllers
}
// PUT Katalog/{id}
[HttpPut("id")]
public ActionResult<IEnumerable<Book>> EditBook(Book book)
[HttpPut("{id}")]
public ActionResult<Book> EditBook(long id, Book book)
{
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,
ProductId =book.ProductId,
Category= book.Category,
ImageLink =book.ImageLink,
LendTime =book.LendTime,
LendType = book.LendType
});
_repository.EditBook(id, book);
return Ok(book);
}
// DELETE katalog/{id}
[HttpDelete("id")]
public ActionResult<IEnumerable<Book>> DeleteBook (long id)
[HttpDelete("{id}")]
public ActionResult<Book> DeleteBook (long id)
{
var book = _repository.GetBookById(id);
if(book == null)

Binary file not shown.

View File

@ -11,8 +11,7 @@ namespace BuecherwurmAPI.Models
public string Language {get; set;}
public long Pages {get; set;}
public long Year {get; set;}
[Key]
[Required]
[Key, Required]
public long ProductId { get; set; }
public CategoryEnum Category { get; set; }
public string ImageLink { get; set; }
@ -20,4 +19,19 @@ namespace BuecherwurmAPI.Models
public LendTypeEnum LendType {get; set;}
}
public class BookPost
{
public string Name {get; set;}
public string Author {get; set;}
public string Country {get; set;}
public string Link {get; set;}
public string Language {get; set;}
public long Pages {get; set;}
public long Year {get; set;}
public CategoryEnum Category { get; set; }
public string ImageLink { get; set; }
public long LendTime {get; set;}
public LendTypeEnum LendType {get; set;}
}
}

View File

@ -59,8 +59,6 @@ namespace BuecherwurmAPI.Models
while (dataReader.Read())
{
var returned = (long) dataReader["Returned"] == 1;
items.Add(new Item
{
ItemId = (long) dataReader["ItemId"],

View File

@ -6,11 +6,15 @@ using Microsoft.VisualBasic.CompilerServices;
namespace BuecherwurmAPI.Models
{
internal class Katalog : IBookRepo
public interface ICatalogue
{
}
internal class KatalogModel : ICatalogue
{
private SqliteConnection _dbConnection;
public Katalog()
public KatalogModel()
{
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
_dbConnection = new SqliteConnection(connectionBuilder.ConnectionString);
@ -84,22 +88,21 @@ namespace BuecherwurmAPI.Models
}
}
public long AddBook (Book book)
public long AddBook (BookPost book)
{
using (var command = _dbConnection.CreateCommand())
{
// funktioniert das so?
//muss ProduktId übergeben werden?
command.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) VALUES (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @ProductId, @Category, @ImageLink, @LendTime, @LendType )";
command.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) VALUES (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, NULL, @Category, @ImageLink, @LendTime, @LendType )";
command.Parameters.Add(new SqliteParameter("@Name", book.Name));
command.Parameters.Add(new SqliteParameter("@Author", book.Author));
command.Parameters.Add(new SqliteParameter("@Contry", book.Country));
command.Parameters.Add(new SqliteParameter("@Country", book.Country));
command.Parameters.Add(new SqliteParameter("@Link", book.Link));
command.Parameters.Add(new SqliteParameter("@Language", book.Language));
command.Parameters.Add(new SqliteParameter("@Pages", book.Pages));
command.Parameters.Add(new SqliteParameter("@Year", book.Year));
command.Parameters.Add(new SqliteParameter("@ProductId", book.ProductId));
command.Parameters.Add(new SqliteParameter("@Category", book.Category));
command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink));
command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime));
@ -122,17 +125,16 @@ namespace BuecherwurmAPI.Models
using (var command = _dbConnection.CreateCommand())
{
// funktioniert das so?
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id;
command.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) VALUES (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @ProductId, @Category, @ImageLink, @LendTime, @LendType )";
command.Parameters.Add(new SqliteParameter("@id", id));
command.CommandText = @"UPDATE Books SET (Name, Author, Country, Link, Language, Pages, Year, Category, ImageLink, LendTime, LendType) = (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @Category, @ImageLink, @LendTime, @LendType ) WHERE ProductId = @id";
command.Parameters.Add(new SqliteParameter("@Name", book.Name));
command.Parameters.Add(new SqliteParameter("@Author", book.Author));
command.Parameters.Add(new SqliteParameter("@Contry", book.Country));
command.Parameters.Add(new SqliteParameter("@Country", book.Country));
command.Parameters.Add(new SqliteParameter("@Link", book.Link));
command.Parameters.Add(new SqliteParameter("@Language", book.Language));
command.Parameters.Add(new SqliteParameter("@Pages", book.Pages));
command.Parameters.Add(new SqliteParameter("@Year", book.Year));
command.Parameters.Add(new SqliteParameter("@ProductId", book.ProductId));
command.Parameters.Add(new SqliteParameter("@Category", book.Category));
command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink));
command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime));
@ -140,13 +142,7 @@ namespace BuecherwurmAPI.Models
var success = command.ExecuteNonQuery();
if (success == 1)
{
command.CommandText = @"SELECT last_insert_rowid()";
return (long)command.ExecuteScalar();
}
return -1;
return id;
}
}

View File

@ -34,6 +34,7 @@ namespace BuecherwurmAPI
// That allows to swap the implementation easily.
services.AddScoped<ILend, LendModel>();
services.AddScoped<IItem, ItemModel>();
services.AddScoped<ICatalogue, KatalogModel>();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
}