Ejemplo n.º 1
0
        public async Task <Result <UserInternalDto> > AddInternalAsync(UserInternalDto entity)
        {
            var resService = new Result <UserInternalDto>();

            try
            {
                var ctx   = GetOpenConnection();
                var users = new GenericRepositoryEF <KntDbContext, User>(ctx);

                var newEntity = entity.GetSimpleDto <User>();

                var resRep = await users.AddAsync(newEntity);

                resService.Entity = resRep.Entity?.GetSimpleDto <UserInternalDto>();

                resService.ErrorList = resRep.ErrorList;

                await CloseIsTempConnection(ctx);
            }
            catch (Exception ex)
            {
                AddExecptionsMessagesToErrorsList(ex, resService.ErrorList);
            }
            return(ResultDomainAction(resService));
        }
Ejemplo n.º 2
0
    public async Task <Result <UserInternalDto> > AddInternalAsync(UserInternalDto entity)
    {
        var result = new Result <UserInternalDto>();

        try
        {
            var db = GetOpenConnection();

            var sql = @"INSERT INTO Users (UserId, UserName, EMail, FullName, RoleDefinition, Disabled, PasswordHash, PasswordSalt )
                        VALUES (@UserId, @UserName, @EMail, @FullName, @RoleDefinition, @Disabled, @PasswordHash, @PasswordSalt)";

            var r = await db.ExecuteAsync(sql.ToString(),
                                          new { entity.UserId, entity.UserName, entity.EMail, entity.FullName, entity.RoleDefinition, entity.Disabled, entity.PasswordHash, entity.PasswordSalt });

            if (r == 0)
            {
                result.ErrorList.Add("Entity not inserted");
            }

            result.Entity = entity;

            await CloseIsTempConnection(db);
        }
        catch (Exception ex)
        {
            AddExecptionsMessagesToErrorsList(ex, result.ErrorList);
        }
        return(ResultDomainAction(result));
    }
Ejemplo n.º 3
0
        public async Task <Result <UserDto> > Create(UserRegisterDto userRegisterInfo)
        {
            Result <UserDto> resService = new Result <UserDto>();

            var password = userRegisterInfo.Password;

            try
            {
                if (string.IsNullOrWhiteSpace(password))
                {
                    throw new AppException("Password is required");
                }

                if ((await _repository.Users.GetInternalAsync(userRegisterInfo.UserName)).Entity != null)
                {
                    throw new AppException("Username \"" + userRegisterInfo.UserName + "\" is already taken");
                }
                else
                {
                    byte[] passwordHash, passwordSalt;
                    CreatePasswordHash(password, out passwordHash, out passwordSalt);

                    var newEntity = new UserInternalDto();
                    newEntity.SetSimpleDto(userRegisterInfo);
                    newEntity.UserId       = Guid.NewGuid();
                    newEntity.PasswordHash = passwordHash;
                    newEntity.PasswordSalt = passwordSalt;

                    var resRep = await _repository.Users.AddInternalAsync(newEntity);

                    resService.Entity = resRep.Entity?.GetSimpleDto <UserDto>();
                    if (!resRep.IsValid)
                    {
                        CopyErrorList(resRep.ErrorList, resService.ErrorList);
                    }
                }
            }
            catch (Exception ex)
            {
                if (resService == null)
                {
                    resService = new Result <UserDto>();
                }
                AddExecptionsMessagesToErrorsList(ex, resService.ErrorList);
            }
            return(ResultDomainAction(resService));
        }