Browse Source

finalized catalogue

Amedeo
nek0 11 months ago
parent
commit
ffd841b526
7 changed files with 48 additions and 52 deletions
  1. +3
    -3
      Controllers/ItemController.cs
  2. +15
    -28
      Controllers/KatalogController.cs
  3. BIN
      LongWormMemory.db
  4. +16
    -2
      Models/Book.cs
  5. +0
    -2
      Models/ItemModel.cs
  6. +13
    -17
      Models/KatalogModel.cs
  7. +1
    -0
      Startup.cs

+ 3
- 3
Controllers/ItemController.cs 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}


+ 15
- 28
Controllers/KatalogController.cs 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)


BIN
LongWormMemory.db View File


+ 16
- 2
Models/Book.cs 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;}
}
}

+ 0
- 2
Models/ItemModel.cs 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"],


+ 13
- 17
Models/KatalogModel.cs 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;
}
}


+ 1
- 0
Startup.cs 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());
}


Loading…
Cancel
Save