Browse Source

Added KatalogModel

Amedeo
Naumann 2 years ago
parent
commit
1637f45d0e
  1. 10
      Controllers/KatalogController.cs
  2. 8
      Models/Book.cs
  3. 6
      Models/IBookRepo.cs
  4. 93
      Models/KatalogModel.cs

10
Controllers/KatalogController.cs

@ -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();
}

8
Models/Book.cs

@ -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;}
}

6
Models/IBookRepo.cs

@ -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

@ -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…
Cancel
Save