2021-10-26 22:52:39 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
|
|
|
|
|
|
using Selector.Model;
|
2021-12-19 18:44:03 +00:00
|
|
|
using Selector.Events;
|
2021-10-26 22:52:39 +01:00
|
|
|
|
|
|
|
namespace Selector.Web.Areas.Identity.Pages.Account.Manage
|
|
|
|
{
|
|
|
|
public partial class LastFmModel : PageModel
|
|
|
|
{
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
2021-12-19 18:44:03 +00:00
|
|
|
private readonly UserEventBus UserEvent;
|
2021-10-26 22:52:39 +01:00
|
|
|
|
|
|
|
public LastFmModel(
|
2021-12-19 18:44:03 +00:00
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
UserEventBus userEvent)
|
2021-10-26 22:52:39 +01:00
|
|
|
{
|
|
|
|
_userManager = userManager;
|
2021-12-19 18:44:03 +00:00
|
|
|
UserEvent = userEvent;
|
2021-10-26 22:52:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[TempData]
|
|
|
|
public string StatusMessage { get; set; }
|
|
|
|
|
|
|
|
[BindProperty]
|
|
|
|
public InputModel Input { get; set; }
|
|
|
|
|
|
|
|
public class InputModel
|
|
|
|
{
|
|
|
|
[Display(Name = "Username")]
|
|
|
|
public string Username { get; set; }
|
|
|
|
}
|
|
|
|
|
2021-10-27 17:31:29 +01:00
|
|
|
private Task LoadAsync(ApplicationUser user)
|
2021-10-26 22:52:39 +01:00
|
|
|
{
|
|
|
|
Input = new InputModel
|
|
|
|
{
|
|
|
|
Username = user.LastFmUsername,
|
|
|
|
};
|
2021-10-27 17:31:29 +01:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2021-10-26 22:52:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IActionResult> OnGetAsync()
|
|
|
|
{
|
|
|
|
var user = await _userManager.GetUserAsync(User);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
await LoadAsync(user);
|
|
|
|
return Page();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<IActionResult> OnPostChangeUsernameAsync()
|
|
|
|
{
|
|
|
|
var user = await _userManager.GetUserAsync(User);
|
|
|
|
if (user == null)
|
|
|
|
{
|
|
|
|
return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
await LoadAsync(user);
|
|
|
|
return Page();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input.Username != user.LastFmUsername)
|
|
|
|
{
|
2021-12-19 18:44:03 +00:00
|
|
|
var oldUsername = user.LastFmUsername;
|
2021-12-05 00:29:38 +00:00
|
|
|
user.LastFmUsername = Input.Username?.Trim();
|
2021-12-19 18:44:03 +00:00
|
|
|
|
2021-10-27 17:31:29 +01:00
|
|
|
await _userManager.UpdateAsync(user);
|
2021-12-19 18:44:03 +00:00
|
|
|
UserEvent.OnLastfmCredChange(this, new LastfmChange {
|
|
|
|
UserId = user.Id,
|
|
|
|
PreviousUsername = oldUsername,
|
|
|
|
NewUsername = user.LastFmUsername
|
|
|
|
});
|
2021-10-26 22:52:39 +01:00
|
|
|
|
|
|
|
StatusMessage = "Username changed";
|
|
|
|
return RedirectToPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusMessage = "Username unchanged";
|
|
|
|
return RedirectToPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|