From 23d45d708474039c00feac553e85845a388a2179 Mon Sep 17 00:00:00 2001 From: Yosheng Date: Sun, 30 Jul 2023 15:36:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/UserService.cs | 9 +++++ .../Pages/UserManagement/UserManagement.razor | 35 +++++++++++++------ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/UserPointManagement.Application/Services/UserService.cs b/src/UserPointManagement.Application/Services/UserService.cs index f6d1fa7..c1bd2d9 100644 --- a/src/UserPointManagement.Application/Services/UserService.cs +++ b/src/UserPointManagement.Application/Services/UserService.cs @@ -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); } diff --git a/src/UserPointManagement.Web/Pages/UserManagement/UserManagement.razor b/src/UserPointManagement.Web/Pages/UserManagement/UserManagement.razor index 5e6a0ab..6ffe8c1 100644 --- a/src/UserPointManagement.Web/Pages/UserManagement/UserManagement.razor +++ b/src/UserPointManagement.Web/Pages/UserManagement/UserManagement.razor @@ -6,6 +6,7 @@ @using global::UserPointManagement.Model.Entities @inject IJSRuntime JS @inject IUserService UserService; +@inject MessageService Message @@ -84,13 +85,14 @@
- + - + @@ -102,7 +104,6 @@ public class Model { - [Required] public string Name { get; set; } public string Mobile { get; set; } @@ -148,10 +149,9 @@ /// /// when form is submited, close the modal /// - /// + /// private void OnFinish(EditContext editContext) { - Console.WriteLine("e"); _visible = false; } @@ -161,13 +161,26 @@ /// 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("保存成功!"); + } } } \ No newline at end of file