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} // GET api/nventar/{id}
[HttpGet("{itemId}")] [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) if (item != null)
{ {
return Ok(item); return Ok(item);
} }
return NoContent(); return NotFound();
} }
// DELETE api/inventar/{id} // DELETE api/inventar/{id}

View file

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

Binary file not shown.

View file

@ -11,8 +11,7 @@ namespace BuecherwurmAPI.Models
public string Language {get; set;} public string Language {get; set;}
public long Pages {get; set;} public long Pages {get; set;}
public long Year {get; set;} public long Year {get; set;}
[Key] [Key, Required]
[Required]
public long ProductId { get; set; } public long ProductId { get; set; }
public CategoryEnum Category { get; set; } public CategoryEnum Category { get; set; }
public string ImageLink { get; set; } public string ImageLink { get; set; }
@ -20,4 +19,19 @@ namespace BuecherwurmAPI.Models
public LendTypeEnum LendType {get; set;} 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()) while (dataReader.Read())
{ {
var returned = (long) dataReader["Returned"] == 1;
items.Add(new Item items.Add(new Item
{ {
ItemId = (long) dataReader["ItemId"], ItemId = (long) dataReader["ItemId"],

View file

@ -6,11 +6,15 @@ using Microsoft.VisualBasic.CompilerServices;
namespace BuecherwurmAPI.Models namespace BuecherwurmAPI.Models
{ {
internal class Katalog : IBookRepo public interface ICatalogue
{
}
internal class KatalogModel : ICatalogue
{ {
private SqliteConnection _dbConnection; private SqliteConnection _dbConnection;
public Katalog() public KatalogModel()
{ {
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
_dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); _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()) using (var command = _dbConnection.CreateCommand())
{ {
// funktioniert das so? // funktioniert das so?
//muss ProduktId übergeben werden? //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("@Name", book.Name));
command.Parameters.Add(new SqliteParameter("@Author", book.Author)); 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("@Link", book.Link));
command.Parameters.Add(new SqliteParameter("@Language", book.Language)); command.Parameters.Add(new SqliteParameter("@Language", book.Language));
command.Parameters.Add(new SqliteParameter("@Pages", book.Pages)); command.Parameters.Add(new SqliteParameter("@Pages", book.Pages));
command.Parameters.Add(new SqliteParameter("@Year", book.Year)); 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("@Category", book.Category));
command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink)); command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink));
command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime)); command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime));
@ -122,17 +125,16 @@ namespace BuecherwurmAPI.Models
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
// funktioniert das so? // funktioniert das so?
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; command.Parameters.Add(new SqliteParameter("@id", 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.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("@Name", book.Name));
command.Parameters.Add(new SqliteParameter("@Author", book.Author)); 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("@Link", book.Link));
command.Parameters.Add(new SqliteParameter("@Language", book.Language)); command.Parameters.Add(new SqliteParameter("@Language", book.Language));
command.Parameters.Add(new SqliteParameter("@Pages", book.Pages)); command.Parameters.Add(new SqliteParameter("@Pages", book.Pages));
command.Parameters.Add(new SqliteParameter("@Year", book.Year)); 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("@Category", book.Category));
command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink)); command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink));
command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime)); command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime));
@ -140,13 +142,7 @@ namespace BuecherwurmAPI.Models
var success = command.ExecuteNonQuery(); var success = command.ExecuteNonQuery();
if (success == 1) return id;
{
command.CommandText = @"SELECT last_insert_rowid()";
return (long)command.ExecuteScalar();
}
return -1;
} }
} }

View file

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