public async Task <UserInfoResponse> ModifyUser(UpdateUserInfoRequest request, int userId) { Check.Value(request, "Request").NotNull(); request.Validate(ImmediateValidator.Instance); using (var conn = _dbConnFactory.CreateConnection()) { var uniqueEmail = await conn.QueryFirstOrDefaultAsync <string>("SELECT email FROM users WHERE " + $"email='{request.Email}' AND id<>'{userId}'"); Check.Value(uniqueEmail, "Request").IsNull("This email already exist"); var uniqueNickName = await conn.QueryFirstOrDefaultAsync <string>("SELECT nick_name FROM users WHERE " + $"nick_name='{request.NickName}' AND id<>'{userId}'"); Check.Value(uniqueNickName, "Request").IsNull("This email already exist"); var dbUser = await conn.GetUserById(userId); Check.Value(dbUser).NotNull("User doesn't exist"); conn.Execute("UPDATE users SET first_name=@FirstName, last_name=@LastName, patronymic=@Patronymic, " + $"nick_name=@NickName, email=@Email, phone_number=@PhoneNumber, description=@Description where id='{userId}'", request); return(AutoMapper.Mapper.Map <UpdateUserInfoRequest, UserInfoResponse>(request)); } }
public async Task <UserInfoResponse> ModifyUser(UpdateUserInfoRequest request, int userId) { Check.Value(request, "Request").NotNull(); request.Validate(ImmediateValidator.Instance); using (var conn = _dbConnFactory.CreateConnection()) { var uniqueEmail = await conn.CheckUniqueEmail(request.Email, userId); Check.Value(uniqueEmail, "Request").IsNull("This email already exist"); var uniqueNickName = await conn.CheckUniqueNickName(request.NickName, userId); Check.Value(uniqueNickName, "Request").IsNull("This email already exist"); var dbUser = await conn.GetUserById(userId); Check.Value(dbUser).NotNull("User doesn't exist"); conn.UpdateUser(userId, request); return(AutoMapper.Mapper.Map <UpdateUserInfoRequest, UserInfoResponse>(request)); } }