Merge remote-tracking branch 'origin/Felix'

This commit is contained in:
Naumann 2020-06-04 14:06:54 +02:00
commit 4a6bb9313b
5 changed files with 57 additions and 15 deletions

View file

@ -70,6 +70,7 @@ namespace BuecherwurmAPI.Controllers
// DELETE katalog/{id} // DELETE katalog/{id}
[HttpDelete("{id}")] [HttpDelete("{id}")]
public ActionResult<Book> DeleteBook (long id) public ActionResult<Book> DeleteBook (long id)
{ {
var book = _repository.GetBookById(id); var book = _repository.GetBookById(id);

View file

@ -35,7 +35,7 @@ namespace BuecherwurmAPI.Controllers
[HttpPost] [HttpPost]
public ActionResult<LendPost> LendsPost(LendPost lend) public ActionResult<LendPost> LendsPost(LendPost lend)
{ {
var newId = _repository.insertLendReturningId(lend); var newId = _repository.AddLend(lend);
if (newId > 0) if (newId > 0)
{ {
var item = new Lend var item = new Lend
@ -68,11 +68,12 @@ namespace BuecherwurmAPI.Controllers
return Ok(lend); return Ok(lend);
} }
//PATCH api/leihvorgang/{id} //PUT api/leihvorgang/{id}
[HttpPatch("{id}")] [HttpPut("{id}")]
public ActionResult LendPatchById(int id, JsonPatchDocument<Lend> patchDocument) public ActionResult LendPatchById(long id, LendPost lend)
{ {
return Ok(); var lendPatch = _repository.insertLendReturningId(id, lend);
return Ok(lendPatch);
} }
} }
} }

View file

@ -52,17 +52,22 @@ namespace BuecherwurmAPI.Models
return books; return books;
} }
public Book GetBookById(long id) public Book GetBookById(long id)
{ {
Book book = new Book{};
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; command.Parameters.Add(new SqliteParameter("@id", id));
command.CommandText = @"SELECT * FROM Books WHERE ProductId = @id"; command.CommandText = @"SELECT * FROM Books WHERE ProductId = @id";
var dataReader = command.ExecuteReader(); var dataReader = command.ExecuteReader();
var book = new Book while (dataReader.Read())
{ {
Name = (string) dataReader["Books"], book =new Book
{
Name = (string) dataReader["Name"],
Author = (string) dataReader["Author"], Author = (string) dataReader["Author"],
Country = (string) dataReader["Country"], Country = (string) dataReader["Country"],
Link = (string) dataReader["Link"], Link = (string) dataReader["Link"],
@ -77,13 +82,17 @@ namespace BuecherwurmAPI.Models
}; };
return book; return book;
} }
return null;
}
} }
public void DeleteBook(long id) public void DeleteBook(long id)
{ {
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id; command.Parameters.Add(new SqliteParameter("@id", id));
command.CommandText = @"DELETE * FROM Books WHERE ProductId= @id"; command.CommandText = @"DELETE FROM Books WHERE ProductId= @id";
var dataReader = command.ExecuteReader(); var dataReader = command.ExecuteReader();
} }
} }
@ -140,7 +149,7 @@ namespace BuecherwurmAPI.Models
command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime)); command.Parameters.Add(new SqliteParameter("@LendTime", book.LendTime));
command.Parameters.Add(new SqliteParameter("@LendType", book.LendType)); command.Parameters.Add(new SqliteParameter("@LendType", book.LendType));
var success = command.ExecuteNonQuery(); command.ExecuteNonQuery();
return id; return id;
} }

View file

@ -24,5 +24,7 @@ namespace BuecherwurmAPI.Models
public DateTime ReturnDate {get; set;} public DateTime ReturnDate {get; set;}
public string Customer {get; set;} public string Customer {get; set;}
public bool Returned { get; set; }
} }
} }

View file

@ -101,14 +101,43 @@ namespace BuecherwurmAPI.Models
return null; return null;
} }
public long insertLendReturningId(LendPost lend) public long insertLendReturningId(long id, LendPost lend)
{ {
using (var command = _dbConnection.CreateCommand()) 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("@itemId", lend.ItemId));
command.Parameters.Add(new SqliteParameter("@returnDate", lend.ReturnDate)); command.Parameters.Add(new SqliteParameter("@returnDate", lend.ReturnDate));
command.Parameters.Add(new SqliteParameter("@customer", lend.Customer)); 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(); var success = command.ExecuteNonQuery();