feat: 添加用户保存验证

pull/1/head
Yosheng 2023-07-30 15:36:40 +08:00
parent d04bc75105
commit 23d45d7084
2 changed files with 33 additions and 11 deletions

View File

@ -60,6 +60,15 @@ public class UserService : IUserService
public async Task CreateUser(User input)
{
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync();
if (_userPointManagementDbContext.Users.Any(x => x.Mobile == input.Mobile))
{
throw new ArgumentException("手机号不可重复!");
}
if (_userPointManagementDbContext.Users.Any(x => x.Name == input.Name))
{
throw new ArgumentException("名称不可重复!");
}
_userPointManagementDbContext.Users.Add(input);
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
}

View File

@ -6,6 +6,7 @@
@using global::UserPointManagement.Model.Entities
@inject IJSRuntime JS
@inject IUserService UserService;
@inject MessageService Message
<PageContainer Title="用户管理">
<GridRow Style="margin: 10px 0">
@ -84,13 +85,14 @@
<Form Loading="loading" Model="@model"
LabelColSpan="8"
WrapperColSpan="16"
ValidateMode=@FormValidateMode.Rules
OnFinish="OnFinish"
OnFinishFailed="OnFinishFailed"
@ref="@_form">
<FormItem Label="姓名">
<FormItem Label="姓名" Rules=@(new FormValidationRule[] { new FormValidationRule { Required = true, Message = "名称不可为空!" }, new FormValidationRule() { Len = 50, Message = "名称上限50字" } })>
<Input @bind-Value="@context.Name"/>
</FormItem>
<FormItem Label="手机号">
<FormItem Label="手机号" Rules="@(new FormValidationRule[] { new FormValidationRule { Type = FormFieldType.Regexp, Pattern = @"^1\d{10}$", Message = "请输入正确手机号", Required = true } })">
<Input @bind-Value="@context.Mobile"/>
</FormItem>
</Form>
@ -102,7 +104,6 @@
public class Model
{
[Required]
public string Name { get; set; }
public string Mobile { get; set; }
@ -148,10 +149,9 @@
/// <summary>
/// when form is submited, close the modal
/// </summary>
/// <param name="args"></param>
/// <param name="editContext"></param>
private void OnFinish(EditContext editContext)
{
Console.WriteLine("e");
_visible = false;
}
@ -161,13 +161,26 @@
/// <param name="e"></param>
private async Task HandleOk(MouseEventArgs e)
{
_form.Submit();
await UserService.CreateUser(new User()
if (_form.Validate())
{
Name = model.Name,
Mobile = model.Mobile
}).ConfigureAwait(false);
await RefreshTable().ConfigureAwait(false);
try
{
await UserService.CreateUser(new User()
{
Name = model.Name,
Mobile = model.Mobile
}).ConfigureAwait(false);
}
catch (Exception exception)
{
await Message.Error(exception.Message);
}
_visible = false;
model.Mobile = string.Empty;
model.Name = string.Empty;
await RefreshTable().ConfigureAwait(false);
await Message.Success("保存成功!");
}
}
}