BuecherwurmAPI/Models/KatalogModel.cs

150 lines
No EOL
6.9 KiB
C#

using System;
using System.Collections.Generic;
using BuecherwurmAPI.Models;
using Microsoft.Data.Sqlite;
using Microsoft.VisualBasic.CompilerServices;
namespace BuecherwurmAPI.Models
{
public interface ICatalogue
{
}
internal class KatalogModel : ICatalogue
{
private SqliteConnection _dbConnection;
public KatalogModel()
{
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 Products";
var dataReader = command.ExecuteReader();
while (dataReader.Read())
{
books.Add(new Book
{
Name = (string) dataReader["Name"],
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 Products WHERE ProductId = @id";
var dataReader = command.ExecuteReader();
var book = new Book
{
Name = (string) dataReader["Name"],
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(long id)
{
using (var command = _dbConnection.CreateCommand())
{
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id;
command.CommandText = @"DELETE * FROM Produkts WHERE ProductId= @id";
var dataReader = command.ExecuteReader();
}
}
public long AddBook (BookPost book)
{
using (var command = _dbConnection.CreateCommand())
{
// funktioniert das so?
//muss ProduktId übergeben werden?
command.CommandText = @"INSERT INTO Products (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("@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("@Category", book.Category));
command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink));
command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime));
command.Parameters.Add(new SqliteParameter("@LendType", book.LendType));
var success = command.ExecuteNonQuery();
if (success == 1)
{
command.CommandText = @"SELECT last_insert_rowid()";
return (long)command.ExecuteScalar();
}
return -1;
}
}
public long EditBook (long id, Book book)
{
using (var command = _dbConnection.CreateCommand())
{
// funktioniert das so?
command.Parameters.Add(new SqliteParameter("@id", id));
command.CommandText = @"UPDATE Products 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("@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("@Category", book.Category));
command.Parameters.Add(new SqliteParameter("@ImageLink", book.ImageLink));
command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime));
command.Parameters.Add(new SqliteParameter("@LendType", book.LendType));
var success = command.ExecuteNonQuery();
return id;
}
}
}
}