Esempio n. 1
0
        public async Task <bool> CheckAccount(CheckAccountDto dto)
        {
            var query = new StringBuilder();

            query.Append("bys_user.email = @Email");
            if (dto.UserName != null)
            {
                query.Append(" OR bys_account.username = @UserName");
            }

            var param = new
            {
                UserName = dto.UserName,
                Email    = dto.Email
            };

            var user = (await this.DatabaseConnectService.Connection.FindAsync <User>(x => x
                                                                                      .Include <Data.Entity.Account.Account>(j => j.LeftOuterJoin())
                                                                                      .Where($"{query}")
                                                                                      .WithParameters(param))).FirstOrDefault();

            return(user != null ? (user.Email == dto.Email ? true : throw new BusinessException("Username or email exists", ErrorCode.USERNAME_EMAIL_EXIST)) : true);
        }
Esempio n. 2
0
        public async Task <DetailUserResultDto> CreateOrUpdateUser(CreateOrUpdateUserDto dto)
        {
            ValidateCreateOrUpDateUser(dto);

            var checkAccount = new CheckAccountDto
            {
                UserName = dto.Id == null ? dto.UserName : null,
                Email    = dto.Email
            };

            await CheckAccount(checkAccount);

            var now = DateTime.UtcNow;

            using (IDbTransaction trans = this.DatabaseConnectService.Connection.BeginTransaction())
            {
                try
                {
                    if (dto.Id == null)
                    {
                        var user = dto.ToUser();
                        user.CreatedAt = now;
                        user.CreatedBy = _sessionService.UserId;
                        await this.DatabaseConnectService.Connection.InsertAsync <User>(user, x => x.AttachToTransaction(trans));

                        var account = dto.ToAccount();
                        account.CreatedAt = now;
                        account.CreatedBy = _sessionService.UserId;
                        account.UserId    = user.Id;
                        await this.DatabaseConnectService.Connection.InsertAsync <Data.Entity.Account.Account>(account, x => x.AttachToTransaction(trans));

                        if (dto.Group != null)
                        {
                            var userGroup = dto.Group.ToUserGroup();
                            userGroup.UserId    = user.Id;
                            userGroup.CreatedAt = now;

                            await this.DatabaseConnectService.Connection.InsertAsync <UserGroup>(userGroup, x => x.AttachToTransaction(trans));
                        }
                        dto.Id = user.Id;
                    }
                    else
                    {
                        var user = (await this.DatabaseConnectService.Connection.FindAsync <User>(x => x
                                                                                                  .AttachToTransaction(trans)
                                                                                                  .Include <UserGroup>(join => join.LeftOuterJoin())
                                                                                                  .Where($"bys_user.id = @UserId")
                                                                                                  .WithParameters(new { UserId = dto.Id }))).FirstOrDefault();

                        var userGroupAlive = user.UserGroups.Where(x => x.Status == EntityStatus.Alive.ToString()).FirstOrDefault();

                        var userInsert = dto.ToUser();
                        userInsert.ModifiedAt = now;
                        userInsert.UpdateBy   = _sessionService.UserId;
                        userInsert.CreatedAt  = user.CreatedAt;
                        userInsert.CreatedBy  = user.CreatedBy;

                        await this.DatabaseConnectService.Connection.UpdateAsync <User>(userInsert, x => x.AttachToTransaction(trans));

                        if (dto.Group != null)
                        {
                            var userGroupInsert = dto.Group.ToUserGroup();
                            userGroupInsert.UserId    = user.Id;
                            userGroupInsert.CreatedAt = now;
                            await this.DatabaseConnectService.Connection.InsertAsync <UserGroup>(userGroupInsert, x => x.AttachToTransaction(trans));

                            if (userGroupAlive != null)
                            {
                                userGroupAlive.Status = EntityStatus.Delete.ToString();
                                await this.DatabaseConnectService.Connection.UpdateAsync <UserGroup>(userGroupAlive, x => x.AttachToTransaction(trans));
                            }
                        }
                    }

                    trans.Commit();

                    _redisCache.Remove(CacheConst.AllUser);

                    return(await GetDetailUserById((Guid)dto.Id));
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw new BusinessException(e.Message, ErrorCode.INTERNAL_SERVER_ERROR);
                }
            }
        }