Files
rr3-server/RR3CommunityServer/Pages/DeviceSettings.cshtml.cs
Daniel Elliott 8ba7c605f1 Add device settings management and web panel sync API
Features:
- New DeviceSettings admin page at /devicesettings
- Manage device server configurations (URL, mode, deviceId)
- 3 new API endpoints for APK sync functionality
- UserSettings database model with SQLite storage

Implementation:
- ServerSettingsController.cs with getUserSettings, updateUserSettings, getAllUserSettings
- DeviceSettings.cshtml Razor page with add/edit/delete UI
- DeviceSettings.cshtml.cs page model with CRUD operations
- UserSettings model added to ApiModels.cs
- UserSettings DbSet added to RR3DbContext
- EF Core migration: 20260219180936_AddUserSettings
- Link added to Admin dashboard

API Endpoints:
- GET /api/settings/getUserSettings?deviceId={id} - APK sync endpoint
- POST /api/settings/updateUserSettings - Web panel update
- GET /api/settings/getAllUserSettings - Admin list view

Database Schema:
- UserSettings table (Id, DeviceId, ServerUrl, Mode, LastUpdated)
- SQLite storage with EF Core migrations

Integration:
- Works with APK SettingsActivity sync button
- Real-time configuration updates
- Emoji logging for all operations
- Device-specific server URL management

Usage:
1. Admin configures device settings at /devicesettings
2. User opens RR3 APK and taps Sync from Web Panel
3. APK downloads settings via API
4. Settings saved to SharedPreferences
5. Game restart applies configuration

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-02-19 10:15:02 -08:00

109 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using RR3CommunityServer.Data;
using RR3CommunityServer.Models;
namespace RR3CommunityServer.Pages;
public class DeviceSettingsModel : PageModel
{
private readonly RR3DbContext _context;
private readonly ILogger<DeviceSettingsModel> _logger;
public DeviceSettingsModel(RR3DbContext context, ILogger<DeviceSettingsModel> logger)
{
_context = context;
_logger = logger;
}
public List<UserSettings> DeviceSettings { get; set; } = new();
public string CurrentServerUrl { get; set; } = string.Empty;
public async Task OnGetAsync()
{
CurrentServerUrl = $"{Request.Scheme}://{Request.Host}";
DeviceSettings = await _context.UserSettings
.OrderByDescending(s => s.LastUpdated)
.ToListAsync();
_logger.LogInformation($"📋 Loaded {DeviceSettings.Count} device settings");
}
public async Task<IActionResult> OnPostAddOrUpdateAsync(string deviceId, string mode, string serverUrl)
{
try
{
if (string.IsNullOrWhiteSpace(deviceId))
{
TempData["Error"] = "Device ID is required";
return RedirectToPage();
}
_logger.LogInformation($"🔄 Adding/Updating settings: deviceId={deviceId}, mode={mode}, url={serverUrl}");
var existingSettings = await _context.UserSettings
.Where(s => s.DeviceId == deviceId)
.FirstOrDefaultAsync();
if (existingSettings == null)
{
// Create new
var newSettings = new UserSettings
{
DeviceId = deviceId,
Mode = mode,
ServerUrl = serverUrl ?? string.Empty,
LastUpdated = DateTime.UtcNow
};
_context.UserSettings.Add(newSettings);
_logger.LogInformation($" Created new settings for {deviceId}");
TempData["Message"] = $"Settings created for device: {deviceId}";
}
else
{
// Update existing
existingSettings.Mode = mode;
existingSettings.ServerUrl = serverUrl ?? string.Empty;
existingSettings.LastUpdated = DateTime.UtcNow;
_logger.LogInformation($"✏️ Updated settings for {deviceId}");
TempData["Message"] = $"Settings updated for device: {deviceId}";
}
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "❌ Error saving device settings");
TempData["Error"] = "Failed to save settings";
}
return RedirectToPage();
}
public async Task<IActionResult> OnPostDeleteAsync(string deviceId)
{
try
{
var settings = await _context.UserSettings
.Where(s => s.DeviceId == deviceId)
.FirstOrDefaultAsync();
if (settings != null)
{
_context.UserSettings.Remove(settings);
await _context.SaveChangesAsync();
_logger.LogInformation($"🗑️ Deleted settings for {deviceId}");
TempData["Message"] = $"Settings deleted for device: {deviceId}";
}
}
catch (Exception ex)
{
_logger.LogError(ex, "❌ Error deleting device settings");
TempData["Error"] = "Failed to delete settings";
}
return RedirectToPage();
}
}