aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/service/UserServiceImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/service/UserServiceImpl.java')
-rw-r--r--src/main/java/com/juick/service/UserServiceImpl.java53
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;
+ }
}
+