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) public async Task CreateUser(User input)
{ {
await using var _userPointManagementDbContext = await _dbContextFactory.CreateDbContextAsync(); 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); _userPointManagementDbContext.Users.Add(input);
await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false); await _userPointManagementDbContext.SaveChangesAsync().ConfigureAwait(false);
} }

View File

@ -6,6 +6,7 @@
@using global::UserPointManagement.Model.Entities @using global::UserPointManagement.Model.Entities
@inject IJSRuntime JS @inject IJSRuntime JS
@inject IUserService UserService; @inject IUserService UserService;
@inject MessageService Message
<PageContainer Title="用户管理"> <PageContainer Title="用户管理">
<GridRow Style="margin: 10px 0"> <GridRow Style="margin: 10px 0">
@ -84,13 +85,14 @@
<Form Loading="loading" Model="@model" <Form Loading="loading" Model="@model"
LabelColSpan="8" LabelColSpan="8"
WrapperColSpan="16" WrapperColSpan="16"
ValidateMode=@FormValidateMode.Rules
OnFinish="OnFinish" OnFinish="OnFinish"
OnFinishFailed="OnFinishFailed" OnFinishFailed="OnFinishFailed"
@ref="@_form"> @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"/> <Input @bind-Value="@context.Name"/>
</FormItem> </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"/> <Input @bind-Value="@context.Mobile"/>
</FormItem> </FormItem>
</Form> </Form>
@ -102,7 +104,6 @@
public class Model public class Model
{ {
[Required]
public string Name { get; set; } public string Name { get; set; }
public string Mobile { get; set; } public string Mobile { get; set; }
@ -148,10 +149,9 @@
/// <summary> /// <summary>
/// when form is submited, close the modal /// when form is submited, close the modal
/// </summary> /// </summary>
/// <param name="args"></param> /// <param name="editContext"></param>
private void OnFinish(EditContext editContext) private void OnFinish(EditContext editContext)
{ {
Console.WriteLine("e");
_visible = false; _visible = false;
} }
@ -161,13 +161,26 @@
/// <param name="e"></param> /// <param name="e"></param>
private async Task HandleOk(MouseEventArgs e) private async Task HandleOk(MouseEventArgs e)
{ {
_form.Submit(); if (_form.Validate())
{
try
{
await UserService.CreateUser(new User() await UserService.CreateUser(new User()
{ {
Name = model.Name, Name = model.Name,
Mobile = model.Mobile Mobile = model.Mobile
}).ConfigureAwait(false); }).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 RefreshTable().ConfigureAwait(false);
await Message.Success("保存成功!");
}
} }
} }