La in dapper, och routing. fixade upp lite exempelsidor
This commit is contained in:
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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user