Skip to content

Commit

Permalink
Add initial app
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 13, 2024
1 parent e550089 commit c16610c
Show file tree
Hide file tree
Showing 99 changed files with 11,014 additions and 7,502 deletions.
22 changes: 22 additions & 0 deletions MyApp.ServiceInterface/BlazorRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

namespace MyApp.ServiceInterface;

public class BlazorRenderer(HtmlRenderer htmlRenderer)
{
public Task<string> RenderComponent<T>() where T : IComponent
=> RenderComponent<T>(ParameterView.Empty);

public Task<string> RenderComponent<T>(Dictionary<string, object?> dictionary) where T : IComponent
=> RenderComponent<T>(ParameterView.FromDictionary(dictionary));

private Task<string> RenderComponent<T>(ParameterView parameters) where T : IComponent
{
return htmlRenderer.Dispatcher.InvokeAsync(async () =>
{
var output = await htmlRenderer.RenderComponentAsync<T>(parameters);
return output.ToHtmlString();
});
}
}
23 changes: 23 additions & 0 deletions MyApp.ServiceInterface/Data/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace MyApp.Data;

public class AppConfig
{
public static AppConfig Instance { get; } = new();
public string LocalBaseUrl { get; set; }
public string PublicBaseUrl { get; set; }
public string CacheDir { get; set; }
public string? GitPagesBaseUrl { get; set; }
public List<ApplicationUser> ModelUsers { get; set; } = [];
public ApplicationUser DefaultUser { get; set; } = new()
{
Model = "unknown",
UserName = "unknown",
ProfileUrl = "/img/profiles/user2.svg",
};

public ApplicationUser GetApplicationUser(string model)
{
var user = ModelUsers.FirstOrDefault(x => x.Model == model);
return user ?? DefaultUser;
}
}
4 changes: 3 additions & 1 deletion MyApp.ServiceInterface/Data/ApplicationUser.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using Microsoft.AspNetCore.Identity;
using ServiceStack.DataAnnotations;

namespace MyApp.Data;

// Add profile data for application users by adding properties to the ApplicationUser class
[Alias("AspNetUsers")]
public class ApplicationUser : IdentityUser
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? DisplayName { get; set; }
public string? ProfileUrl { get; set; }
public string? Model { get; set; }
}

24 changes: 24 additions & 0 deletions MyApp.ServiceInterface/Data/IdFiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Concurrent;
using ServiceStack.IO;

namespace MyApp.Data;

public class IdFiles(int id, string dir1, string dir2, string fileId, List<IVirtualFile> files)
{
public int Id { get; init; } = id;
public string Dir1 { get; init; } = dir1;
public string Dir2 { get; init; } = dir2;
public string FileId { get; init; } = fileId;
public List<IVirtualFile> Files { get; init; } = files;
public ConcurrentDictionary<string, string> FileContents { get; } = [];

public async Task LoadContentsAsync()
{
if (FileContents.Count > 0) return;
var tasks = new List<Task>();
tasks.AddRange(Files.Select(async file => {
FileContents[file.Name] = await file.ReadAllTextAsync();
}));
await Task.WhenAll(tasks);
}
}
68 changes: 68 additions & 0 deletions MyApp.ServiceInterface/Data/R2Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using MyApp.ServiceModel;
using ServiceStack;
using ServiceStack.IO;

namespace MyApp.Data;

public static class R2Extensions
{
public static Dictionary<string,int> ModelScores = new()
{
["starcoder2:3b"] = 1, //3B
["phi"] = 2, //2.7B
["gemma:2b"] = 3,
["gemma"] = 4, //7B
["codellama"] = 5, //7B
["mistral"] = 6, //7B
["starcoder2:15b"] = 7, //15B
["mixtral"] = 8, //47B
};

public static (string dir1, string dir2, string fileId) ToFileParts(this int id)
{
var idStr = $"{id}".PadLeft(9, '0');
var dir1 = idStr[..3];
var dir2 = idStr.Substring(3, 3);
var fileId = idStr[6..];
return (dir1, dir2, fileId);
}

public static async Task<IdFiles> GetQuestionFilesAsync(this R2VirtualFiles r2, int id)
{
var (dir1, dir2, fileId) = ToFileParts(id);

var files = (await r2.EnumerateFilesAsync($"{dir1}/{dir2}").ToListAsync())
.Where(x => x.Name.Glob($"{fileId}.*"))
.OrderByDescending(x => x.LastModified)
.Cast<IVirtualFile>()
.ToList();

return new IdFiles(id: id, dir1: dir1, dir2: dir2, fileId: fileId, files: files);
}

public static async Task<QuestionAndAnswers?> ToQuestionAndAnswers(this IdFiles idFiles)
{
var fileName = idFiles.FileId + ".json";
await idFiles.LoadContentsAsync();

var to = new QuestionAndAnswers();
foreach (var entry in idFiles.FileContents)
{
if (entry.Key == fileName)
{
to.Post = entry.Value.FromJson<Post>();
}
else if (entry.Key.StartsWith(idFiles.FileId + ".a."))
{
to.Answers.Add(entry.Value.FromJson<Answer>());
}
}

if (to.Post == null)
return null;

to.Answers.Each(x => x.UpVotes = ModelScores.GetValueOrDefault(x.Model, 1));
to.Answers.Sort((a, b) => b.Votes - a.Votes);
return to;
}
}
2 changes: 1 addition & 1 deletion MyApp.ServiceInterface/EmailServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using ServiceStack;
using MyApp.ServiceModel;

namespace MyApp.ServiceInterface;
namespace MyApp.Data;

/// <summary>
/// Configuration for sending emails using SMTP servers in EmailServices
Expand Down
1 change: 1 addition & 0 deletions MyApp.ServiceInterface/MyApp.ServiceInterface.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.*" />
<PackageReference Include="ServiceStack" Version="8.*" />
<PackageReference Include="ServiceStack.Ormlite" Version="8.*" />
<PackageReference Include="ServiceStack.Aws" Version="8.*" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions MyApp.ServiceInterface/MyServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using MyApp.ServiceModel;
using ServiceStack.OrmLite;

namespace MyApp.ServiceInterface;
namespace MyApp.Data;

public class MyServices : Service
{
Expand All @@ -15,8 +15,7 @@ public async Task<object> Any(AdminData request)
{
var tables = new (string Label, Type Type)[]
{
("Bookings", typeof(Booking)),
("Coupons", typeof(Coupon)),
("Posts", typeof(Post)),
};
var dialect = Db.GetDialectProvider();
var totalSql = tables.Map(x => $"SELECT '{x.Label}', COUNT(*) FROM {dialect.GetQuotedTableName(x.Type.GetModelMetadata())}")
Expand Down
179 changes: 0 additions & 179 deletions MyApp.ServiceModel/Bookings.cs

This file was deleted.

Loading

0 comments on commit c16610c

Please sign in to comment.