diff --git a/Controllers/InventarController.cs b/Controllers/InventarController.cs deleted file mode 100644 index 2c5e5cd..0000000 --- a/Controllers/InventarController.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System.Collections.Generic; -using BuecherwurmAPI.Models; -using Microsoft.AspNetCore.Mvc; -using System.Linq; -//using Microsoft.EntityFrameworkCore; - - -namespace BuecherwurmAPI.Controllers -{ - [Route("inventar")] - [ApiController] - public class InventarController : ControllerBase - { - private readonly IItemRepo _repository; - - public InventarController(IItemRepo repository) - { - _repository = repository; - } - // GET Inventar - [HttpGet] - public ActionResult> GetAllItems() - { - var items = _repository.GetAllItems(); - return Ok(items); - } - - // POST Inventar - [HttpPost] - public ActionResult> NewItem(int bookId) - { - var itemid = insertItemReturningId(bookId); - return Ok(new Item - { - Id = itemId, - BookId = bookId, - }); - } - - // GET Inventar/{id} - [HttpGet("{id}", Name = "GetItemByID")] - public ActionResult> GetItemByID(int id) - { - var item = _repository.GetItemById(id); - if (item != null) - { - return Ok(item); - } - return NoContent(); - - } - - - // DELETE inventory/{id} - [HttpDelete("id")] - public ActionResult> DeleteItem(int id) - { - var item = _repository.GetItemById(id); - if (item == null) - { - return NotFound(); - } - _repository.DeleteItem(item); - return NoContent(); - } - - } - -} diff --git a/Controllers/ItemController.cs b/Controllers/ItemController.cs new file mode 100644 index 0000000..d089fb1 --- /dev/null +++ b/Controllers/ItemController.cs @@ -0,0 +1,67 @@ +using System.Collections.Generic; +using BuecherwurmAPI.Models; +using Microsoft.AspNetCore.Mvc; +using System.Linq; +using Microsoft.Data.Sqlite; +//using Microsoft.EntityFrameworkCore; + + +namespace BuecherwurmAPI.Controllers +{ + [Route("api/inventar")] + [ApiController] + public class ItemController : ControllerBase + { + private readonly ItemModel _repository; + + public ItemController(IItem repository) + { + _repository = (ItemModel)repository; + } + // GET api/inventar + [HttpGet] + public ActionResult> GetAllItems() + { + var items = _repository.GetAllItems(); + return Ok(items); + } + + // POST api/inventar + [HttpPost] + + public ActionResult> NewItem(ItemPost item) + { + var newItem = _repository.NewItem(item); + return Ok(newItem); + + } + + // GET api/nventar/{id} + [HttpGet("{itemId}")] + public ActionResult> GetItemByID(long itemId) + { + var item = _repository.GetItemById(itemId); + if (item != null) + { + return Ok(item); + } + return NotFound(); + } + + // DELETE api/inventar/{id} + + [HttpDelete("{itemId}")] + public ActionResult> DeleteItem(long itemId) + { + var item = _repository.GetItemById(itemId); + if(item == null) + { + return NotFound(); + } + _repository.DeleteItem(itemId); + return NoContent(); + } + + } + +} diff --git a/Controllers/KatalogController.cs b/Controllers/KatalogController.cs index 63fb7be..0e4a68d 100644 --- a/Controllers/KatalogController.cs +++ b/Controllers/KatalogController.cs @@ -6,15 +6,15 @@ using System.Linq; namespace BuecherwurmAPI.Controllers { - [Route("katalog") ] + [Route("api/katalog") ] [ApiController] public class KatalogController :ControllerBase { - private readonly IBookRepo _repository; + private readonly KatalogModel _repository; - public KatalogController (IBookRepo repository) + public KatalogController (ICatalogue repository) { - _repository=repository; + _repository= (KatalogModel)repository; } // GET Katalog [HttpGet] @@ -26,9 +26,9 @@ namespace BuecherwurmAPI.Controllers // POST Katalog [HttpPost] - public ActionResult> AddBook(Book book) + public ActionResult AddBook(BookPost book) { - + var id = _repository.AddBook(book); return Ok(new Book { Name = book.Name, @@ -38,7 +38,7 @@ namespace BuecherwurmAPI.Controllers Language= book.Language, Pages= book.Pages, Year=book.Year, - ProductId =book.ProductId, + ProductId = id, Category= book.Category, ImageLink =book.ImageLink, LendTime =book.LendTime, @@ -48,8 +48,8 @@ namespace BuecherwurmAPI.Controllers // GET katalog/{id} - [HttpGet("{id}", Name ="GetBookByID")] - public ActionResult > GetBookByID(long id) + [HttpGet("{id}")] + public ActionResult GetBookByID(long id) { var book = _repository.GetBookById(id); if (book != null) @@ -61,29 +61,16 @@ namespace BuecherwurmAPI.Controllers } // PUT Katalog/{id} - [HttpPut("id")] - public ActionResult> EditBook(Book book) + [HttpPut("{id}")] + public ActionResult EditBook(long id, Book book) { - return Ok(new Book - { - Name = book.Name, - Author= book.Author, - Country= book.Country, - Link= book.Link, - Language= book.Language, - Pages= book.Pages, - Year=book.Year, - ProductId =book.ProductId, - Category= book.Category, - ImageLink =book.ImageLink, - LendTime =book.LendTime, - LendType = book.LendType - }); + _repository.EditBook(id, book); + return Ok(book); } // DELETE katalog/{id} - [HttpDelete("id")] - public ActionResult> DeleteBook (long id) + [HttpDelete("{id}")] + public ActionResult DeleteBook (long id) { var book = _repository.GetBookById(id); if(book == null) diff --git a/Controllers/LendController.cs b/Controllers/LendController.cs index b18ff9b..f599103 100644 --- a/Controllers/LendController.cs +++ b/Controllers/LendController.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Threading.Tasks; using AutoMapper; using BuecherwurmAPI.Models; -using BuecherwurmAPI.DTOs; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.JsonPatch; using Microsoft.Extensions.Logging; @@ -17,12 +16,12 @@ namespace BuecherwurmAPI.Controllers public class LendController : ControllerBase { private readonly LendModel _repository; - private readonly IMapper _mapper; + //private readonly IMapper _mapper; - public LendController(SqliteConnection connection, IMapper mapper) + public LendController(ILend repo) { - _repository = new LendModel(connection); - _mapper = mapper; + _repository = (LendModel)repo; + //_mapper = mapper; } //GET api/leihvorgang @@ -34,21 +33,8 @@ namespace BuecherwurmAPI.Controllers //POST api/leihvorgang [HttpPost] - public ActionResult LendsPost(LendSeed lend) + public ActionResult LendsPost(LendPost lend) { - /* - Internally a lend is stored with an id - but the client shouldn't be allowed to set or change it - therefore the package 'AutoMapper' is used to prevent errors - that could happen when doing this task manually. - It takes the information from the client and maps it to the - corresponding internal object which then will be returned. - Furthermore it could be used to keep some attributes secret. - Another nice effect of this is that the implementation could be changed - while the interface could be retained by some minor changes in the code. - - DTO stands for Data Transfer Object - */ var newId = _repository.insertLendReturningId(lend); if (newId > 0) { diff --git a/DTOs/LendRead.cs b/DTOs/LendRead.cs deleted file mode 100644 index 5813213..0000000 --- a/DTOs/LendRead.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace BuecherwurmAPI.DTOs -{ - public class LendReadDTO - { - public int ItemId { get; set;} - public DateTime ReturnDate { get; set; } - public string Customer { get; set; } - public bool Returned { get; set; } - } -} \ No newline at end of file diff --git a/LongWormMemory.db b/LongWormMemory.db index 4b1deb5..2af348a 100644 Binary files a/LongWormMemory.db and b/LongWormMemory.db differ diff --git a/Models/Book.cs b/Models/Book.cs index bc90fd5..3e605de 100644 --- a/Models/Book.cs +++ b/Models/Book.cs @@ -11,8 +11,7 @@ namespace BuecherwurmAPI.Models public string Language {get; set;} public long Pages {get; set;} public long Year {get; set;} - [Key] - [Required] + [Key, Required] public long ProductId { get; set; } public CategoryEnum Category { get; set; } public string ImageLink { get; set; } @@ -20,4 +19,19 @@ namespace BuecherwurmAPI.Models public LendTypeEnum LendType {get; set;} } + public class BookPost + { + public string Name {get; set;} + public string Author {get; set;} + public string Country {get; set;} + public string Link {get; set;} + public string Language {get; set;} + public long Pages {get; set;} + public long Year {get; set;} + public CategoryEnum Category { get; set; } + public string ImageLink { get; set; } + public long LendTime {get; set;} + public LendTypeEnum LendType {get; set;} + } + } \ No newline at end of file diff --git a/Models/IItemRepo.cs b/Models/IItemRepo.cs index 46d7f62..7646320 100644 --- a/Models/IItemRepo.cs +++ b/Models/IItemRepo.cs @@ -6,8 +6,8 @@ namespace BuecherwurmAPI.Models public interface IItemRepo { IEnumerable GetAllItems(); - Item GetItemById(int id); - void NewItem(Item item); - void DeleteItem(Item item); + Item GetItemById(long itemId); + Item NewItem(ItemPost item); + void DeleteItem(long itemId); } } \ No newline at end of file diff --git a/Models/Interfaces.cs b/Models/Interfaces.cs deleted file mode 100644 index 64a6026..0000000 --- a/Models/Interfaces.cs +++ /dev/null @@ -1,9 +0,0 @@ -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 17e6017..dbff417 100644 --- a/Models/Item.cs +++ b/Models/Item.cs @@ -5,9 +5,12 @@ namespace BuecherwurmAPI.Models public class Item { [Key, Required] - public int Id { get; set; } + public long ItemId { get; set; } [Required] + public long BookId { get; set; } + } + public class ItemPost + { public int BookId { get; set; } } - } \ No newline at end of file diff --git a/Models/ItemModel.cs b/Models/ItemModel.cs new file mode 100644 index 0000000..b4ef5ed --- /dev/null +++ b/Models/ItemModel.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using BuecherwurmAPI.Models; +using Microsoft.Data.Sqlite; +using Microsoft.VisualBasic.CompilerServices; + +namespace BuecherwurmAPI.Models +{ + public interface IItem + { + + } + + internal class ItemModel : IItem + { + private SqliteConnection _dbConnection; + + public ItemModel() + { + var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; + _dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); + _dbConnection.Open(); + } + + public bool IdExits(string table, long id) + { + using (var command = _dbConnection.CreateCommand()) + { + command.Parameters.Add(new SqliteParameter("@Id", SqliteType.Integer)).Value = id; + + // Certain parts of the query can't be filled with variables for security reasons. + switch (table) + { + case Tables.Table.Items: + command.CommandText = @"SELECT EXISTS(SELECT 0 FROM Items WHERE Id = @Id)"; + break; + } + + var dr = command.ExecuteReader(); + + long result = 0; + while (dr.Read()) + { + result = (long) dr[0]; + } + return result != 0; + } + } + + public IEnumerable GetAllItems() + { + var items = new List(); + + // using automatically disposes the command after completion + using (var command = _dbConnection.CreateCommand()) + { + command.CommandText = "SELECT * FROM Items"; + var dataReader = command.ExecuteReader(); + + while (dataReader.Read()) + { + items.Add(new Item + { + ItemId = (long) dataReader["ItemId"], + BookId = (long) dataReader["BookId"] + }); + } + } + return items; + } + + public Item GetItemById(long itemId) + { + using (var command = _dbConnection.CreateCommand()) + { + command.Parameters.Add(new SqliteParameter("@itemId", itemId)); + command.CommandText = "SELECT * FROM Items WHERE ItemId = @itemId"; + var dataReader = command.ExecuteReader(); + + while (dataReader.Read()) + { + var item = new Item + { + ItemId = (long) dataReader["ItemId"], + BookId = (long) dataReader["BookId"] + }; + return item; + } + } + return null; + } + public Item NewItem(ItemPost itemPost) + { + using (var command = _dbConnection.CreateCommand()) + { + //hier soll jetz der NewItem Command folgen. ein POST mit EingabeInfo {id} = "BookId" + command.CommandText = "INSERT INTO Items (BookId) VALUES (@bookId)"; + command.CommandType = System.Data.CommandType.Text; + command.Parameters.Add(new SqliteParameter("@bookId", itemPost.BookId)); + command.ExecuteNonQuery(); + command.CommandText = "select last_insert_rowid()"; + var LastRowId64 = (long)command.ExecuteScalar(); + var item = new Item{ + ItemId = LastRowId64, + BookId = itemPost.BookId + }; + return item; + } + } + + public void DeleteItem(long itemId) + { + using (var command = _dbConnection.CreateCommand()) + { + command.CommandText = "DELETE FROM Items WHERE ItemId = @itemId"; + command.Parameters.Add(new SqliteParameter("@itemId", itemId)); + command.ExecuteNonQuery(); + } + } + } +} \ No newline at end of file diff --git a/Models/KatalogModel.cs b/Models/KatalogModel.cs index e1dd357..64f2347 100644 --- a/Models/KatalogModel.cs +++ b/Models/KatalogModel.cs @@ -6,11 +6,15 @@ using Microsoft.VisualBasic.CompilerServices; namespace BuecherwurmAPI.Models { - internal class Katalog : IBookRepo + public interface ICatalogue + { + + } + internal class KatalogModel : ICatalogue { private SqliteConnection _dbConnection; - public Katalog() + public KatalogModel() { var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; _dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); @@ -84,22 +88,21 @@ namespace BuecherwurmAPI.Models } } - public long AddBook (Book book) + public long AddBook (BookPost book) { using (var command = _dbConnection.CreateCommand()) { // funktioniert das so? //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.CommandText = @"INSERT INTO Books (Name, Author, Country, Link, Language, Pages, Year, ProductId, Category, ImageLink, LendTime, LendType) VALUES (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, NULL, @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("@Country", 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)); @@ -122,17 +125,16 @@ namespace BuecherwurmAPI.Models 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 (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @ProductId, @Category, @ImageLink, @LendTime, @LendType )"; + command.Parameters.Add(new SqliteParameter("@id", id)); + command.CommandText = @"UPDATE Books SET (Name, Author, Country, Link, Language, Pages, Year, Category, ImageLink, LendTime, LendType) = (@Name, @Author, @Country, @Link, @Language, @Pages, @Year, @Category, @ImageLink, @LendTime, @LendType ) WHERE ProductId = @id"; 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("@Country", 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)); @@ -140,13 +142,7 @@ namespace BuecherwurmAPI.Models var success = command.ExecuteNonQuery(); - if (success == 1) - { - command.CommandText = @"SELECT last_insert_rowid()"; - return (long)command.ExecuteScalar(); - } - - return -1; + return id; } } diff --git a/Models/Lend.cs b/Models/Lend.cs index 6c258f2..50ba663 100644 --- a/Models/Lend.cs +++ b/Models/Lend.cs @@ -17,7 +17,7 @@ namespace BuecherwurmAPI.Models public bool Returned { get; set; } } - public class LendSeed + public class LendPost { public long ItemId {get; set;} diff --git a/Models/LendModel.cs b/Models/LendModel.cs index 014ac36..3c2bba7 100644 --- a/Models/LendModel.cs +++ b/Models/LendModel.cs @@ -6,13 +6,21 @@ using Microsoft.VisualBasic.CompilerServices; namespace BuecherwurmAPI.Models { - internal class LendModel : IModel + + public interface ILend { - public SqliteConnection _dbConnection; - public LendModel(SqliteConnection connection) + } + + internal class LendModel : ILend + { + private SqliteConnection _dbConnection; + + public LendModel() { - _dbConnection = connection; + var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; + _dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); + _dbConnection.Open(); } public bool IdExits(string table, long id) @@ -93,7 +101,7 @@ namespace BuecherwurmAPI.Models return null; } - public long insertLendReturningId(LendSeed lend) + public long insertLendReturningId(LendPost lend) { using (var command = _dbConnection.CreateCommand()) { diff --git a/Models/Tables.cs b/Models/Tables.cs index f3143cb..2bc61c3 100644 --- a/Models/Tables.cs +++ b/Models/Tables.cs @@ -6,6 +6,7 @@ namespace BuecherwurmAPI.Models { public const string Lends = "Lends"; public const string Katalog = "Katalog"; + public const string Items = "Items"; } } } \ No newline at end of file diff --git a/Profiles/LendProfile.cs b/Profiles/LendProfile.cs index 074931c..2d48c44 100644 --- a/Profiles/LendProfile.cs +++ b/Profiles/LendProfile.cs @@ -1,5 +1,4 @@ using AutoMapper; -using BuecherwurmAPI.DTOs; using BuecherwurmAPI.Models; namespace BuecherwurmAPI.Profiles @@ -8,7 +7,7 @@ namespace BuecherwurmAPI.Profiles { public LendProfile() { - CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/Startup.cs b/Startup.cs index c17f544..e8d9d83 100644 --- a/Startup.cs +++ b/Startup.cs @@ -32,7 +32,9 @@ 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.AddScoped(); + services.AddScoped(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); }