From a707f23dc244a65d0f5cc68f3d1304b6c6ac2ab8 Mon Sep 17 00:00:00 2001 From: nek0 Date: Wed, 3 Jun 2020 09:26:07 +0200 Subject: [PATCH] db connection meowdy meow --- Controllers/InventarController.cs | 10 ++++----- Controllers/LendController.cs | 34 +++++++++++++++++++------------ Models/ILendRepo.cs | 11 ---------- Models/IRepository.cs | 13 ------------ Models/Interfaces.cs | 9 ++++++++ Models/Item.cs | 3 +-- Models/Lend.cs | 15 ++++++++++++++ Models/LendModel.cs | 31 ++++++++++++++++++++++------ Startup.cs | 2 +- 9 files changed, 77 insertions(+), 51 deletions(-) delete mode 100644 Models/ILendRepo.cs delete mode 100644 Models/IRepository.cs create mode 100644 Models/Interfaces.cs diff --git a/Controllers/InventarController.cs b/Controllers/InventarController.cs index 5e8870d..2c5e5cd 100644 --- a/Controllers/InventarController.cs +++ b/Controllers/InventarController.cs @@ -27,16 +27,16 @@ namespace BuecherwurmAPI.Controllers // POST Inventar [HttpPost] - public ActionResult> NewItem(Item item) + public ActionResult> NewItem(int bookId) { + var itemid = insertItemReturningId(bookId); return Ok(new Item { - Id = item.Id, - BookId = item.BookId, + Id = itemId, + BookId = bookId, }); } - - + // GET Inventar/{id} [HttpGet("{id}", Name = "GetItemByID")] public ActionResult> GetItemByID(int id) diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs index 813d893..b18ff9b 100644 --- a/Controllers/LendController.cs +++ b/Controllers/LendController.cs @@ -8,7 +8,7 @@ using BuecherwurmAPI.DTOs; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.JsonPatch; using Microsoft.Extensions.Logging; - +using Microsoft.Data.Sqlite; namespace BuecherwurmAPI.Controllers { @@ -16,12 +16,12 @@ namespace BuecherwurmAPI.Controllers [ApiController] public class LendController : ControllerBase { - private readonly IRepository _repository; + private readonly LendModel _repository; private readonly IMapper _mapper; - public LendController(IRepository repository, IMapper mapper) + public LendController(SqliteConnection connection, IMapper mapper) { - _repository = repository; + _repository = new LendModel(connection); _mapper = mapper; } @@ -34,7 +34,7 @@ namespace BuecherwurmAPI.Controllers //POST api/leihvorgang [HttpPost] - public ActionResult LendsPost(Lend lend) + public ActionResult LendsPost(LendSeed lend) { /* Internally a lend is stored with an id @@ -49,15 +49,23 @@ namespace BuecherwurmAPI.Controllers DTO stands for Data Transfer Object */ - var item = new Lend + var newId = _repository.insertLendReturningId(lend); + if (newId > 0) { - Id = 256, - Customer = lend.Customer, - Returned = lend.Returned, - ItemId = lend.ItemId, - ReturnDate = lend.ReturnDate - }; - return Ok(item); + var item = new Lend + { + Id = newId, + Customer = lend.Customer, + Returned = false, + ItemId = lend.ItemId, + ReturnDate = lend.ReturnDate + }; + return Ok(item); + } + else + { + return BadRequest(); + } //return Ok(_mapper.Map(item)); } diff --git a/Models/ILendRepo.cs b/Models/ILendRepo.cs deleted file mode 100644 index 9e12f99..0000000 --- a/Models/ILendRepo.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Collections.Generic; -using BuecherwurmAPI.Models; - -namespace BuecherwurmAPI.Models -{ - public interface ILendRepo - { - IEnumerable GetAllLends(); - Lend GetLendById(int id); - } -} \ No newline at end of file diff --git a/Models/IRepository.cs b/Models/IRepository.cs deleted file mode 100644 index 6e79a2a..0000000 --- a/Models/IRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Collections.Generic; -using BuecherwurmAPI.Models; -using Microsoft.EntityFrameworkCore.Metadata.Conventions; - -namespace BuecherwurmAPI.Models -{ - public interface IRepository - { - IEnumerable GetAllLends(); - Lend GetLendById(long id); - bool IdExits(string table, long id); - } -} \ No newline at end of file diff --git a/Models/Interfaces.cs b/Models/Interfaces.cs new file mode 100644 index 0000000..64a6026 --- /dev/null +++ b/Models/Interfaces.cs @@ -0,0 +1,9 @@ +using Microsoft.Data.Sqlite; + +namespace BuecherwurmAPI.Models +{ + public interface IModel + { + + } +} \ No newline at end of file diff --git a/Models/Item.cs b/Models/Item.cs index c6e6012..17e6017 100644 --- a/Models/Item.cs +++ b/Models/Item.cs @@ -4,8 +4,7 @@ namespace BuecherwurmAPI.Models { public class Item { - [Key] - [Required] + [Key, Required] public int Id { get; set; } [Required] public int BookId { get; set; } diff --git a/Models/Lend.cs b/Models/Lend.cs index d2e7954..6c258f2 100644 --- a/Models/Lend.cs +++ b/Models/Lend.cs @@ -1,13 +1,28 @@ using System; +using System.ComponentModel.DataAnnotations; namespace BuecherwurmAPI.Models { public class Lend { + [Key, Required] public long Id { get; set; } + [Required] public long ItemId { get; set;} + [Required] public DateTime ReturnDate { get; set; } + [Required] public string Customer { get; set; } + [Required] public bool Returned { get; set; } } + + public class LendSeed + { + public long ItemId {get; set;} + + public DateTime ReturnDate {get; set;} + + public string Customer {get; set;} + } } diff --git a/Models/LendModel.cs b/Models/LendModel.cs index c69942a..014ac36 100644 --- a/Models/LendModel.cs +++ b/Models/LendModel.cs @@ -6,15 +6,13 @@ using Microsoft.VisualBasic.CompilerServices; namespace BuecherwurmAPI.Models { - internal class Repository : IRepository + internal class LendModel : IModel { - private SqliteConnection _dbConnection; + public SqliteConnection _dbConnection; - public Repository() + public LendModel(SqliteConnection connection) { - var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; - _dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); - _dbConnection.Open(); + _dbConnection = connection; } public bool IdExits(string table, long id) @@ -94,5 +92,26 @@ namespace BuecherwurmAPI.Models } return null; } + + public long insertLendReturningId(LendSeed lend) + { + using (var command = _dbConnection.CreateCommand()) + { + command.CommandText = "INSERT INTO Lends VALUES (NULL, @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(); + + if (success == 1) + { + command.CommandText = @"SELECT last_insert_rowid()"; + return (long)command.ExecuteScalar(); + } + + return -1; + } + } } } \ No newline at end of file diff --git a/Startup.cs b/Startup.cs index b6cc4f5..c17f544 100644 --- a/Startup.cs +++ b/Startup.cs @@ -32,7 +32,7 @@ namespace BuecherwurmAPI // Adds a service that is created once per connection. // It takes an interface and a specific implementation. // That allows to swap the implementation easily. - services.AddScoped(); + services.AddScoped(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); }