diff options
author | Vitaly Takmazov | 2023-01-15 09:47:06 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-01-15 09:47:06 +0300 |
commit | 5c11e3e85b70a3789c3e330209122723f1b38121 (patch) | |
tree | 4f8e432a2795e9dc91a0092c38e5dc27c8c7e7c7 /src/main/java/com/juick/service/UserServiceImpl.java | |
parent | 9cc0391c14f64a5b6e0835f1ed07f56304372cf5 (diff) |
PushQueriesService -> UserService
Diffstat (limited to 'src/main/java/com/juick/service/UserServiceImpl.java')
-rw-r--r-- | src/main/java/com/juick/service/UserServiceImpl.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java index 4bc28afa..6114bf78 100644 --- a/src/main/java/com/juick/service/UserServiceImpl.java +++ b/src/main/java/com/juick/service/UserServiceImpl.java @@ -928,4 +928,57 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { public boolean canDeleteTelegramUser(User user) { return getEmails(user).size() > 0 || getFbCrossPostStatus(user.getUid()).isConnected() || getVkTokens(user.getUid()).isPresent(); } + private class TokenMapper implements RowMapper<ExternalToken> { + + @Override + public ExternalToken mapRow(ResultSet rs, int rowNum) throws SQLException { + return new ExternalToken( + null, + rs.getString("service_type"), + rs.getString("regid"), + null + ); + } + } + + @Transactional(readOnly = true) + @Override + public Collection<ExternalToken> getToken(final int uid, final String serviceType) { + return getJdbcTemplate().query( + "SELECT regid, service_type FROM user_services WHERE user_id=? AND service_type=?", + new TokenMapper(), + uid, serviceType); + } + + @Transactional(readOnly = true) + @Override + public Collection<ExternalToken> getTokens(final Collection<Integer> uids) { + if (CollectionUtils.isEmpty(uids)) + return Collections.emptyList(); + return getNamedParameterJdbcTemplate().query( + """ + SELECT regid, service_type FROM user_services INNER JOIN users + ON (users.id = user_services.user_id) WHERE users.id IN (:ids)""", + new MapSqlParameterSource("ids", uids), + new TokenMapper()); + } + + @Transactional + @Override + public boolean addToken(final Integer uid, final String serviceType, final String token) { + try { + return getJdbcTemplate().update( + "INSERT INTO user_services(user_id, regid, service_type) VALUES (?, ?, ?)", + uid, token, serviceType) > 0; + } catch (DataIntegrityViolationException e) { + return false; + } + } + + @Transactional + @Override + public boolean deleteToken(final String serviceType, final String token) { + return getJdbcTemplate().update("DELETE FROM user_services WHERE regid=? AND service_type=?", token, serviceType) > 0; + } } + |