diff --git a/Fluzm/Fluzm.csproj b/Fluzm/Fluzm.csproj index 4cf2573..36b05cc 100644 --- a/Fluzm/Fluzm.csproj +++ b/Fluzm/Fluzm.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 disable enable @@ -11,6 +11,7 @@ + diff --git a/Fluzm/public/scripts/models/PostData.ts b/Fluzm/public/scripts/models/PostData.ts index da2ceef..6244102 100644 --- a/Fluzm/public/scripts/models/PostData.ts +++ b/Fluzm/public/scripts/models/PostData.ts @@ -5,7 +5,7 @@ export interface PostData { textFragment: string rating: number imageUrl: string - commentIds: string[] + commentIds: number[] } export default PostData diff --git a/Fluzm/src/DTO/ChannelData.cs b/Fluzm/src/DTO/ChannelData.cs new file mode 100644 index 0000000..113a4e3 --- /dev/null +++ b/Fluzm/src/DTO/ChannelData.cs @@ -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; } +} \ No newline at end of file diff --git a/Fluzm/src/DTO/PostData.cs b/Fluzm/src/DTO/PostData.cs new file mode 100644 index 0000000..7baf6dd --- /dev/null +++ b/Fluzm/src/DTO/PostData.cs @@ -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(); +} \ No newline at end of file diff --git a/Fluzm/src/DTO/UserData.cs b/Fluzm/src/DTO/UserData.cs new file mode 100644 index 0000000..956dcbc --- /dev/null +++ b/Fluzm/src/DTO/UserData.cs @@ -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; } +} \ No newline at end of file diff --git a/Fluzm/src/Database.cs b/Fluzm/src/Database.cs new file mode 100644 index 0000000..51ceccc --- /dev/null +++ b/Fluzm/src/Database.cs @@ -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(); + _db.CreateTable(); + _db.CreateTable(); + + _idGenerator = new IdGenerator(new Dictionary + { + { typeof(UserData), _db.Table().Count() }, + { typeof(ChannelData), _db.Table().Count() }, + { typeof(PostData), _db.Table().Count() }, + }); + } + + public UserData GetUserData(int id) => _db.Table().First(u => u.id == id); + + public ChannelData GetChannel(int id) => _db.Table().First(c => c.channelId == id); + + public PostData GetPostData(int id) => _db.Table().First(p => p.postId == id); + +} \ No newline at end of file diff --git a/Fluzm/src/IdGenerator.cs b/Fluzm/src/IdGenerator.cs new file mode 100644 index 0000000..aece0f2 --- /dev/null +++ b/Fluzm/src/IdGenerator.cs @@ -0,0 +1,22 @@ +namespace Fluzm; + +public class IdGenerator +{ + private Dictionary _last_ids; + + public IdGenerator(IDictionary initial_values) + { + _last_ids = new Dictionary(initial_values); + } + + // returns id unique for type + public int GenerateId() + { + lock (_last_ids) + { + int new_id = _last_ids[typeof(T)]+1; + _last_ids[typeof(T)] = new_id; + return new_id; + } + } +} \ No newline at end of file diff --git a/Fluzm/src/Program.cs b/Fluzm/src/Program.cs index 2528751..46ab273 100644 --- a/Fluzm/src/Program.cs +++ b/Fluzm/src/Program.cs @@ -16,12 +16,13 @@ internal static class Program private static readonly IOPath configPath = "./config.dtsod"; private static CancellationTokenSource mainCancel = new(); private static ILogger logger = new ConsoleLogger(); - private static Router router = new Router(); + private static Router router = new Router(); public static async Task Main(string[] args) { 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(); @@ -56,7 +63,8 @@ internal static class Program }, mainCancel.Token); requestId++; } - + + // stop server.Stop(); logger.LogInfo("Main", "server stopped"); }