From 226faafafcf6537e5594e6deb547ee5af945d606 Mon Sep 17 00:00:00 2001 From: Naumann Date: Wed, 3 Jun 2020 07:37:37 +0200 Subject: [PATCH 1/5] added new Methodes to KatalogModel --- Models/KatalogModel.cs | 16 ++++++++++++++-- Models/Lend.cs | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Models/KatalogModel.cs b/Models/KatalogModel.cs index 6fcb3c1..7e28488 100644 --- a/Models/KatalogModel.cs +++ b/Models/KatalogModel.cs @@ -76,12 +76,24 @@ namespace BuecherwurmAPI.Models } public void DeleteBook(Book book) { - + using (var command = _dbConnection.CreateCommand()) + { + // funktioniert das so? + command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = book.ProductId; + command.CommandText = @"DELETE * FROM Books WHERE ProductId= @id"; + var dataReader = command.ExecuteReader(); + } } public void AddBook (Book book) { - + using (var command = _dbConnection.CreateCommand()) + { + // funktioniert das so? + command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = book.ProductId; + command.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) "; + var dataReader = command.ExecuteReader(); + } } public void EditBook (Book book) diff --git a/Models/Lend.cs b/Models/Lend.cs index d2e7954..5bd643b 100644 --- a/Models/Lend.cs +++ b/Models/Lend.cs @@ -1,9 +1,12 @@ using System; +using System.ComponentModel.DataAnnotations; namespace BuecherwurmAPI.Models { public class Lend { + [Key] + [Required] public long Id { get; set; } public long ItemId { get; set;} public DateTime ReturnDate { get; set; } From a1ffee2e09e11a1c23e66026128b9b912a69f5af Mon Sep 17 00:00:00 2001 From: Naumann Date: Wed, 3 Jun 2020 07:59:31 +0200 Subject: [PATCH 2/5] fixed AddBook and EditBook --- Controllers/KatalogController.cs | 2 +- Models/IBookRepo.cs | 4 ++-- Models/KatalogModel.cs | 17 +++++++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) 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..bd13521 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 DeleteBook(long id); void AddBook (Book book); - void EditBook (Book book); + void EditBook (long id, Book book); } } \ No newline at end of file diff --git a/Models/KatalogModel.cs b/Models/KatalogModel.cs index 7e28488..f5e5823 100644 --- a/Models/KatalogModel.cs +++ b/Models/KatalogModel.cs @@ -74,12 +74,12 @@ namespace BuecherwurmAPI.Models return book; } } - public void DeleteBook(Book book) + public void DeleteBook(long id) { using (var command = _dbConnection.CreateCommand()) { // funktioniert das so? - command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = book.ProductId; + command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; command.CommandText = @"DELETE * FROM Books WHERE ProductId= @id"; var dataReader = command.ExecuteReader(); } @@ -90,15 +90,20 @@ namespace BuecherwurmAPI.Models using (var command = _dbConnection.CreateCommand()) { // funktioniert das so? - command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = book.ProductId; - command.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) "; + 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)"; var dataReader = command.ExecuteReader(); } } - public void EditBook (Book book) + public void 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(); + } } } From 67eb721d3899ccce3709c6274c2bc4665362816c Mon Sep 17 00:00:00 2001 From: Naumann Date: Wed, 3 Jun 2020 09:41:45 +0200 Subject: [PATCH 3/5] zwischenstand --- Models/KatalogModel.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Models/KatalogModel.cs b/Models/KatalogModel.cs index f5e5823..b8a794d 100644 --- a/Models/KatalogModel.cs +++ b/Models/KatalogModel.cs @@ -91,7 +91,20 @@ namespace BuecherwurmAPI.Models { // 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)"; - var dataReader = command.ExecuteReader(); + + 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; } } From ae3cbb0cd3aa66f0cde03225a99cdd77c6c29729 Mon Sep 17 00:00:00 2001 From: Naumann Date: Wed, 3 Jun 2020 10:19:45 +0200 Subject: [PATCH 4/5] . --- 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; } } From da74cbc6d5b523361481fd35668741212ed34f2c Mon Sep 17 00:00:00 2001 From: Naumann Date: Wed, 3 Jun 2020 11:34:06 +0200 Subject: [PATCH 5/5] . --- Controllers/InventarController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controllers/InventarController.cs b/Controllers/InventarController.cs index 6f3495e..2c5e5cd 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, }); }