La in dapper, och routing. fixade upp lite exempelsidor
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.66" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.6" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SpaProxy">
|
||||
<Version>9.*-*</Version>
|
||||
|
45
Server/Business/Services/DatabaseService.cs
Normal file
45
Server/Business/Services/DatabaseService.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Bibblan.Models;
|
||||
using Bibblan.ViewModels;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Npgsql;
|
||||
|
||||
namespace Bibblan.Business.Services
|
||||
{
|
||||
|
||||
public class BookFilter
|
||||
{
|
||||
public int? Author;
|
||||
}
|
||||
|
||||
public class DatabaseService
|
||||
{
|
||||
BibblanOptions settings;
|
||||
public DatabaseService(IOptions<BibblanOptions> options) {
|
||||
settings = options.Value;
|
||||
}
|
||||
|
||||
public IEnumerable<Books> GetBooks(int count, BookFilter filter = null)
|
||||
{
|
||||
var conn = new NpgsqlConnection(settings.BibblanConnection);
|
||||
var query = "select * from books";
|
||||
if(filter != null)
|
||||
{
|
||||
query += " where ";
|
||||
if(filter.Author != null)
|
||||
{
|
||||
query += $"id in (select book from books_authors_link where author = {filter.Author})";
|
||||
}
|
||||
}
|
||||
return conn.Query<Books>(query).Take(count).ToList();
|
||||
}
|
||||
|
||||
public IEnumerable<AuthorVm> GetAuthors(int count)
|
||||
{
|
||||
var query = "select a.id, a.name, count(bal.*) as bookcount\r\nfrom authors a\r\nleft join books_authors_link bal on a.id = bal.author\r\ngroup by a.id , a.name ";
|
||||
var conn = new NpgsqlConnection(settings.BibblanConnection);
|
||||
return conn.Query<AuthorVm>(query).Take(count).ToList();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
53
Server/Controllers/BibblanController.cs
Normal file
53
Server/Controllers/BibblanController.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Bibblan.Business.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bibblan.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class BibblanController : ControllerBase
|
||||
{
|
||||
DatabaseService _db;
|
||||
CalibreService _calibre;
|
||||
|
||||
public BibblanController(DatabaseService databaseService, CalibreService calibre)
|
||||
{
|
||||
_db = databaseService;
|
||||
_calibre = calibre;
|
||||
}
|
||||
|
||||
[HttpGet("cover")]
|
||||
public IActionResult GetCover(string path)
|
||||
{
|
||||
//TODO: Bör kanske inte gå direkt mot calibres filer..
|
||||
var bytes = _calibre.Cover(path);
|
||||
return File(bytes, "image/jpeg", true);
|
||||
}
|
||||
|
||||
[HttpGet("books")]
|
||||
public IActionResult GetBooks()
|
||||
{
|
||||
var authors = _db.GetBooks(100).ToList();
|
||||
return Ok(authors);
|
||||
}
|
||||
|
||||
[HttpGet("authors")]
|
||||
public IActionResult GetAuthors()
|
||||
{
|
||||
var authors = _db.GetAuthors(100).ToList();
|
||||
return Ok(authors);
|
||||
}
|
||||
|
||||
[HttpGet("books/author/{authorid}")]
|
||||
public IActionResult GetBooksByAuthor(int authorid)
|
||||
{
|
||||
var authors = _db.GetBooks(100, new BookFilter
|
||||
{
|
||||
Author = authorid
|
||||
}).ToList();
|
||||
return Ok(authors);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@@ -13,6 +13,7 @@ configSection.Bind(config);
|
||||
builder.Services.Configure<BibblanOptions>(configSection);
|
||||
|
||||
builder.Services.AddScoped<CalibreService, CalibreService>();
|
||||
builder.Services.AddScoped<DatabaseService, DatabaseService>();
|
||||
|
||||
|
||||
builder.Services.AddControllers();
|
||||
@@ -26,6 +27,8 @@ builder.Services.AddDbContext<PostgresCalibreContext>(options => options.UseNpgs
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
|
||||
|
||||
app.UseDefaultFiles();
|
||||
app.MapStaticAssets();
|
||||
|
||||
|
Reference in New Issue
Block a user