db connection meowdy meow
This commit is contained in:
parent
1637f45d0e
commit
a707f23dc2
9 changed files with 77 additions and 51 deletions
|
@ -27,16 +27,16 @@ namespace BuecherwurmAPI.Controllers
|
||||||
|
|
||||||
// POST Inventar
|
// POST Inventar
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult<IEnumerable<Item>> NewItem(Item item)
|
public ActionResult<IEnumerable<Item>> NewItem(int bookId)
|
||||||
{
|
{
|
||||||
|
var itemid = insertItemReturningId(bookId);
|
||||||
return Ok(new Item
|
return Ok(new Item
|
||||||
{
|
{
|
||||||
Id = item.Id,
|
Id = itemId,
|
||||||
BookId = item.BookId,
|
BookId = bookId,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// GET Inventar/{id}
|
// GET Inventar/{id}
|
||||||
[HttpGet("{id}", Name = "GetItemByID")]
|
[HttpGet("{id}", Name = "GetItemByID")]
|
||||||
public ActionResult<IEnumerable<Item>> GetItemByID(int id)
|
public ActionResult<IEnumerable<Item>> GetItemByID(int id)
|
||||||
|
|
|
@ -8,7 +8,7 @@ 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,12 +16,12 @@ 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(SqliteConnection connection, IMapper mapper)
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = new LendModel(connection);
|
||||||
_mapper = mapper;
|
_mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ namespace BuecherwurmAPI.Controllers
|
||||||
|
|
||||||
//POST api/leihvorgang
|
//POST api/leihvorgang
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult<LendReadDTO> LendsPost(Lend lend)
|
public ActionResult<LendReadDTO> LendsPost(LendSeed lend)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
Internally a lend is stored with an id
|
Internally a lend is stored with an id
|
||||||
|
@ -49,15 +49,23 @@ namespace BuecherwurmAPI.Controllers
|
||||||
|
|
||||||
DTO stands for Data Transfer Object
|
DTO stands for Data Transfer Object
|
||||||
*/
|
*/
|
||||||
|
var newId = _repository.insertLendReturningId(lend);
|
||||||
|
if (newId > 0)
|
||||||
|
{
|
||||||
var item = new Lend
|
var item = new Lend
|
||||||
{
|
{
|
||||||
Id = 256,
|
Id = newId,
|
||||||
Customer = lend.Customer,
|
Customer = lend.Customer,
|
||||||
Returned = lend.Returned,
|
Returned = false,
|
||||||
ItemId = lend.ItemId,
|
ItemId = lend.ItemId,
|
||||||
ReturnDate = lend.ReturnDate
|
ReturnDate = lend.ReturnDate
|
||||||
};
|
};
|
||||||
return Ok(item);
|
return Ok(item);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
//return Ok(_mapper.Map<LendReadDTO>(item));
|
//return Ok(_mapper.Map<LendReadDTO>(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
using System.Collections.Generic;
|
|
||||||
using BuecherwurmAPI.Models;
|
|
||||||
|
|
||||||
namespace BuecherwurmAPI.Models
|
|
||||||
{
|
|
||||||
public interface ILendRepo
|
|
||||||
{
|
|
||||||
IEnumerable<Lend> GetAllLends();
|
|
||||||
Lend GetLendById(int id);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
9
Models/Interfaces.cs
Normal file
9
Models/Interfaces.cs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
|
namespace BuecherwurmAPI.Models
|
||||||
|
{
|
||||||
|
public interface IModel
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,8 +4,7 @@ namespace BuecherwurmAPI.Models
|
||||||
{
|
{
|
||||||
public class Item
|
public class Item
|
||||||
{
|
{
|
||||||
[Key]
|
[Key, Required]
|
||||||
[Required]
|
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
[Required]
|
[Required]
|
||||||
public int BookId { get; set; }
|
public int BookId { get; set; }
|
||||||
|
|
|
@ -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 LendSeed
|
||||||
|
{
|
||||||
|
public long ItemId {get; set;}
|
||||||
|
|
||||||
|
public DateTime ReturnDate {get; set;}
|
||||||
|
|
||||||
|
public string Customer {get; set;}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,15 +6,13 @@ using Microsoft.VisualBasic.CompilerServices;
|
||||||
|
|
||||||
namespace BuecherwurmAPI.Models
|
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 = connection;
|
||||||
_dbConnection = new SqliteConnection(connectionBuilder.ConnectionString);
|
|
||||||
_dbConnection.Open();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IdExits(string table, long id)
|
public bool IdExits(string table, long id)
|
||||||
|
@ -94,5 +92,26 @@ namespace BuecherwurmAPI.Models
|
||||||
}
|
}
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -32,7 +32,7 @@ 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<IModel, LendModel>();
|
||||||
|
|
||||||
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue