Update the position count and initiator
This commit is contained in:
1431
src/Managing.Infrastructure.Database/Migrations/20250815011248_AddInitiatorIdentifierToPositions.Designer.cs
generated
Normal file
1431
src/Managing.Infrastructure.Database/Migrations/20250815011248_AddInitiatorIdentifierToPositions.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Managing.Infrastructure.Databases.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddInitiatorIdentifierToPositions : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_Positions_Date",
|
||||
table: "Positions");
|
||||
|
||||
migrationBuilder.AddColumn<Guid>(
|
||||
name: "InitiatorIdentifier",
|
||||
table: "Positions",
|
||||
type: "uuid",
|
||||
nullable: false,
|
||||
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Positions_InitiatorIdentifier",
|
||||
table: "Positions",
|
||||
column: "InitiatorIdentifier");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_Positions_InitiatorIdentifier",
|
||||
table: "Positions");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "InitiatorIdentifier",
|
||||
table: "Positions");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Positions_Date",
|
||||
table: "Positions",
|
||||
column: "Date");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -231,6 +231,9 @@ namespace Managing.Infrastructure.Databases.Migrations
|
||||
.HasPrecision(18, 8)
|
||||
.HasColumnType("numeric(18,8)");
|
||||
|
||||
b.Property<int>("LongPositionCount")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(255)
|
||||
@@ -244,6 +247,9 @@ namespace Managing.Infrastructure.Databases.Migrations
|
||||
.HasPrecision(18, 8)
|
||||
.HasColumnType("numeric(18,8)");
|
||||
|
||||
b.Property<int>("ShortPositionCount")
|
||||
.HasColumnType("integer");
|
||||
|
||||
b.Property<DateTime>("StartupTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
@@ -668,6 +674,9 @@ namespace Managing.Infrastructure.Databases.Migrations
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.Property<Guid>("InitiatorIdentifier")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("MoneyManagementJson")
|
||||
.HasColumnType("text");
|
||||
|
||||
@@ -711,11 +720,11 @@ namespace Managing.Infrastructure.Databases.Migrations
|
||||
|
||||
b.HasKey("Identifier");
|
||||
|
||||
b.HasIndex("Date");
|
||||
|
||||
b.HasIndex("Identifier")
|
||||
.IsUnique();
|
||||
|
||||
b.HasIndex("InitiatorIdentifier");
|
||||
|
||||
b.HasIndex("OpenTradeId");
|
||||
|
||||
b.HasIndex("Status");
|
||||
|
||||
@@ -23,6 +23,11 @@ public class PositionEntity
|
||||
[MaxLength(255)] public string AccountName { get; set; }
|
||||
|
||||
public int? UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Identifier of the bot or entity that initiated this position
|
||||
/// </summary>
|
||||
[Required] public Guid InitiatorIdentifier { get; set; }
|
||||
|
||||
// Foreign keys to trades
|
||||
public int? OpenTradeId { get; set; }
|
||||
|
||||
@@ -300,6 +300,7 @@ public class ManagingDbContext : DbContext
|
||||
entity.Property(e => e.SignalIdentifier).IsRequired().HasMaxLength(255);
|
||||
entity.Property(e => e.AccountName).IsRequired().HasMaxLength(255);
|
||||
entity.Property(e => e.UserId);
|
||||
entity.Property(e => e.InitiatorIdentifier).IsRequired();
|
||||
entity.Property(e => e.MoneyManagementJson).HasColumnType("text");
|
||||
|
||||
// Configure relationship with User
|
||||
@@ -333,7 +334,7 @@ public class ManagingDbContext : DbContext
|
||||
entity.HasIndex(e => e.Identifier).IsUnique();
|
||||
entity.HasIndex(e => e.UserId);
|
||||
entity.HasIndex(e => e.Status);
|
||||
entity.HasIndex(e => e.Date);
|
||||
entity.HasIndex(e => e.InitiatorIdentifier);
|
||||
|
||||
// Composite indexes
|
||||
entity.HasIndex(e => new { e.UserId, e.Identifier });
|
||||
|
||||
@@ -561,7 +561,8 @@ public static class PostgreSqlMappers
|
||||
entity.User != null ? Map(entity.User) : null)
|
||||
{
|
||||
Status = entity.Status,
|
||||
SignalIdentifier = entity.SignalIdentifier
|
||||
SignalIdentifier = entity.SignalIdentifier,
|
||||
InitiatorIdentifier = entity.InitiatorIdentifier
|
||||
};
|
||||
|
||||
// Set ProfitAndLoss with proper type
|
||||
@@ -596,6 +597,7 @@ public static class PostgreSqlMappers
|
||||
SignalIdentifier = position.SignalIdentifier,
|
||||
AccountName = position.AccountName,
|
||||
UserId = position.User?.Id ?? 0,
|
||||
InitiatorIdentifier = position.InitiatorIdentifier,
|
||||
MoneyManagementJson = position.MoneyManagement != null
|
||||
? JsonConvert.SerializeObject(position.MoneyManagement)
|
||||
: null
|
||||
|
||||
@@ -397,6 +397,22 @@ public class PostgreSqlTradingRepository : ITradingRepository
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Position>> GetPositionsByInitiatorIdentifierAsync(Guid initiatorIdentifier)
|
||||
{
|
||||
var positions = await _context.Positions
|
||||
.AsNoTracking()
|
||||
.Include(p => p.User)
|
||||
.Include(p => p.OpenTrade)
|
||||
.Include(p => p.StopLossTrade)
|
||||
.Include(p => p.TakeProfit1Trade)
|
||||
.Include(p => p.TakeProfit2Trade)
|
||||
.Where(p => p.InitiatorIdentifier == initiatorIdentifier)
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return PostgreSqlMappers.Map(positions);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Position>> GetAllPositionsAsync()
|
||||
{
|
||||
var positions = await _context.Positions
|
||||
|
||||
Reference in New Issue
Block a user