Working on api backend

This commit is contained in:
Max 2023-07-16 20:10:02 +02:00
parent ebb605477c
commit 8e6f96c9df
14 changed files with 227 additions and 18 deletions

View File

@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpRss", "SharpRss\SharpR
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolQit", "ToolQit\ToolQit\ToolQit.csproj", "{BD905344-9DBF-4986-B853-E70B22848876}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToolQit", "ToolQit\ToolQit\ToolQit.csproj", "{BD905344-9DBF-4986-B853-E70B22848876}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpSyndicationApi", "SharpSyndicationApi\SharpSyndicationApi.csproj", "{349033BA-F5C3-4CDB-AFEA-9E85711FFBD5}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -27,6 +29,10 @@ Global
{BD905344-9DBF-4986-B853-E70B22848876}.Debug|Any CPU.Build.0 = Debug|Any CPU {BD905344-9DBF-4986-B853-E70B22848876}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD905344-9DBF-4986-B853-E70B22848876}.Release|Any CPU.ActiveCfg = Release|Any CPU {BD905344-9DBF-4986-B853-E70B22848876}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD905344-9DBF-4986-B853-E70B22848876}.Release|Any CPU.Build.0 = Release|Any CPU {BD905344-9DBF-4986-B853-E70B22848876}.Release|Any CPU.Build.0 = Release|Any CPU
{349033BA-F5C3-4CDB-AFEA-9E85711FFBD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{349033BA-F5C3-4CDB-AFEA-9E85711FFBD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{349033BA-F5C3-4CDB-AFEA-9E85711FFBD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{349033BA-F5C3-4CDB-AFEA-9E85711FFBD5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -1,6 +1,11 @@
namespace SharpRss.Core namespace SharpRss.Core
{ {
public struct DbState
{
}
// Interface for implementing the database endpoints. // Interface for implementing the database endpoints.
public interface IDbAccess public interface IDbAccess
{ {

View File

@ -12,6 +12,13 @@ using SharpRss.Models;
namespace SharpRss namespace SharpRss
{ {
public class ItemFetchState
{
public string[]? SyndicationUrls { get; internal set; }
public int TakeAmount { get; internal set; } = 20;
public int TakenCount { get; internal set; }
public HashSet<SyndicationItemModel> Items { get; internal set; } = new HashSet<SyndicationItemModel>();
}
internal static class DbAccess internal static class DbAccess
{ {
private static readonly string ConnectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};"; private static readonly string ConnectionString = $"Data Source={Path.Combine(Environment.CurrentDirectory, "sharp_rss.sqlite")};";
@ -198,14 +205,16 @@ namespace SharpRss
} }
dbTransaction.Commit(); dbTransaction.Commit();
} }
public static async Task<HashSet<SyndicationItemModel>> GetSyndicationItemsAsync(string[]? encodedSyndicationUrls)
public static async Task<bool> GetSyndicationItemsAsync(ItemFetchState state)
{ {
await using SqliteConnection dbc = new SqliteConnection(ConnectionString); await using SqliteConnection dbc = new SqliteConnection(ConnectionString);
dbc.Open(); dbc.Open();
HashSet<SyndicationItemModel> items = new HashSet<SyndicationItemModel>(); HashSet<SyndicationItemModel> items = new HashSet<SyndicationItemModel>();
await TempCache.UpdateCache(CacheFetch.Syndication); await TempCache.UpdateCache(CacheFetch.Syndication);
await using DbDataReader reader = await dbc.ExecuteReaderAsync(encodedSyndicationUrls == null ? "SELECT * FROM syndication_item" : $"SELECT * FROM syndication_item WHERE encoded_syndication_url IN({FormatParametersFromArray(encodedSyndicationUrls)})"); await using DbDataReader reader = await dbc.ExecuteReaderAsync(state.SyndicationUrls == null ? "SELECT * FROM syndication_item" : $"SELECT * FROM syndication_item WHERE encoded_syndication_url IN({FormatParametersFromArray(state.SyndicationUrls)})");
//NOTE(dbg): While debugging this part of the function the code below (Reader.ReadAsync) will return 0, because the debugger already reads the items from the reader. //NOTE(dbg): While debugging this part of the function the code below (Reader.ReadAsync) will return 0, because the debugger already reads the items from the reader.
while (await reader.ReadAsync()) while (await reader.ReadAsync())
{ {
@ -229,7 +238,8 @@ namespace SharpRss
items.Add(syndicationItemModel); items.Add(syndicationItemModel);
} }
Log.Information("Fetching feed items resulted: {ItemCount} item(s)", items.Count); Log.Information("Fetching feed items resulted: {ItemCount} item(s)", items.Count);
return items; state.Items = items;
return true;
} }
public static async void Initialize() public static async void Initialize()
{ {

View File

@ -1,13 +0,0 @@
using System;
namespace SharpRss
{
// To keep a state at fetching item from the database.
public class FetchState
{
public int TakeAmount { get; set; } = 20;
public int TotalFetched { get; internal set; }
public string DbTableName { get; internal set; } = string.Empty;
public DateTime LastFetchTime { get; internal set; }
}
}

View File

@ -72,8 +72,9 @@ namespace SharpRss.Services
public async Task<HashSet<SyndicationItemModel>> GetSyndicationItemsAsync(string feedId, string? groupId = null) => await GetSyndicationItemsFromSyndicationsAsync(new[] { feedId }, groupId); public async Task<HashSet<SyndicationItemModel>> GetSyndicationItemsAsync(string feedId, string? groupId = null) => await GetSyndicationItemsFromSyndicationsAsync(new[] { feedId }, groupId);
public async Task<HashSet<SyndicationItemModel>> GetSyndicationItemsFromSyndicationsAsync(string[] feedIds, string? categoryId = null) public async Task<HashSet<SyndicationItemModel>> GetSyndicationItemsFromSyndicationsAsync(string[] feedIds, string? categoryId = null)
{ {
var items = await DbAccess.GetSyndicationItemsAsync(feedIds); /*var items = await DbAccess.GetSyndicationItemsAsync(feedIds);
return items; return items;*/
return new HashSet<SyndicationItemModel>();
} }

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SharpSyndicationApi.Models;
namespace SharpSyndicationApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CategoryController : ControllerBase
{
private readonly DataContext _context;
public CategoryController(DataContext context)
{
_context = context;
}
[HttpPost]
public async Task PostCategory(Category category)
{
_context.Categories.Add(category);
await _context.SaveChangesAsync();
}
[HttpGet]
[ProducesResponseType(200, Type = typeof(IEnumerable<Category>))]
public ActionResult<IEnumerable<Category>> GetCategories()
{
IEnumerable<Category> categories = _context.Categories.ToList();
if (categories == null || !categories.Any())
return NotFound();
return Ok(categories);
}
}
}

View File

@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
using SharpSyndicationApi.Models;
namespace SharpSyndicationApi
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
public DbSet<Category> Categories { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System;
namespace SharpSyndicationApi.Models
{
public class Category
{
public string Name { get; set; }
public string Id { get; set; }
public string Icon { get; set; }
public string HexColor { get; set; }
public DateTime CreationDate { get; set; }
}
}

View File

@ -0,0 +1,16 @@
namespace SharpSyndicationApi.Models
{
public class ContinuationToken
{
public int TakeAmount { get; set; } = 30;
public int TotalTaken { get; set; }
public ApiCallType ApiCall { get; set; }
}
public enum ApiCallType
{
Category,
Syndication,
SyndicationItem
}
}

View File

@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SharpSyndicationApi;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddDbContext<DataContext>(opt => opt.UseInMemoryDatabase("sharprss"));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,41 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:42659",
"sslPort": 44369
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5277",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7119;http://localhost:5277",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="7.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ToolQit\ToolQit\ToolQit.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}