From ae3cbb0cd3aa66f0cde03225a99cdd77c6c29729 Mon Sep 17 00:00:00 2001 From: Naumann Date: Wed, 3 Jun 2020 10:19:45 +0200 Subject: [PATCH] . --- Controllers/InventarController.cs | 2 +- Models/IBookRepo.cs | 4 +-- Models/KatalogModel.cs | 49 +++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/Controllers/InventarController.cs b/Controllers/InventarController.cs index 2c5e5cd..6f3495e 100644 --- a/Controllers/InventarController.cs +++ b/Controllers/InventarController.cs @@ -32,7 +32,7 @@ namespace BuecherwurmAPI.Controllers var itemid = insertItemReturningId(bookId); return Ok(new Item { - Id = itemId, + Id = itemid, BookId = bookId, }); } diff --git a/Models/IBookRepo.cs b/Models/IBookRepo.cs index bd13521..2bf60e9 100644 --- a/Models/IBookRepo.cs +++ b/Models/IBookRepo.cs @@ -8,7 +8,7 @@ namespace BuecherwurmAPI.Models IEnumerable GetAllBooks(); Book GetBookById(long id); void DeleteBook(long id); - void AddBook (Book book); - void EditBook (long id, Book book); + 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 b8a794d..e1dd357 100644 --- a/Models/KatalogModel.cs +++ b/Models/KatalogModel.cs @@ -78,23 +78,32 @@ namespace BuecherwurmAPI.Models { using (var command = _dbConnection.CreateCommand()) { - // funktioniert das so? 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? - 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)"; + //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("@itemId", lend.ItemId)); - command.Parameters.Add(new SqliteParameter("@returnDate", lend.ReturnDate)); - command.Parameters.Add(new SqliteParameter("@customer", lend.Customer)); + 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(); @@ -108,14 +117,36 @@ namespace BuecherwurmAPI.Models } } - public void EditBook (long id, 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 (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(); + 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; } }