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;
|
|
|
|
|
|
|
|
namespace Selector.Web.Areas.Identity.Pages.Account.Manage
|
|
|
|
{
|
|
|
|
public partial class LastFmModel : PageModel
|
|
|
|
{
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
|
|
|
|
public LastFmModel(
|
|
|
|
UserManager<ApplicationUser> userManager)
|
|
|
|
{
|
|
|
|
_userManager = userManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
[TempData]
|
|
|
|
public string StatusMessage { get; set; }
|
|
|
|
|
|
|
|
[BindProperty]
|
|
|
|
public InputModel Input { get; set; }
|
|
|
|
|
|
|
|
public class InputModel
|
|
|
|
{
|
|
|
|
[Required]
|
|
|
|
[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-10-27 17:31:29 +01:00
|
|
|
user.LastFmUsername = Input.Username.Trim();
|
|
|
|
await _userManager.UpdateAsync(user);
|
2021-10-26 22:52:39 +01:00
|
|
|
|
|
|
|
StatusMessage = "Username changed";
|
|
|
|
return RedirectToPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusMessage = "Username unchanged";
|
|
|
|
return RedirectToPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|