💾 Step 4: Repository Service Pattern
Wrap MartRepoService CRUD operations with your app's models. Every qTeVerse app uses this exact pattern.
public class YourRepoService
{
private readonly MartRepoService _repo;
private readonly MartAuthService _auth;
public YourRepoService(MartRepoService repo, MartAuthService auth)
{
_repo = repo;
_auth = auth;
}
/// Create a new widget record in the user's AT Protocol repo
public async Task<MartResult<MartRecord>> CreateWidgetAsync(Widget widget)
{
widget.CreatedAt = DateTime.UtcNow.ToString("O");
return await _repo.CreateRecordAsync(YourLexicon.Widget, widget.ToRecord());
}
/// List all widgets with cursor-based pagination
public async Task<(List<Widget> Items, string? Cursor)> ListWidgetsAsync(string? cursor = null)
{
var result = await _repo.ListRecordsAsync(YourLexicon.Widget, 50, cursor);
if (!result.Success) return (new(), null);
var items = new List<Widget>();
string? nextCursor = null;
if (result.Value.TryGetProperty("records", out var records))
{
foreach (var rec in records.EnumerateArray())
{
if (rec.TryGetProperty("value", out var val))
{
var w = Widget.FromRecord(val);
if (rec.TryGetProperty("uri", out var u))
{
w.Uri = u.GetString() ?? "";
w.Rkey = w.Uri.Split('/').LastOrDefault() ?? "";
}
items.Add(w);
}
}
}
if (result.Value.TryGetProperty("cursor", out var c))
nextCursor = c.GetString();
return (items, nextCursor);
}
/// Delete a widget by its record key
public async Task<MartResult<bool>> DeleteWidgetAsync(string rkey)
=> await _repo.DeleteRecordAsync(YourLexicon.Widget, rkey);
}