This commit is contained in:
David Renner 2020-06-03 15:45:00 +02:00
commit 7314dc780d
17 changed files with 286 additions and 153 deletions

View file

@ -8,17 +8,17 @@ using Microsoft.Data.Sqlite;
namespace BuecherwurmAPI.Controllers namespace BuecherwurmAPI.Controllers
{ {
[Route("inventar")] [Route("api/inventar")]
[ApiController] [ApiController]
public class ItemController : ControllerBase public class ItemController : ControllerBase
{ {
private readonly IItemRepo _repository; private readonly ItemModel _repository;
public ItemController(IItemRepo repository) public ItemController(IItem repository)
{ {
_repository = repository; _repository = (ItemModel)repository;
} }
// GET Inventar // GET api/inventar
[HttpGet] [HttpGet]
public ActionResult<IEnumerable<Item>> GetAllItems() public ActionResult<IEnumerable<Item>> GetAllItems()
{ {
@ -30,42 +30,31 @@ namespace BuecherwurmAPI.Controllers
return NoContent(); return NoContent();
} }
// POST Inventar // POST api/inventar
[HttpPost] [HttpPost]
public ActionResult<IEnumerable<Item>> NewItem(ItemPost item) public ActionResult<IEnumerable<Item>> NewItem(ItemPost item)
{ {
try var newItem = _repository.NewItem(item);
{ return Ok(newItem);
var newItem = _repository.NewItem(item);
if (item != null)
{
return Ok(newItem);
}
return NotFound();
}
catch (System.Exception)
{
return StatusCode(304); //not Modified
}
} }
// GET api/nventar/{id}
// GET Inventar/{id}
[HttpGet("{itemId}")] [HttpGet("{itemId}")]
public ActionResult<IEnumerable<Item>> GetItemByID(long id) public ActionResult<IEnumerable<Item>> GetItemByID(long itemId)
{ {
var item = _repository.GetItemById(id); var item = _repository.GetItemById(itemId);
if (item != null) if (item != null)
{ {
return Ok(item); return Ok(item);
} }
return NoContent(); return NotFound();
} }
// DELETE inventory/{id} // DELETE api/inventar/{id}
[HttpDelete("itemId")] [HttpDelete("{itemId}")]
public ActionResult<IEnumerable<Item>> DeleteItem(long itemId) public ActionResult<IEnumerable<Item>> DeleteItem(long itemId)
{ {
var item = _repository.GetItemById(itemId); var item = _repository.GetItemById(itemId);

View file

@ -6,15 +6,15 @@ using System.Linq;
namespace BuecherwurmAPI.Controllers namespace BuecherwurmAPI.Controllers
{ {
[Route("katalog") ] [Route("api/katalog") ]
[ApiController] [ApiController]
public class KatalogController :ControllerBase 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 // GET Katalog
[HttpGet] [HttpGet]
@ -26,9 +26,9 @@ namespace BuecherwurmAPI.Controllers
// POST Katalog // POST Katalog
[HttpPost] [HttpPost]
public ActionResult<IEnumerable<Book>> NeuesBuch(Book book) public ActionResult<Book> AddBook(BookPost book)
{ {
var id = _repository.AddBook(book);
return Ok(new Book return Ok(new Book
{ {
Name = book.Name, Name = book.Name,
@ -38,7 +38,7 @@ namespace BuecherwurmAPI.Controllers
Language= book.Language, Language= book.Language,
Pages= book.Pages, Pages= book.Pages,
Year=book.Year, Year=book.Year,
ProductId =book.ProductId, ProductId = id,
Category= book.Category, Category= book.Category,
ImageLink =book.ImageLink, ImageLink =book.ImageLink,
LendTime =book.LendTime, LendTime =book.LendTime,
@ -48,8 +48,8 @@ namespace BuecherwurmAPI.Controllers
// GET katalog/{id} // GET katalog/{id}
[HttpGet("{id}", Name ="GetBookByID")] [HttpGet("{id}")]
public ActionResult <IEnumerable<Book>> GetBookByID(int id) public ActionResult <Book> GetBookByID(long id)
{ {
var book = _repository.GetBookById(id); var book = _repository.GetBookById(id);
if (book != null) if (book != null)
@ -61,36 +61,23 @@ namespace BuecherwurmAPI.Controllers
} }
// PUT Katalog/{id} // PUT Katalog/{id}
[HttpPut("id")] [HttpPut("{id}")]
public ActionResult<IEnumerable<Book>> BuchBearbeiten(Book book) public ActionResult<Book> EditBook(long id, Book book)
{ {
return Ok(new Book _repository.EditBook(id, book);
{ return Ok(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
});
} }
// DELETE katalog/{id} // DELETE katalog/{id}
[HttpDelete("id")] [HttpDelete("{id}")]
public ActionResult<IEnumerable<Book>> BuchEntfernen (int id) public ActionResult<Book> DeleteBook (long id)
{ {
var book = _repository.GetBookById(id); var book = _repository.GetBookById(id);
if(book == null) if(book == null)
{ {
return NotFound(); return NotFound();
} }
_repository.BuchEntfernen(book); _repository.DeleteBook(id);
return NoContent(); return NoContent();
} }

View file

@ -4,11 +4,10 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AutoMapper; using AutoMapper;
using BuecherwurmAPI.Models; using BuecherwurmAPI.Models;
using BuecherwurmAPI.DTOs;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.JsonPatch; using Microsoft.AspNetCore.JsonPatch;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Data.Sqlite;
namespace BuecherwurmAPI.Controllers namespace BuecherwurmAPI.Controllers
{ {
@ -16,13 +15,13 @@ namespace BuecherwurmAPI.Controllers
[ApiController] [ApiController]
public class LendController : ControllerBase public class LendController : ControllerBase
{ {
private readonly IRepository _repository; private readonly LendModel _repository;
private readonly IMapper _mapper; //private readonly IMapper _mapper;
public LendController(IRepository repository, IMapper mapper) public LendController(ILend repo)
{ {
_repository = repository; _repository = (LendModel)repo;
_mapper = mapper; //_mapper = mapper;
} }
//GET api/leihvorgang //GET api/leihvorgang
@ -34,30 +33,25 @@ namespace BuecherwurmAPI.Controllers
//POST api/leihvorgang //POST api/leihvorgang
[HttpPost] [HttpPost]
public ActionResult<LendReadDTO> LendsPost(Lend lend) public ActionResult<LendPost> LendsPost(LendPost lend)
{ {
/* var newId = _repository.insertLendReturningId(lend);
Internally a lend is stored with an id if (newId > 0)
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 item = new Lend
{ {
Id = 256, var item = new Lend
Customer = lend.Customer, {
Returned = lend.Returned, Id = newId,
ItemId = lend.ItemId, Customer = lend.Customer,
ReturnDate = lend.ReturnDate Returned = false,
}; ItemId = lend.ItemId,
return Ok(item); ReturnDate = lend.ReturnDate
};
return Ok(item);
}
else
{
return BadRequest();
}
//return Ok(_mapper.Map<LendReadDTO>(item)); //return Ok(_mapper.Map<LendReadDTO>(item));
} }

View file

@ -1,9 +0,0 @@
using System;
namespace BuecherwurmAPI.DTOs
{
public class ItemReadDTO
{
public int BookId { get; set;}
}
}

View file

@ -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; }
}
}

Binary file not shown.

View file

@ -9,14 +9,28 @@ namespace BuecherwurmAPI.Models
public string Country {get; set;} public string Country {get; set;}
public string Link {get; set;} public string Link {get; set;}
public string Language {get; set;} public string Language {get; set;}
public int Pages {get; set;} public long Pages {get; set;}
public int Year {get; set;} public long Year {get; set;}
[Key] [Key, Required]
[Required] public long ProductId { get; set; }
public int ProductId { get; set; }
public CategoryEnum Category { get; set; } public CategoryEnum Category { get; set; }
public string ImageLink { get; set; } public string ImageLink { get; set; }
public int LendTime {get; set;} public long LendTime {get; set;}
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;} public LendTypeEnum LendType {get; set;}
} }

View file

@ -6,7 +6,9 @@ namespace BuecherwurmAPI.Models
public interface IBookRepo public interface IBookRepo
{ {
IEnumerable<Book> GetAllBooks(); IEnumerable<Book> GetAllBooks();
Book GetBookById(int id); Book GetBookById(long id);
void BuchEntfernen(Book book); void DeleteBook(long id);
long AddBook (Book book);
long EditBook (long id, Book book);
} }
} }

View file

@ -1,11 +0,0 @@
using System.Collections.Generic;
using BuecherwurmAPI.Models;
namespace BuecherwurmAPI.Models
{
public interface ILendRepo
{
IEnumerable<Lend> GetAllLends();
Lend GetLendById(int id);
}
}

View file

@ -1,13 +0,0 @@
using System.Collections.Generic;
using BuecherwurmAPI.Models;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
namespace BuecherwurmAPI.Models
{
public interface IRepository
{
IEnumerable<Lend> GetAllLends();
Lend GetLendById(long id);
bool IdExits(string table, long id);
}
}

View file

@ -13,5 +13,4 @@ namespace BuecherwurmAPI.Models
{ {
public int BookId { get; set; } public int BookId { get; set; }
} }
} }

View file

@ -6,7 +6,12 @@ using Microsoft.VisualBasic.CompilerServices;
namespace BuecherwurmAPI.Models namespace BuecherwurmAPI.Models
{ {
internal class ItemModel : IItemRepo public interface IItem
{
}
internal class ItemModel : IItem
{ {
private SqliteConnection _dbConnection; private SqliteConnection _dbConnection;
@ -49,13 +54,11 @@ namespace BuecherwurmAPI.Models
// using automatically disposes the command after completion // using automatically disposes the command after completion
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
command.CommandText = "SELECT FROM Items"; command.CommandText = "SELECT * FROM Items";
var dataReader = command.ExecuteReader(); var dataReader = command.ExecuteReader();
while (dataReader.Read()) while (dataReader.Read())
{ {
var returned = (long) dataReader["Returned"] == 1;
items.Add(new Item items.Add(new Item
{ {
ItemId = (long) dataReader["ItemId"], ItemId = (long) dataReader["ItemId"],
@ -71,13 +74,11 @@ namespace BuecherwurmAPI.Models
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
command.Parameters.Add(new SqliteParameter("@itemId", itemId)); command.Parameters.Add(new SqliteParameter("@itemId", itemId));
command.CommandText = "SELECT FROM Items WHERE Id = @itemId"; command.CommandText = "SELECT * FROM Items WHERE ItemId = @itemId";
var dataReader = command.ExecuteReader(); var dataReader = command.ExecuteReader();
while (dataReader.Read()) while (dataReader.Read())
{ {
var returned = (long) dataReader["Returned"] == 1;
var item = new Item var item = new Item
{ {
ItemId = (long) dataReader["ItemId"], ItemId = (long) dataReader["ItemId"],
@ -111,7 +112,6 @@ namespace BuecherwurmAPI.Models
{ {
using (var command = _dbConnection.CreateCommand()) using (var command = _dbConnection.CreateCommand())
{ {
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = itemId;
command.CommandText = "DELETE FROM Items WHERE ItemId = @itemId"; command.CommandText = "DELETE FROM Items WHERE ItemId = @itemId";
command.Parameters.Add(new SqliteParameter("@itemId", itemId)); command.Parameters.Add(new SqliteParameter("@itemId", itemId));
command.ExecuteNonQuery(); command.ExecuteNonQuery();

150
Models/KatalogModel.cs Normal file
View file

@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using BuecherwurmAPI.Models;
using Microsoft.Data.Sqlite;
using Microsoft.VisualBasic.CompilerServices;
namespace BuecherwurmAPI.Models
{
public interface ICatalogue
{
}
internal class KatalogModel : ICatalogue
{
private SqliteConnection _dbConnection;
public KatalogModel()
{
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
_dbConnection = new SqliteConnection(connectionBuilder.ConnectionString);
_dbConnection.Open();
}
public IEnumerable<Book> GetAllBooks()
{
var books= new List<Book>();
using (var command = _dbConnection.CreateCommand())
{
command.CommandText = @"SELECT * FROM Books";
var dataReader = command.ExecuteReader();
while (dataReader.Read())
{
books.Add(new Book
{
Name = (string) dataReader["Books"],
Author = (string) dataReader["Author"],
Country = (string) dataReader["Country"],
Link = (string) dataReader["Link"],
Language = (string) dataReader["Language"],
Pages = (long) dataReader["Pages"],
Year = (long) dataReader["Year"],
ProductId = (long) dataReader["ProductId"],
Category = (CategoryEnum) dataReader["Category"],
ImageLink = (string) dataReader["ImageLink"],
LendTime = (long) dataReader["LendTime"],
LendType = (LendTypeEnum) dataReader["LendType"]
});
}
}
return books;
}
public Book GetBookById(long id)
{
using (var command = _dbConnection.CreateCommand())
{
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id;
command.CommandText = @"SELECT * FROM Books WHERE ProductId = @id";
var dataReader = command.ExecuteReader();
var book = new Book
{
Name = (string) dataReader["Books"],
Author = (string) dataReader["Author"],
Country = (string) dataReader["Country"],
Link = (string) dataReader["Link"],
Language = (string) dataReader["Language"],
Pages = (long) dataReader["Pages"],
Year = (long) dataReader["Year"],
ProductId = (long) dataReader["ProductId"],
Category = (CategoryEnum) dataReader["Category"],
ImageLink = (string) dataReader["ImageLink"],
LendTime = (long) dataReader["LendTime"],
LendType = (LendTypeEnum) dataReader["LendType"]
};
return book;
}
}
public void DeleteBook(long id)
{
using (var command = _dbConnection.CreateCommand())
{
command.Parameters.Add(new SqliteParameter("@id", SqliteType.Integer)).Value = id;
command.CommandText = @"DELETE * FROM Books WHERE ProductId= @id";
var dataReader = command.ExecuteReader();
}
}
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, 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("@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("@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;
}
}
public long EditBook (long id, Book book)
{
using (var command = _dbConnection.CreateCommand())
{
// funktioniert das so?
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("@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("@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();
return id;
}
}
}
}

View file

@ -1,13 +1,28 @@
using System; using System;
using System.ComponentModel.DataAnnotations;
namespace BuecherwurmAPI.Models namespace BuecherwurmAPI.Models
{ {
public class Lend public class Lend
{ {
[Key, Required]
public long Id { get; set; } public long Id { get; set; }
[Required]
public long ItemId { get; set;} public long ItemId { get; set;}
[Required]
public DateTime ReturnDate { get; set; } public DateTime ReturnDate { get; set; }
[Required]
public string Customer { get; set; } public string Customer { get; set; }
[Required]
public bool Returned { get; set; } public bool Returned { get; set; }
} }
public class LendPost
{
public long ItemId {get; set;}
public DateTime ReturnDate {get; set;}
public string Customer {get; set;}
}
} }

View file

@ -6,11 +6,17 @@ using Microsoft.VisualBasic.CompilerServices;
namespace BuecherwurmAPI.Models namespace BuecherwurmAPI.Models
{ {
internal class Repository : IRepository
public interface ILend
{
}
internal class LendModel : ILend
{ {
private SqliteConnection _dbConnection; private SqliteConnection _dbConnection;
public Repository() public LendModel()
{ {
var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"}; var connectionBuilder = new SqliteConnectionStringBuilder {DataSource = "LongWormMemory.db"};
_dbConnection = new SqliteConnection(connectionBuilder.ConnectionString); _dbConnection = new SqliteConnection(connectionBuilder.ConnectionString);
@ -94,5 +100,26 @@ namespace BuecherwurmAPI.Models
} }
return null; return null;
} }
public long insertLendReturningId(LendPost 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;
}
}
} }
} }

View file

@ -1,5 +1,4 @@
using AutoMapper; using AutoMapper;
using BuecherwurmAPI.DTOs;
using BuecherwurmAPI.Models; using BuecherwurmAPI.Models;
namespace BuecherwurmAPI.Profiles namespace BuecherwurmAPI.Profiles
@ -8,7 +7,7 @@ namespace BuecherwurmAPI.Profiles
{ {
public LendProfile() public LendProfile()
{ {
CreateMap<Lend, LendReadDTO>(); CreateMap<Lend, LendPost>();
} }
} }
} }

View file

@ -32,7 +32,9 @@ namespace BuecherwurmAPI
// Adds a service that is created once per connection. // Adds a service that is created once per connection.
// It takes an interface and a specific implementation. // It takes an interface and a specific implementation.
// That allows to swap the implementation easily. // That allows to swap the implementation easily.
services.AddScoped<IRepository, Repository>(); services.AddScoped<ILend, LendModel>();
services.AddScoped<IItem, ItemModel>();
services.AddScoped<ICatalogue, KatalogModel>();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
} }