diff --git a/Controllers/KatalogController.cs b/Controllers/KatalogController.cs index d7c4d52..63fb7be 100644 --- a/Controllers/KatalogController.cs +++ b/Controllers/KatalogController.cs @@ -90,7 +90,7 @@ namespace BuecherwurmAPI.Controllers { return NotFound(); } - _repository.DeleteBook(book); + _repository.DeleteBook(id); return NoContent(); } diff --git a/Models/IBookRepo.cs b/Models/IBookRepo.cs index 9d4aca2..2bf60e9 100644 --- a/Models/IBookRepo.cs +++ b/Models/IBookRepo.cs @@ -7,8 +7,8 @@ namespace BuecherwurmAPI.Models { IEnumerable GetAllBooks(); Book GetBookById(long id); - void DeleteBook(Book book); - void AddBook (Book book); - void EditBook (Book book); + void DeleteBook(long id); + long AddBook (Book book); + long EditBook (long id, Book book); } } \ No newline at end of file diff --git a/Models/KatalogModel.cs b/Models/KatalogModel.cs index 6fcb3c1..e1dd357 100644 --- a/Models/KatalogModel.cs +++ b/Models/KatalogModel.cs @@ -74,19 +74,80 @@ namespace BuecherwurmAPI.Models return book; } } - public void DeleteBook(Book 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 Books WHERE ProductId= @id"; + var dataReader = command.ExecuteReader(); + } } - public void AddBook (Book book) + public long AddBook (Book book) { + using (var command = _dbConnection.CreateCommand()) + { + // funktioniert das so? + //muss ProduktId übergeben werden? + command.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) VALUES (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @ProductId, @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("@Contry", 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("@ProductId", book.ProductId)); + 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 void EditBook (Book book) + public long EditBook (long id, Book book) { + 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 (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @ProductId, @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("@Contry", 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("@ProductId", book.ProductId)); + 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; + } } }