diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs index f599103..6f1d84d 100644 --- a/Controllers/LendController.cs +++ b/Controllers/LendController.cs @@ -35,7 +35,7 @@ namespace BuecherwurmAPI.Controllers [HttpPost] public ActionResult LendsPost(LendPost lend) { - var newId = _repository.insertLendReturningId(lend); + var newId = _repository.AddLend(lend); if (newId > 0) { var item = new Lend @@ -68,11 +68,12 @@ namespace BuecherwurmAPI.Controllers return Ok(lend); } - //PATCH api/leihvorgang/{id} - [HttpPatch("{id}")] - public ActionResult LendPatchById(int id, JsonPatchDocument patchDocument) + //PUT api/leihvorgang/{id} + [HttpPut("{id}")] + public ActionResult LendPatchById(long id, LendPost lend) { - return Ok(); + var lendPatch = _repository.insertLendReturningId(id, lend); + return Ok(lendPatch); } } } \ No newline at end of file diff --git a/Models/KatalogModel.cs b/Models/KatalogModel.cs index c218974..62c9691 100644 --- a/Models/KatalogModel.cs +++ b/Models/KatalogModel.cs @@ -113,13 +113,15 @@ namespace BuecherwurmAPI.Models } return book; } + + } public void DeleteProduct(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"; + command.Parameters.Add(new SqliteParameter("@id", id)); + command.CommandText = @"DELETE FROM Books WHERE ProductId= @id"; var dataReader = command.ExecuteReader(); } } @@ -214,7 +216,7 @@ namespace BuecherwurmAPI.Models command.Parameters.Add(new SqliteParameter("@Topic", ((MagazinPost)book).Topic)); } - var success = command.ExecuteNonQuery(); + command.ExecuteNonQuery(); return id; } diff --git a/Models/Lend.cs b/Models/Lend.cs index 50ba663..debbdf3 100644 --- a/Models/Lend.cs +++ b/Models/Lend.cs @@ -24,5 +24,7 @@ namespace BuecherwurmAPI.Models public DateTime ReturnDate {get; set;} public string Customer {get; set;} + + public bool Returned { get; set; } } } diff --git a/Models/LendModel.cs b/Models/LendModel.cs index 3c2bba7..f4407b9 100644 --- a/Models/LendModel.cs +++ b/Models/LendModel.cs @@ -101,14 +101,43 @@ namespace BuecherwurmAPI.Models return null; } - public long insertLendReturningId(LendPost lend) + public long insertLendReturningId(long id, LendPost lend) { using (var command = _dbConnection.CreateCommand()) { - command.CommandText = "INSERT INTO Lends VALUES (NULL, @itemId, @returnDate, @customer, false)"; + command.CommandText = "UPDATE Lends SET (ItemId, ReturnDate, Customer, Returned) = (@itemId, @returnDate, @customer, @returned) WHERE Id = @id"; + + command.Parameters.Add(new SqliteParameter("@id", id)); 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("@returned", lend.Returned)); + + + var success = command.ExecuteNonQuery(); + + if (success == 1) + { + command.CommandText = @"SELECT last_insert_rowid()"; + return (long)command.ExecuteScalar(); + } + + return -1; + + } + } + + public long AddLend (LendPost lend) + { + using (var command = _dbConnection.CreateCommand()) + { + // funktioniert das so? + //muss ProduktId übergeben werden? + command.CommandText = @"INSERT INTO Lends (itemid, ReturnDate, Customer, Returned) VALUES (@ItemId, @ReturnDate, @Customer, false)"; + + 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();