2020-06-02 10:55:17 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 05:59:31 +00:00
|
|
|
public void DeleteBook(long id)
|
2020-06-02 10:55:17 +00:00
|
|
|
{
|
2020-06-03 05:37:37 +00:00
|
|
|
using (var command = _dbConnection.CreateCommand())
|
|
|
|
{
|
|
|
|
// funktioniert das so?
|
2020-06-03 05:59:31 +00:00
|
|
|
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id;
|
2020-06-03 05:37:37 +00:00
|
|
|
command.CommandText = @"DELETE * FROM Books WHERE ProductId= @id";
|
|
|
|
var dataReader = command.ExecuteReader();
|
|
|
|
}
|
2020-06-02 10:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AddBook (Book book)
|
|
|
|
{
|
2020-06-03 05:37:37 +00:00
|
|
|
using (var command = _dbConnection.CreateCommand())
|
|
|
|
{
|
|
|
|
// funktioniert das so?
|
2020-06-03 05:59:31 +00:00
|
|
|
command.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) VALUES (book.Name, book.Author, book.Contry, book.Link, book.Language, book.Pages, book.Year, book.ProductId, book.Category, book.ImageLink, book.LendTime, book.LendType)";
|
2020-06-03 07:41:45 +00:00
|
|
|
|
|
|
|
command.Parameters.Add(new SqliteParameter("@itemId", lend.ItemId));
|
|
|
|
command.Parameters.Add(new SqliteParameter("@returnDate", lend.ReturnDate));
|
|
|
|
command.Parameters.Add(new SqliteParameter("@customer", lend.Customer));
|
|
|
|
|
|
|
|
var success = command.ExecuteNonQuery();
|
|
|
|
|
|
|
|
if (success == 1)
|
|
|
|
{
|
|
|
|
command.CommandText = @"SELECT last_insert_rowid()";
|
|
|
|
return (long)command.ExecuteScalar();
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2020-06-03 05:37:37 +00:00
|
|
|
}
|
2020-06-02 10:55:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 05:59:31 +00:00
|
|
|
public void EditBook (long id, Book book)
|
2020-06-02 10:55:17 +00:00
|
|
|
{
|
2020-06-03 05:59:31 +00:00
|
|
|
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 (book.Name, book.Author, book.Contry, book.Link, book.Language, book.Pages, book.Year, book.ProductId, book.Category, book.ImageLink, book.LendTime, book.LendType) WHERE ProductId = @id";
|
|
|
|
var dataReader = command.ExecuteReader();
|
|
|
|
}
|
2020-06-02 10:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|