在update方法中,Optionals始终在运行并将值设置为0。

huangapple go评论69阅读模式
英文:

Optionals in update method always running and setting values to 0

问题

在更新方法中使用了Optionals,一旦运行,将会更新它们各自的PostgreSQL数据库列,前面几个是字符串,可以正常更新。而最后两个是整数(INT)类型(Authorisation和ChapterProgress),当我调用第一个更新其值时,会将另一个基于整数的可选项设置为0,反之亦然。问题代码在以下的最后两个可选项中:

Optional.ofNullable(user.getAuthorisation())
    .filter(auth -> !StringUtils.isEmpty(auth))
    .ifPresent(auth -> userDao.updateAuthorisation(id, auth));

Optional.ofNullable(user.getChapterProgress())
    .filter(cp -> !StringUtils.isEmpty(cp))
    .ifPresent(cp -> userDao.updateChapterProgress(id, cp));

我尝试注释掉每个可选项中的.filter行,但没有改变。

User Dao如下所示:

public interface UserDao {
    // ...
    int updateAuthorisation(UUID id, int authorisation);
    int updateChapterProgress(UUID id, int chapterProgress);
}

在UserDataAccessServcie中实现,如下所示:

public class UserDataAccessService implements UserDao {
    // ...
    @Override
    public int updateAuthorisation(UUID id, int authorisation) {
        String sql = "UPDATE users SET authorisation = ? WHERE id = ?";
        return jdbcTemplate.update(sql, authorisation, id);
    }

    @Override
    public int updateChapterProgress(UUID id, int chapterProgress) {
        String sql = "UPDATE users SET chapterprogress = ? WHERE id = ?";
        return jdbcTemplate.update(sql, chapterProgress, id);
    }
    // ...
}

如果还有其他问题,请随时提问。

英文:

Im am using Optionals in an update method that once ran will update thier respective postgres db column, the first few are strings and updating fine. The last two are INT's(Authorisation and ChapterProgress) and when i call the first of them to update its value it sets the other INT based optional to 0 and vice versa. The problem Code is the last two optionals below:

private final UserDao userDao;

//constructor
@Autowired
public UserService(@Qualifier("postgres1")UserDao userDao) {
	this.userDao= userDao;
}
//methods

public int addUser(UUID id,User user) {
	UUID newId = Optional.ofNullable(id)
            .orElse(UUID.randomUUID());
	return userDao.insertUser(newId,user);
}

public List<User> getAllUsers(){
	return userDao.selectAllUsers();
}

@GetMapping
public Optional<User> getUserById(UUID id){
	return userDao.selectUserByID(id);
}

public int deleteUser(UUID id) {
	return userDao.deleteUserById(id);
}

@PutMapping
public void updateUser(UUID id, User user) {

Optional.ofNullable(user.getSurname())
.filter(surname -> !StringUtils.isEmpty(surname))
.map(StringUtils::capitalize)
.ifPresent(surname -> userDao.updateSurname(id, surname));

Optional.ofNullable(user.getFirstname())
.filter(firstname -> !StringUtils.isEmpty(firstname))
.map(StringUtils::capitalize)
.ifPresent(firstname -> userDao.updateFirstname(id, firstname));

Optional.ofNullable(user.getUsername())
.filter(uname -> !StringUtils.isEmpty(uname))
.ifPresent(uname -> userDao.updateUsername(id, uname));

Optional.ofNullable(user.getPassword())
.filter(pw -> !StringUtils.isEmpty(pw))
.ifPresent(pw -> userDao.updatePassword(id, pw));

Optional.ofNullable(user.getAuthorisation())
.filter(auth -> !StringUtils.isEmpty(auth))
.ifPresent(auth -> userDao.updateAuthorisation(id, auth));

Optional.ofNullable(user.getChapterProgress())
.filter(cp -> !StringUtils.isEmpty(cp))
.ifPresent(cp -> userDao.updateChapterProgress(id, cp));

}

I have tried commenting out the .filter line on each and nothing changed.

User Dao looks like this.

public interface UserDao {

int insertUser(UUID id, User user);//good

default int insertUser(User user) {//good
	UUID id = UUID.randomUUID();
	return insertUser(id, user);
}

List<User> selectAllUsers();//good

Optional<User> selectUserByID(UUID id);//good

int deleteUserById(UUID id);//good

int updateUserById(UUID id, User newUser);//good

int updateSurname(UUID id, String surname);//good

int updateFirstname(UUID id, String firstname);//good

int updateUsername(UUID id, String username);//good

int updatePassword(UUID id, String password);//good

int updateAuthorisation(UUID id, int authorisation);//

int updateChapterProgress(UUID id, int chapterProgress);//

and is implemented in UserDataAccessServcie, like so:

private JdbcTemplate jdbcTemplate;

@Autowired
public UserDataAccessService(JdbcTemplate jdbcTemplate) {
	this.jdbcTemplate= jdbcTemplate;
}

@Override//create
public int insertUser(UUID id, User user) {
	String sql ="INSERT INTO users(id,surname,firstname,username,password,authorisation,chapterprogress) VALUES (?,?,?,?,?,?,?);";
    return jdbcTemplate.update(sql,id,
    		user.getSurname(),
    		user.getFirstname(),
    		user.getUsername(),
    		user.getPassword(),
    		user.getAuthorisation(),
    		user.getChapterProgress()
    );
}

@Override//read all
public List<User> selectAllUsers() {
final String sql= "SELECT * FROM users";
	return jdbcTemplate.query(sql, (resultSet,i)->{
		return new User(UUID.fromString(
				resultSet.getString("id")),
				resultSet.getString("surname"),
				resultSet.getString("firstName"),
				resultSet.getString("userName"),
				resultSet.getString("password"),
				resultSet.getInt("authorisation"),
				resultSet.getInt("chapterProgress"));
	});
}

@Override//read single
public Optional<User> selectUserByID(UUID id) {
	final String sql= "SELECT * FROM users WHERE id=?";
	User user= jdbcTemplate.queryForObject(sql, new Object[] {id}, (resultSet,i) ->{
		return new User(
				UUID.fromString(resultSet.getString("id")),
				resultSet.getString("surname"), ("firstName"), ("userName"), ("password"),
				resultSet.getInt("authorisation"),
				resultSet.getInt("chapterProgress"));
	});
	return Optional.ofNullable(user);
}

@Override//update//possibly to authorisation problem, consider changing 
		//to void to stop returning a '0'
public int updateUserById(UUID id, User newUser) {
	return 1;
}

@Override
public int updateSurname(UUID id, String surname) {
    String sql ="UPDATE users"
    		+ " SET surname = ? "
    		+ "WHERE id = ?";
    return jdbcTemplate.update(sql, surname, id);
}

@Override
public int updateFirstname(UUID id, String firstname) {
    String sql ="UPDATE users SET firstName = ? WHERE id = ?";
    return jdbcTemplate.update(sql, firstname, id);
}

@Override
public int updateUsername(UUID id, String username) {
    String sql = "UPDATE users SET username = ? WHERE id = ?";
    return jdbcTemplate.update(sql, username, id);
}

@Override
public int updatePassword(UUID id, String password) {
    String sql = "UPDATE users SET password = ? WHERE id = ?";
    return jdbcTemplate.update(sql, password, id);
}

@Override
public int updateAuthorisation(UUID id, int authorisation) {
    String sql = "UPDATE users SET authorisation = ? WHERE id = ?";
    return jdbcTemplate.update(sql, authorisation, id);
}

@Override
public int updateChapterProgress(UUID id, int chapterProgress) {
    String sql = "UPDATE users SET chapterprogress = ? WHERE id = ?";
    return jdbcTemplate.update(sql, chapterProgress, id);
}

@Override//delete
	public int deleteUserById(UUID id) {
		final String sql= "DELETE FROM users WHERE id=?";
		jdbcTemplate.update(sql, id);
		return 1;
	}

I have a feeling im forgetting/doing something simple but any help is very much appreciated

Thanks

答案1

得分: 1

原来问题出在两个获取方法的返回类型上。通过使用Integer作为数据类型并返回一个Integer而不是int,它允许数据库对其进行操作并存储它。

英文:

Turns out it was the return type of the two get methods stated. By using Integer as the data type and returning an Integer instead of an int it allows the db to manipulate it and store it.

huangapple
  • 本文由 发表于 2020年8月15日 01:19:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63417327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定