Просмотр исходного кода

😁增加更新或删除时判断实体对象是否存在

zuohuaijun 2 лет назад
Родитель
Сommit
f8271e0220

+ 2 - 1
Admin.NET/Admin.NET.Core/GlobalUsings.cs

@@ -36,6 +36,7 @@ global using Microsoft.Extensions.Configuration;
 global using Microsoft.Extensions.DependencyInjection;
 global using Microsoft.Extensions.Logging;
 global using Microsoft.Extensions.Options;
+global using NewLife;
 global using NewLife.Caching;
 global using Newtonsoft.Json;
 global using SKIT.FlurlHttpClient;
@@ -60,4 +61,4 @@ global using System.Text;
 global using System.Text.RegularExpressions;
 global using System.Web;
 global using UAParser;
-global using Yitter.IdGenerator;
+global using Yitter.IdGenerator;

+ 1 - 3
Admin.NET/Admin.NET.Core/Service/Org/SysOrgService.cs

@@ -7,8 +7,6 @@
 // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
 // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
 
-using NewLife;
-
 namespace Admin.NET.Core.Service;
 
 /// <summary>
@@ -176,7 +174,7 @@ public class SysOrgService : IDynamicApiController, ITransient
     [DisplayName("删除机构")]
     public async Task DeleteOrg(DeleteOrgInput input)
     {
-        var sysOrg = await _sysOrgRep.GetFirstAsync(u => u.Id == input.Id);
+        var sysOrg = await _sysOrgRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
 
         // 是否有权限操作此机构
         if (!_userManager.SuperAdmin)

+ 3 - 8
Admin.NET/Admin.NET.Core/Service/Pos/SysPosService.cs

@@ -51,8 +51,7 @@ public class SysPosService : IDynamicApiController, ITransient
     [DisplayName("增加职位")]
     public async Task AddPos(AddPosInput input)
     {
-        var isExist = await _sysPosRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code);
-        if (isExist)
+        if (await _sysPosRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code))
             throw Oops.Oh(ErrorCodeEnum.D6000);
 
         await _sysPosRep.InsertAsync(input.Adapt<SysPos>());
@@ -67,8 +66,7 @@ public class SysPosService : IDynamicApiController, ITransient
     [DisplayName("更新职位")]
     public async Task UpdatePos(UpdatePosInput input)
     {
-        var isExist = await _sysPosRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id);
-        if (isExist)
+        if (await _sysPosRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id))
             throw Oops.Oh(ErrorCodeEnum.D6000);
 
         var sysPos = await _sysPosRep.GetByIdAsync(input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D6003);
@@ -87,10 +85,7 @@ public class SysPosService : IDynamicApiController, ITransient
     [DisplayName("删除职位")]
     public async Task DeletePos(DeletePosInput input)
     {
-        var sysPos = await _sysPosRep.GetFirstAsync(u => u.Id == input.Id);
-        if (sysPos == null)
-            throw Oops.Oh(ErrorCodeEnum.D6003);
-
+        var sysPos = await _sysPosRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D6003);
         if (!_userManager.SuperAdmin && sysPos.CreateUserId != _userManager.UserId)
             throw Oops.Oh(ErrorCodeEnum.D6002);
 

+ 3 - 5
Admin.NET/Admin.NET.Core/Service/Role/SysRoleService.cs

@@ -80,8 +80,7 @@ public class SysRoleService : IDynamicApiController, ITransient
     [DisplayName("增加角色")]
     public async Task AddRole(AddRoleInput input)
     {
-        var isExist = await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code);
-        if (isExist)
+        if (await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code))
             throw Oops.Oh(ErrorCodeEnum.D1006);
 
         var newRole = await _sysRoleRep.AsInsertable(input.Adapt<SysRole>()).ExecuteReturnEntityAsync();
@@ -118,8 +117,7 @@ public class SysRoleService : IDynamicApiController, ITransient
     [DisplayName("更新角色")]
     public async Task UpdateRole(UpdateRoleInput input)
     {
-        var isExist = await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id);
-        if (isExist)
+        if (await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id))
             throw Oops.Oh(ErrorCodeEnum.D1006);
 
         await _sysRoleRep.AsUpdateable(input.Adapt<SysRole>()).IgnoreColumns(true)
@@ -138,7 +136,7 @@ public class SysRoleService : IDynamicApiController, ITransient
     [DisplayName("删除角色")]
     public async Task DeleteRole(DeleteRoleInput input)
     {
-        var sysRole = await _sysRoleRep.GetFirstAsync(u => u.Id == input.Id);
+        var sysRole = await _sysRoleRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
         if (sysRole.Code == CommonConst.SysAdminRole)
             throw Oops.Oh(ErrorCodeEnum.D1019);
 

+ 5 - 6
Admin.NET/Admin.NET.Core/Service/User/SysUserService.cs

@@ -136,7 +136,7 @@ public class SysUserService : IDynamicApiController, ITransient
     [DisplayName("删除用户")]
     public async Task DeleteUser(DeleteUserInput input)
     {
-        var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
+        var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D0009);
         if (user.AccountType == AccountTypeEnum.SuperAdmin)
             throw Oops.Oh(ErrorCodeEnum.D1014);
         if (user.Id == _userManager.UserId)
@@ -181,7 +181,7 @@ public class SysUserService : IDynamicApiController, ITransient
     [DisplayName("设置用户状态")]
     public async Task<int> SetStatus(UserInput input)
     {
-        var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.Id);
+        var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D0009);
         if (user.AccountType == AccountTypeEnum.SuperAdmin)
             throw Oops.Oh(ErrorCodeEnum.D1015);
 
@@ -200,7 +200,7 @@ public class SysUserService : IDynamicApiController, ITransient
     [DisplayName("授权用户角色")]
     public async Task GrantRole(UserRoleInput input)
     {
-        var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.UserId);
+        var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.UserId) ?? throw Oops.Oh(ErrorCodeEnum.D0009);
         if (user.AccountType == AccountTypeEnum.SuperAdmin)
             throw Oops.Oh(ErrorCodeEnum.D1022);
 
@@ -215,7 +215,7 @@ public class SysUserService : IDynamicApiController, ITransient
     [DisplayName("修改用户密码")]
     public async Task<int> ChangePwd(ChangePwdInput input)
     {
-        var user = await _sysUserRep.GetFirstAsync(u => u.Id == _userManager.UserId);
+        var user = await _sysUserRep.GetFirstAsync(u => u.Id == _userManager.UserId) ?? throw Oops.Oh(ErrorCodeEnum.D0009);
         if (CryptogramUtil.CryptoType == CryptogramEnum.MD5.ToString())
         {
             if (user.Password != MD5Encryption.Encrypt(input.PasswordOld))
@@ -239,9 +239,8 @@ public class SysUserService : IDynamicApiController, ITransient
     [DisplayName("重置用户密码")]
     public async Task<string> ResetPwd(ResetPwdUserInput input)
     {
+        var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D0009);
         var password = await _sysConfigService.GetConfigValue<string>(CommonConst.SysPassword);
-
-        var user = await _sysUserRep.GetFirstAsync(u => u.Id == input.Id);
         user.Password = CryptogramUtil.Encrypt(password);
         await _sysUserRep.AsUpdateable(user).UpdateColumns(u => u.Password).ExecuteCommandAsync();
         return password;