Added KatalogModel
This commit is contained in:
parent
82df87a8c6
commit
1637f45d0e
4 changed files with 106 additions and 11 deletions
|
@ -26,7 +26,7 @@ namespace BuecherwurmAPI.Controllers
|
||||||
|
|
||||||
// POST Katalog
|
// POST Katalog
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult<IEnumerable<Book>> NeuesBuch(Book book)
|
public ActionResult<IEnumerable<Book>> AddBook(Book book)
|
||||||
{
|
{
|
||||||
|
|
||||||
return Ok(new Book
|
return Ok(new Book
|
||||||
|
@ -49,7 +49,7 @@ namespace BuecherwurmAPI.Controllers
|
||||||
|
|
||||||
// GET katalog/{id}
|
// GET katalog/{id}
|
||||||
[HttpGet("{id}", Name ="GetBookByID")]
|
[HttpGet("{id}", Name ="GetBookByID")]
|
||||||
public ActionResult <IEnumerable<Book>> GetBookByID(int id)
|
public ActionResult <IEnumerable<Book>> GetBookByID(long id)
|
||||||
{
|
{
|
||||||
var book = _repository.GetBookById(id);
|
var book = _repository.GetBookById(id);
|
||||||
if (book != null)
|
if (book != null)
|
||||||
|
@ -62,7 +62,7 @@ namespace BuecherwurmAPI.Controllers
|
||||||
|
|
||||||
// PUT Katalog/{id}
|
// PUT Katalog/{id}
|
||||||
[HttpPut("id")]
|
[HttpPut("id")]
|
||||||
public ActionResult<IEnumerable<Book>> BuchBearbeiten(Book book)
|
public ActionResult<IEnumerable<Book>> EditBook(Book book)
|
||||||
{
|
{
|
||||||
return Ok(new Book
|
return Ok(new Book
|
||||||
{
|
{
|
||||||
|
@ -83,14 +83,14 @@ namespace BuecherwurmAPI.Controllers
|
||||||
|
|
||||||
// DELETE katalog/{id}
|
// DELETE katalog/{id}
|
||||||
[HttpDelete("id")]
|
[HttpDelete("id")]
|
||||||
public ActionResult<IEnumerable<Book>> BuchEntfernen (int id)
|
public ActionResult<IEnumerable<Book>> DeleteBook (long id)
|
||||||
{
|
{
|
||||||
var book = _repository.GetBookById(id);
|
var book = _repository.GetBookById(id);
|
||||||
if(book == null)
|
if(book == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
_repository.BuchEntfernen(book);
|
_repository.DeleteBook(book);
|
||||||
return NoContent();
|
return NoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,14 +9,14 @@ namespace BuecherwurmAPI.Models
|
||||||
public string Country {get; set;}
|
public string Country {get; set;}
|
||||||
public string Link {get; set;}
|
public string Link {get; set;}
|
||||||
public string Language {get; set;}
|
public string Language {get; set;}
|
||||||
public int Pages {get; set;}
|
public long Pages {get; set;}
|
||||||
public int Year {get; set;}
|
public long Year {get; set;}
|
||||||
[Key]
|
[Key]
|
||||||
[Required]
|
[Required]
|
||||||
public int 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; }
|
||||||
public int LendTime {get; set;}
|
public long LendTime {get; set;}
|
||||||
public LendTypeEnum LendType {get; set;}
|
public LendTypeEnum LendType {get; set;}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,9 @@ namespace BuecherwurmAPI.Models
|
||||||
public interface IBookRepo
|
public interface IBookRepo
|
||||||
{
|
{
|
||||||
IEnumerable<Book> GetAllBooks();
|
IEnumerable<Book> GetAllBooks();
|
||||||
Book GetBookById(int id);
|
Book GetBookById(long id);
|
||||||
void BuchEntfernen(Book book);
|
void DeleteBook(Book book);
|
||||||
|
void AddBook (Book book);
|
||||||
|
void EditBook (Book book);
|
||||||
}
|
}
|
||||||
}
|
}
|
93
Models/KatalogModel.cs
Normal file
93
Models/KatalogModel.cs
Normal 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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue