Files
Moonlight/Moonlight/Shared/Views/Profile/Index.razor
2023-07-17 00:48:27 +02:00

112 lines
4.0 KiB
Plaintext

@page "/profile"
@using Moonlight.Shared.Components.Navigations
@using Moonlight.App.Database.Entities
@using Moonlight.App.Models.Forms
@using Moonlight.App.Repositories
@using Mappy.Net
@using Moonlight.App.Exceptions
@using Moonlight.App.Helpers
@using Moonlight.App.Services.Sessions
@inject Repository<User> UserRepository
@inject IdentityService IdentityService
<ProfileNavigation Index="0" />
<LazyLoader Load="Load">
<SmartForm OnValidSubmit="Save" Model="Model">
<div class="card mb-5 mb-xl-10">
<div class="card-body p-9 @(IdentityService.User.StreamerMode ? "blur" : "")">
<div class="row">
<div class="col-lg-6 fv-row fv-plugins-icon-container">
<div class="mb-3">
<label class="form-label">
<TL>First name</TL>
</label>
<InputText @bind-Value="Model.FirstName" class="form-control"></InputText>
</div>
</div>
<div class="col-lg-6 fv-row fv-plugins-icon-container">
<div class="mb-3">
<label class="form-label">
<TL>Last name</TL>
</label>
<InputText @bind-Value="Model.LastName" class="form-control"></InputText>
</div>
</div>
</div>
<div class="mb-3">
<label class="form-label">
<TL>Email address</TL>
</label>
<InputText @bind-Value="Model.Email" class="form-control"></InputText>
</div>
<div class="mb-3">
<label class="form-label">
<TL>Address</TL>
</label>
<InputText @bind-Value="Model.Address" class="form-control"></InputText>
</div>
<div class="mb-3">
<label class="form-label">
<TL>City</TL>
</label>
<InputText @bind-Value="Model.City" class="form-control"></InputText>
</div>
<div class="mb-3">
<label class="form-label">
<TL>State</TL>
</label>
<InputText @bind-Value="Model.State" class="form-control"></InputText>
</div>
<div class="mb-3">
<label class="form-label">
<TL>Country</TL>
</label>
<InputText @bind-Value="Model.Country" class="form-control"></InputText>
</div>
</div>
<div class="card-footer d-flex justify-content-end py-6 px-9">
<button type="submit" class="btn btn-primary">
<TL>Save</TL>
</button>
</div>
</div>
</SmartForm>
</LazyLoader>
@code
{
private UserDataModel Model = new();
private Task Load(LazyLoader loader)
{
Model = Mapper.Map<UserDataModel>(IdentityService.User);
return Task.CompletedTask;
}
private Task Save()
{
// Prevent users from locking out other users by changing their email
Model.Email = Model.Email.ToLower();
var userWithThatEmail = UserRepository
.Get()
.FirstOrDefault(x => x.Email == Model.Email);
if (userWithThatEmail != null && IdentityService.User.Id != userWithThatEmail.Id)
{
Logger.Warn($"A user tried to lock another user out by changing the email. Email: {Model.Email}", "security");
throw new DisplayException("A user with that email does already exist");
}
var user = Mapper.Map(IdentityService.User, Model);
UserRepository.Update(user);
return Task.CompletedTask;
}
}