Erster Commit des bestehenden monolithischen WinForms-Copytraders, inklusive der Alt-Backups (*.bak), damit diese dauerhaft in der Historie rekonstruierbar bleiben. Threema-Lib unter libs/ wurde vendored (nested .git entfernt). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
94 lines
3.3 KiB
C#
94 lines
3.3 KiB
C#
using System;
|
|
using MongoDB.Driver;
|
|
using PolyTraderSharp.Extensions;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using PolyTraderSharp.Models;
|
|
|
|
namespace PolyTraderSharp.Extensions
|
|
{
|
|
public static class MongoDbLiteDBShim
|
|
{
|
|
// 1. LiteDB: FindOne(predicate) -> MongoDB: Find(predicate).FirstOrDefault()
|
|
public static T LiteFindOne<T>(this IMongoCollection<T> col, Expression<Func<T, bool>> predicate)
|
|
{
|
|
return col.Find(predicate).FirstOrDefault();
|
|
}
|
|
|
|
// 2. LiteDB: Find(predicate) -> MongoDB: Find(predicate).ToList()
|
|
// Note: LiteDB returns IEnumerable<T>. ToList() is perfectly fine for iteration.
|
|
public static List<T> LiteFind<T>(this IMongoCollection<T> col, Expression<Func<T, bool>> predicate)
|
|
{
|
|
return col.Find(predicate).ToList();
|
|
}
|
|
|
|
// 3. LiteDB: FindAll() -> MongoDB: Find(_ => true).ToList()
|
|
public static List<T> LiteFindAll<T>(this IMongoCollection<T> col)
|
|
{
|
|
return col.Find(_ => true).ToList();
|
|
}
|
|
|
|
// 4. Upsert extensions mapped to Primary Keys
|
|
public static void Upsert(this IMongoCollection<AccountState> col, AccountState doc)
|
|
{
|
|
col.ReplaceOne(x => x.AccountId == doc.AccountId, doc, new ReplaceOptions { IsUpsert = true });
|
|
}
|
|
|
|
public static void Upsert(this IMongoCollection<TrackedTrader> col, TrackedTrader doc)
|
|
{
|
|
col.ReplaceOne(x => x.Id == doc.Id, doc, new ReplaceOptions { IsUpsert = true });
|
|
}
|
|
|
|
public static void Upsert(this IMongoCollection<Position> col, Position doc)
|
|
{
|
|
col.ReplaceOne(x => x.TokenId == doc.TokenId, doc, new ReplaceOptions { IsUpsert = true });
|
|
}
|
|
|
|
public static void Upsert(this IMongoCollection<MarketData> col, MarketData doc)
|
|
{
|
|
col.ReplaceOne(x => x.Id == doc.Id, doc, new ReplaceOptions { IsUpsert = true });
|
|
}
|
|
|
|
// 5. Update (Updates without upserting if it doesn't exist)
|
|
public static void Update(this IMongoCollection<TrackedTrader> col, TrackedTrader doc)
|
|
{
|
|
col.ReplaceOne(x => x.Id == doc.Id, doc);
|
|
}
|
|
|
|
public static void Update(this IMongoCollection<AccountState> col, AccountState doc)
|
|
{
|
|
col.ReplaceOne(x => x.AccountId == doc.AccountId, doc);
|
|
}
|
|
|
|
public static void Update(this IMongoCollection<MarketData> col, MarketData doc)
|
|
{
|
|
col.ReplaceOne(x => x.Id == doc.Id, doc);
|
|
}
|
|
|
|
// 6. Insert maps perfectly to InsertOne
|
|
public static void Insert<T>(this IMongoCollection<T> col, T doc)
|
|
{
|
|
col.InsertOne(doc);
|
|
}
|
|
|
|
// 7. Delete (by TokenId / String ID)
|
|
public static void Delete(this IMongoCollection<Position> col, string tokenId)
|
|
{
|
|
col.DeleteOne(x => x.TokenId == tokenId);
|
|
}
|
|
|
|
// 8. EnsureIndex shim (MongoDB Index Creation)
|
|
public static void EnsureIndex<T>(this IMongoCollection<T> col, Expression<Func<T, object>> property)
|
|
{
|
|
try
|
|
{
|
|
var indexKeys = Builders<T>.IndexKeys.Ascending(property);
|
|
var indexModel = new CreateIndexModel<T>(indexKeys);
|
|
col.Indexes.CreateOne(indexModel);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|