Add user settings update functionality in UserController and UserService

Implement a new endpoint in UserController to allow users to update their settings. The UserService is updated to handle the logic for updating user settings, including partial updates for various fields. Additionally, the User entity and database schema are modified to accommodate new user settings properties, ensuring persistence and retrieval of user preferences.
This commit is contained in:
2025-12-30 05:54:15 +07:00
parent 95e60108af
commit 79d8a381d9
13 changed files with 2022 additions and 2 deletions

View File

@@ -145,4 +145,29 @@ public class UserController : BaseController
return StatusCode(500, $"Failed to send test message: {ex.Message}");
}
}
/// <summary>
/// Updates the user settings for the current user.
/// </summary>
/// <param name="settings">The user settings to update.</param>
/// <returns>The updated user with the new settings.</returns>
[Authorize]
[HttpPut("settings")]
public async Task<ActionResult<User>> UpdateUserSettings([FromBody] UpdateUserSettingsRequest settings)
{
var user = await GetUser();
// Map API request to DTO
var settingsDto = new Managing.Application.Abstractions.Models.UserSettingsDto
{
LowEthAmountAlert = settings.LowEthAmountAlert,
EnableAutoswap = settings.EnableAutoswap,
AutoswapAmount = settings.AutoswapAmount,
MaxWaitingTimeForPositionToGetFilledSeconds = settings.MaxWaitingTimeForPositionToGetFilledSeconds,
MaxTxnGasFeePerPosition = settings.MaxTxnGasFeePerPosition,
IsGmxEnabled = settings.IsGmxEnabled,
MinimumConfidence = settings.MinimumConfidence
};
var updatedUser = await _userService.UpdateUserSettings(user, settingsDto);
return Ok(updatedUser);
}
}

View File

@@ -0,0 +1,16 @@
using Managing.Common;
using static Managing.Common.Enums;
namespace Managing.Api.Models.Requests;
public class UpdateUserSettingsRequest
{
public decimal? LowEthAmountAlert { get; set; }
public bool? EnableAutoswap { get; set; }
public decimal? AutoswapAmount { get; set; }
public int? MaxWaitingTimeForPositionToGetFilledSeconds { get; set; }
public decimal? MaxTxnGasFeePerPosition { get; set; }
public bool? IsGmxEnabled { get; set; }
public Confidence? MinimumConfidence { get; set; }
}

View File

@@ -0,0 +1,15 @@
using static Managing.Common.Enums;
namespace Managing.Application.Abstractions.Models;
public class UserSettingsDto
{
public decimal? LowEthAmountAlert { get; set; }
public bool? EnableAutoswap { get; set; }
public decimal? AutoswapAmount { get; set; }
public int? MaxWaitingTimeForPositionToGetFilledSeconds { get; set; }
public decimal? MaxTxnGasFeePerPosition { get; set; }
public bool? IsGmxEnabled { get; set; }
public Confidence? MinimumConfidence { get; set; }
}

View File

@@ -1,4 +1,5 @@
using Managing.Application.Abstractions.Shared;
using Managing.Application.Abstractions.Models;
using Managing.Application.Abstractions.Shared;
using Managing.Domain.Users;
using static Managing.Common.Enums;
@@ -11,6 +12,7 @@ public interface IUserService
Task<User> UpdateAgentName(User user, string agentName);
Task<User> UpdateAvatarUrl(User user, string avatarUrl);
Task<User> UpdateTelegramChannel(User user, string telegramChannel);
Task<User> UpdateUserSettings(User user, UserSettingsDto settings);
Task<User> GetUserByName(string name);
Task<User> GetUserByAgentName(string agentName);
Task<User> GetUserByIdAsync(int userId);

View File

@@ -1,5 +1,6 @@
using System.Text.RegularExpressions;
using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions.Models;
using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Application.Abstractions.Shared;
@@ -338,6 +339,36 @@ public class UserService : IUserService
return user;
}
public async Task<User> UpdateUserSettings(User user, UserSettingsDto settings)
{
user = await GetUserByName(user.Name);
// Update settings only if values are provided (partial update support)
if (settings.LowEthAmountAlert.HasValue)
user.LowEthAmountAlert = settings.LowEthAmountAlert.Value;
if (settings.EnableAutoswap.HasValue)
user.EnableAutoswap = settings.EnableAutoswap.Value;
if (settings.AutoswapAmount.HasValue)
user.AutoswapAmount = settings.AutoswapAmount.Value;
if (settings.MaxWaitingTimeForPositionToGetFilledSeconds.HasValue)
user.MaxWaitingTimeForPositionToGetFilledSeconds = settings.MaxWaitingTimeForPositionToGetFilledSeconds.Value;
if (settings.MaxTxnGasFeePerPosition.HasValue)
user.MaxTxnGasFeePerPosition = settings.MaxTxnGasFeePerPosition.Value;
if (settings.IsGmxEnabled.HasValue)
user.IsGmxEnabled = settings.IsGmxEnabled.Value;
if (settings.MinimumConfidence.HasValue)
user.MinimumConfidence = settings.MinimumConfidence.Value;
await _userRepository.SaveOrUpdateUserAsync(user);
return user;
}
public async Task<User> GetUserByName(string name)
{
return await _userRepository.GetUserByNameAsync(name);

View File

@@ -1,5 +1,6 @@
using Managing.Domain.Accounts;
using Orleans;
using static Managing.Common.Enums;
namespace Managing.Domain.Users;
@@ -23,4 +24,13 @@ public class User
[Id(7)] public bool IsAdmin { get; set; } = false;
[Id(8)] public DateTimeOffset? LastConnectionDate { get; set; }
// User Settings
[Id(9)] public decimal? LowEthAmountAlert { get; set; }
[Id(10)] public bool EnableAutoswap { get; set; } = false;
[Id(11)] public decimal? AutoswapAmount { get; set; }
[Id(12)] public int? MaxWaitingTimeForPositionToGetFilledSeconds { get; set; }
[Id(13)] public decimal? MaxTxnGasFeePerPosition { get; set; }
[Id(14)] public bool IsGmxEnabled { get; set; } = false;
[Id(15)] public Confidence? MinimumConfidence { get; set; }
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,90 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Managing.Infrastructure.Databases.Migrations
{
/// <inheritdoc />
public partial class AddUserSettings : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<decimal>(
name: "AutoswapAmount",
table: "Users",
type: "numeric(18,8)",
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "EnableAutoswap",
table: "Users",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "IsGmxEnabled",
table: "Users",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<decimal>(
name: "LowEthAmountAlert",
table: "Users",
type: "numeric(18,8)",
nullable: true);
migrationBuilder.AddColumn<decimal>(
name: "MaxTxnGasFeePerPosition",
table: "Users",
type: "numeric(18,8)",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "MaxWaitingTimeForPositionToGetFilledSeconds",
table: "Users",
type: "integer",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "MinimumConfidence",
table: "Users",
type: "text",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AutoswapAmount",
table: "Users");
migrationBuilder.DropColumn(
name: "EnableAutoswap",
table: "Users");
migrationBuilder.DropColumn(
name: "IsGmxEnabled",
table: "Users");
migrationBuilder.DropColumn(
name: "LowEthAmountAlert",
table: "Users");
migrationBuilder.DropColumn(
name: "MaxTxnGasFeePerPosition",
table: "Users");
migrationBuilder.DropColumn(
name: "MaxWaitingTimeForPositionToGetFilledSeconds",
table: "Users");
migrationBuilder.DropColumn(
name: "MinimumConfidence",
table: "Users");
}
}
}

View File

@@ -1425,16 +1425,37 @@ namespace Managing.Infrastructure.Databases.Migrations
.HasMaxLength(255)
.HasColumnType("character varying(255)");
b.Property<decimal?>("AutoswapAmount")
.HasColumnType("decimal(18,8)");
b.Property<string>("AvatarUrl")
.HasMaxLength(500)
.HasColumnType("character varying(500)");
b.Property<bool>("EnableAutoswap")
.HasColumnType("boolean");
b.Property<bool>("IsAdmin")
.HasColumnType("boolean");
b.Property<bool>("IsGmxEnabled")
.HasColumnType("boolean");
b.Property<DateTimeOffset?>("LastConnectionDate")
.HasColumnType("timestamp with time zone");
b.Property<decimal?>("LowEthAmountAlert")
.HasColumnType("decimal(18,8)");
b.Property<decimal?>("MaxTxnGasFeePerPosition")
.HasColumnType("decimal(18,8)");
b.Property<int?>("MaxWaitingTimeForPositionToGetFilledSeconds")
.HasColumnType("integer");
b.Property<string>("MinimumConfidence")
.HasColumnType("text");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)

View File

@@ -1,6 +1,8 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Managing.Common;
using Microsoft.EntityFrameworkCore;
using static Managing.Common.Enums;
namespace Managing.Infrastructure.Databases.PostgreSql.Entities;
@@ -17,6 +19,15 @@ public class UserEntity
public DateTimeOffset? LastConnectionDate { get; set; }
public bool IsAdmin { get; set; }
// User Settings
[Column(TypeName = "decimal(18,8)")] public decimal? LowEthAmountAlert { get; set; }
public bool EnableAutoswap { get; set; } = false;
[Column(TypeName = "decimal(18,8)")] public decimal? AutoswapAmount { get; set; }
public int? MaxWaitingTimeForPositionToGetFilledSeconds { get; set; }
[Column(TypeName = "decimal(18,8)")] public decimal? MaxTxnGasFeePerPosition { get; set; }
public bool IsGmxEnabled { get; set; } = false;
public Confidence? MinimumConfidence { get; set; }
// Navigation properties
public virtual ICollection<AccountEntity> Accounts { get; set; } = new List<AccountEntity>();
}

View File

@@ -101,6 +101,8 @@ public class ManagingDbContext : DbContext
entity.Property(e => e.AgentName).HasMaxLength(255);
entity.Property(e => e.AvatarUrl).HasMaxLength(500);
entity.Property(e => e.TelegramChannel).HasMaxLength(255);
entity.Property(e => e.MinimumConfidence)
.HasConversion<string>(); // Store enum as string
// Create indexes for performance
entity.HasIndex(e => e.Name).IsUnique();

View File

@@ -133,6 +133,14 @@ public static class PostgreSqlMappers
OwnerWalletAddress = entity.OwnerWalletAddress,
Id = entity.Id, // Assuming Id is the primary key for UserEntity
IsAdmin = entity.IsAdmin,
LastConnectionDate = entity.LastConnectionDate,
LowEthAmountAlert = entity.LowEthAmountAlert,
EnableAutoswap = entity.EnableAutoswap,
AutoswapAmount = entity.AutoswapAmount,
MaxWaitingTimeForPositionToGetFilledSeconds = entity.MaxWaitingTimeForPositionToGetFilledSeconds,
MaxTxnGasFeePerPosition = entity.MaxTxnGasFeePerPosition,
IsGmxEnabled = entity.IsGmxEnabled,
MinimumConfidence = entity.MinimumConfidence,
Accounts = entity.Accounts?.Select(MapAccountWithoutUser).ToList() ?? new List<Account>()
};
}
@@ -167,7 +175,15 @@ public static class PostgreSqlMappers
AvatarUrl = user.AvatarUrl,
TelegramChannel = user.TelegramChannel,
OwnerWalletAddress = user.OwnerWalletAddress,
IsAdmin = user.IsAdmin
IsAdmin = user.IsAdmin,
LastConnectionDate = user.LastConnectionDate,
LowEthAmountAlert = user.LowEthAmountAlert,
EnableAutoswap = user.EnableAutoswap,
AutoswapAmount = user.AutoswapAmount,
MaxWaitingTimeForPositionToGetFilledSeconds = user.MaxWaitingTimeForPositionToGetFilledSeconds,
MaxTxnGasFeePerPosition = user.MaxTxnGasFeePerPosition,
IsGmxEnabled = user.IsGmxEnabled,
MinimumConfidence = user.MinimumConfidence
};
}

View File

@@ -256,6 +256,14 @@ public class PostgreSqlUserRepository : BaseRepositoryWithLogging, IUserReposito
existingUser.TelegramChannel = user.TelegramChannel;
existingUser.OwnerWalletAddress = user.OwnerWalletAddress;
existingUser.IsAdmin = user.IsAdmin;
existingUser.LastConnectionDate = user.LastConnectionDate;
existingUser.LowEthAmountAlert = user.LowEthAmountAlert;
existingUser.EnableAutoswap = user.EnableAutoswap;
existingUser.AutoswapAmount = user.AutoswapAmount;
existingUser.MaxWaitingTimeForPositionToGetFilledSeconds = user.MaxWaitingTimeForPositionToGetFilledSeconds;
existingUser.MaxTxnGasFeePerPosition = user.MaxTxnGasFeePerPosition;
existingUser.IsGmxEnabled = user.IsGmxEnabled;
existingUser.MinimumConfidence = user.MinimumConfidence;
_context.Users.Update(existingUser);