sqlite database

This commit is contained in:
Timerix22 2023-12-18 14:26:04 +06:00
parent 9cd059bad2
commit 0bca7c330d
8 changed files with 117 additions and 6 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@ -11,6 +11,7 @@
<PackageReference Include="DTLib.Ben.Demystifier" Version="1.0.4" />
<PackageReference Include="DTLib.Dtsod" Version="1.3.0" />
<PackageReference Include="DTLib.Logging" Version="1.3.0" />
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
</ItemGroup>
<Target Name="PreBuild" AfterTargets="PreBuildEvent">

View File

@ -5,7 +5,7 @@ export interface PostData {
textFragment: string
rating: number
imageUrl: string
commentIds: string[]
commentIds: number[]
}
export default PostData

View File

@ -0,0 +1,14 @@
using SQLite;
namespace Fluzm.DTO;
#nullable disable
public class ChannelData
{
[PrimaryKey, AutoIncrement]
public int channelId { get; set; }
public string channelName { get; set; }
public string avatarUrl { get; set; }
public int followersCount { get; set; }
public float rating { get; set; }
}

21
Fluzm/src/DTO/PostData.cs Normal file
View File

@ -0,0 +1,21 @@
using System.Linq;
using SQLite;
namespace Fluzm.DTO;
#nullable disable
public class PostData
{
[PrimaryKey, AutoIncrement]
public int postId { get; set; }
public int channelId { get; set; }
public string title { get; set; }
public string textFragment { get; set; }
public float rating { get; set; }
public string imageUrl { get; set; }
[Column("commentIds")]
public string _commentIdsRaw { get; set; }
[Ignore]
public int[] commentIds => _commentIdsRaw.Split(',').Select(s => Convert.ToInt32(s)).ToArray();
}

11
Fluzm/src/DTO/UserData.cs Normal file
View File

@ -0,0 +1,11 @@
using SQLite;
namespace Fluzm.DTO;
#nullable disable
public class UserData
{
[PrimaryKey, AutoIncrement]
public int id { get; set; }
public string name { get; set; }
}

34
Fluzm/src/Database.cs Normal file
View File

@ -0,0 +1,34 @@
using System.Collections.Concurrent;
using Fluzm.DTO;
using SQLite;
namespace Fluzm;
public class DatabaseConnection
{
private static readonly IOPath db_path = "database.sqlite3";
private SQLiteConnection _db;
private IdGenerator _idGenerator;
public DatabaseConnection()
{
_db = new SQLiteConnection(db_path.Str);
_db.CreateTable<UserData>();
_db.CreateTable<ChannelData>();
_db.CreateTable<PostData>();
_idGenerator = new IdGenerator(new Dictionary<Type, int>
{
{ typeof(UserData), _db.Table<UserData>().Count() },
{ typeof(ChannelData), _db.Table<ChannelData>().Count() },
{ typeof(PostData), _db.Table<PostData>().Count() },
});
}
public UserData GetUserData(int id) => _db.Table<UserData>().First(u => u.id == id);
public ChannelData GetChannel(int id) => _db.Table<ChannelData>().First(c => c.channelId == id);
public PostData GetPostData(int id) => _db.Table<PostData>().First(p => p.postId == id);
}

22
Fluzm/src/IdGenerator.cs Normal file
View File

@ -0,0 +1,22 @@
namespace Fluzm;
public class IdGenerator
{
private Dictionary<Type, int> _last_ids;
public IdGenerator(IDictionary<Type, int> initial_values)
{
_last_ids = new Dictionary<Type, int>(initial_values);
}
// returns id unique for type
public int GenerateId<T>()
{
lock (_last_ids)
{
int new_id = _last_ids[typeof(T)]+1;
_last_ids[typeof(T)] = new_id;
return new_id;
}
}
}

View File

@ -22,6 +22,7 @@ internal static class Program
{
try
{
// CLI
Console.InputEncoding = Encoding.UTF8;
Console.OutputEncoding = Encoding.UTF8;
Console.CancelKeyPress += (_, _) =>
@ -30,16 +31,22 @@ internal static class Program
mainCancel.Cancel();
};
// config
Config config;
if (!File.Exists(configPath))
{
logger.LogWarn("Main", "Config file not found.");
logger.LogWarn("Main", "config file not found.");
config = new Config();
File.WriteAllText(configPath, config.ToString());
logger.LogWarn("Main", $"Created default at {configPath}.");
logger.LogWarn("Main", $"created default at {configPath}.");
}
else config = Config.FromDtsod(new DtsodV23(File.ReadAllText(configPath)));
// database
var db = new DatabaseConnection();
// http server
string baseUrl = $"http://{config.Address}:{config.Port}/";
logger.LogInfo("Main", $"starting webserver at {baseUrl} ...");
var server = new HttpListener();
@ -57,6 +64,7 @@ internal static class Program
requestId++;
}
// stop
server.Stop();
logger.LogInfo("Main", "server stopped");
}