Ejemplo n.º 1
0
 /// <summary>
 /// 新建用户实例。
 /// </summary>
 /// <param name="user">用户实例对象。</param>
 /// <param name="cancellationToken">取消标志。</param>
 /// <returns>返回添加用户结果。</returns>
 public override async Task <IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken = default)
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (user == null)
     {
         throw new ArgumentNullException(nameof(user));
     }
     // ReSharper disable once SuspiciousTypeConversion.Global
     if (user is IUserEventHandler <TUser> handler)
     {
         if (await UserContext.BeginTransactionAsync(async db =>
         {
             if (!await db.CreateAsync(user, cancellationToken))
             {
                 return(false);
             }
             if (!await handler.OnCreatedAsync(db, cancellationToken))
             {
                 return(false);
             }
             return(true);
         }, cancellationToken: cancellationToken))
         {
             return(IdentityResult.Success);
         }
     }
     else if (await UserContext.CreateAsync(user, cancellationToken))
     {
         return(IdentityResult.Success);
     }
     return(IdentityResult.Failed(ErrorDescriber.DefaultError()));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 新建用户实例。
        /// </summary>
        /// <param name="user">用户实例对象。</param>
        /// <param name="cancellationToken">取消标志。</param>
        /// <returns>返回添加用户结果。</returns>
        public override async Task <IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            await UserContext.CreateAsync(user, cancellationToken);

            return(IdentityResult.Success);
        }
Ejemplo n.º 3
0
        public void DeleteUserTestMethod()
        {
            UserContext userContext = new UserContext();
            UserModel   user        = new UserModel()
            {
                Age = 20, Name = "Sorbon", Surname = "Rashidov", FullName = "Sorbon Rashidov"
            };

            int newUserId     = userContext.CreateAsync(user).GetAwaiter().GetResult();
            int userDeletedId = userContext.DeleteAsync(newUserId).GetAwaiter().GetResult();

            Assert.AreEqual(newUserId, userDeletedId);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 新建用户实例。
        /// </summary>
        /// <param name="user">用户实例对象。</param>
        /// <param name="cancellationToken">取消标志。</param>
        /// <returns>返回添加用户结果。</returns>
        public override async Task <IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var handlers = ServiceProvider.GetServices <IUserEventHandler <TUser> >().ToList();

            if (handlers.Count > 0)
            {
                handlers = handlers.OrderByDescending(x => x.Priority).ToList();
                if (await UserContext.BeginTransactionAsync(async db =>
                {
                    if (!await db.CreateAsync(user, cancellationToken))
                    {
                        return(false);
                    }

                    foreach (var handler in handlers)
                    {
                        try
                        {
                            if (!await handler.OnCreatedAsync(db, user, cancellationToken))
                            {
                                return(false);
                            }
                        }
                        catch (Exception exception)
                        {
                            Logger.LogError(exception, "添加用户发生错误:{0}", exception.Message);
                            return(false);
                        }
                    }

                    return(true);
                }, cancellationToken: cancellationToken))
                {
                    return(IdentityResult.Success);
                }
            }
            else if (await UserContext.CreateAsync(user, cancellationToken))
            {
                return(IdentityResult.Success);
            }

            return(IdentityResult.Failed(ErrorDescriber.DefaultError()));
        }
Ejemplo n.º 5
0
        public void CreateUserTestMethod()
        {
            UserContext userContext = new UserContext();
            UserModel   user        = new UserModel()
            {
                Age = 18, Name = "Faridun", Surname = "Berdiev", FullName = "Faridun Berdiev"
            };
            UserModel user1 = new UserModel()
            {
                Age = 19, Name = "Ramz", Surname = "Nazarov", FullName = "Ramz Nazarov"
            };
            UserModel user2 = new UserModel()
            {
                Age = 20, Name = "Sorbon", Surname = "Rashidov", FullName = "Sorbon Rashidov"
            };

            userContext.CreateAsync(user).GetAwaiter().GetResult();
            userContext.CreateAsync(user1).GetAwaiter().GetResult();
            userContext.CreateAsync(user2).GetAwaiter().GetResult();

            Assert.AreEqual(user, userContext.ReadAsync().GetAwaiter().GetResult().FirstOrDefault(x => x.Id == 1));
            Assert.AreEqual(user1, userContext.ReadAsync().GetAwaiter().GetResult().FirstOrDefault(x => x.Id == 2));
            Assert.AreEqual(user2, userContext.ReadAsync().GetAwaiter().GetResult().FirstOrDefault(x => x.Id == 3));
        }
Ejemplo n.º 6
0
        public void UpdateUserTestMethod()
        {
            UserContext userContext = new UserContext();
            UserModel   userEntity  = new UserModel()
            {
                Age = 18, Name = "Faridun", Surname = "Berdiev", FullName = "Faridun Berdiev"
            };
            UserModel userNewVersion = new UserModel()
            {
                Id = 1, Age = 22, Name = "Ramzier", Surname = "Nazarov", FullName = "Ramzier Nazarov"
            };

            int       newUserId  = userContext.CreateAsync(userEntity).GetAwaiter().GetResult();
            var       userExist  = userContext.ReadAsync().GetAwaiter().GetResult().FirstOrDefault(x => x.Id == newUserId);
            UserModel userEdited = userContext.UpdateAsync(newUserId, userNewVersion).GetAwaiter().GetResult();

            Assert.AreEqual(userNewVersion.FullName, userEdited.FullName);
        }
Ejemplo n.º 7
0
 public async Task <User> CreateAsync(UserCreateApiModel created, int userId)
 {
     return(await _userContext.CreateAsync(created, userId));
 }