Added KatalogModel

This commit is contained in:
Naumann 2020-06-02 12:55:17 +02:00
parent 82df87a8c6
commit 1637f45d0e
4 changed files with 106 additions and 11 deletions

View file

@ -26,7 +26,7 @@ namespace BuecherwurmAPI.Controllers
// POST Katalog
[HttpPost]
public ActionResult<IEnumerable<Book>> NeuesBuch(Book book)
public ActionResult<IEnumerable<Book>> AddBook(Book book)
{
return Ok(new Book
@ -49,7 +49,7 @@ namespace BuecherwurmAPI.Controllers
// GET katalog/{id}
[HttpGet("{id}", Name ="GetBookByID")]
public ActionResult <IEnumerable<Book>> GetBookByID(int id)
public ActionResult <IEnumerable<Book>> GetBookByID(long id)
{
var book = _repository.GetBookById(id);
if (book != null)
@ -62,7 +62,7 @@ namespace BuecherwurmAPI.Controllers
// PUT Katalog/{id}
[HttpPut("id")]
public ActionResult<IEnumerable<Book>> BuchBearbeiten(Book book)
public ActionResult<IEnumerable<Book>> EditBook(Book book)
{
return Ok(new Book
{
@ -83,14 +83,14 @@ namespace BuecherwurmAPI.Controllers
// DELETE katalog/{id}
[HttpDelete("id")]
public ActionResult<IEnumerable<Book>> BuchEntfernen (int id)
public ActionResult<IEnumerable<Book>> DeleteBook (long id)
{
var book = _repository.GetBookById(id);
if(book == null)
{
return NotFound();
}
_repository.BuchEntfernen(book);
_repository.DeleteBook(book);
return NoContent();
}

View file

@ -9,14 +9,14 @@ namespace BuecherwurmAPI.Models
public string Country {get; set;}
public string Link {get; set;}
public string Language {get; set;}
public int Pages {get; set;}
public int Year {get; set;}
public long Pages {get; set;}
public long Year {get; set;}
[Key]
[Required]
public int ProductId { get; set; }
public long ProductId { get; set; }
public CategoryEnum Category { get; set; }
public string ImageLink { get; set; }
public int LendTime {get; set;}
public long LendTime {get; set;}
public LendTypeEnum LendType {get; set;}
}

View file

@ -6,7 +6,9 @@ namespace BuecherwurmAPI.Models
public interface IBookRepo
{
IEnumerable<Book> GetAllBooks();
Book GetBookById(int id);
void BuchEntfernen(Book book);
Book GetBookById(long id);
void DeleteBook(Book book);
void AddBook (Book book);
void EditBook (Book book);
}
}

93
Models/KatalogModel.cs Normal file
View file

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using BuecherwurmAPI.Models;
using Microsoft.Data.Sqlite;
using Microsoft.VisualBasic.CompilerServices;
namespace BuecherwurmAPI.Models
{
internal class Katalog : IBookRepo
{
private SqliteConnection _dbConnection;
public Katalog()
{
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
_dbConnection = new SqliteConnection(connectionBuilder.ConnectionString);
_dbConnection.Open();
}
public IEnumerable<Book> GetAllBooks()
{
var books= new List<Book>();
using (var command = _dbConnection.CreateCommand())
{
command.CommandText = @"SELECT * FROM Books";
var dataReader = command.ExecuteReader();
while (dataReader.Read())
{
books.Add(new Book
{
Name = (string) dataReader["Books"],
Author = (string) dataReader["Author"],
Country = (string) dataReader["Country"],
Link = (string) dataReader["Link"],
Language = (string) dataReader["Language"],
Pages = (long) dataReader["Pages"],
Year = (long) dataReader["Year"],
ProductId = (long) dataReader["ProductId"],
Category = (CategoryEnum) dataReader["Category"],
ImageLink = (string) dataReader["ImageLink"],
LendTime = (long) dataReader["LendTime"],
LendType = (LendTypeEnum) dataReader["LendType"]
});
}
}
return books;
}
public Book GetBookById(long id)
{
using (var command = _dbConnection.CreateCommand())
{
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id;
command.CommandText = @"SELECT * FROM Books WHERE ProductId = @id";
var dataReader = command.ExecuteReader();
var book = new Book
{
Name = (string) dataReader["Books"],
Author = (string) dataReader["Author"],
Country = (string) dataReader["Country"],
Link = (string) dataReader["Link"],
Language = (string) dataReader["Language"],
Pages = (long) dataReader["Pages"],
Year = (long) dataReader["Year"],
ProductId = (long) dataReader["ProductId"],
Category = (CategoryEnum) dataReader["Category"],
ImageLink = (string) dataReader["ImageLink"],
LendTime = (long) dataReader["LendTime"],
LendType = (LendTypeEnum) dataReader["LendType"]
};
return book;
}
}
public void DeleteBook(Book book)
{
}
public void AddBook (Book book)
{
}
public void EditBook (Book book)
{
}
}
}