From bf5f2142d73a5745124d03f6b6f06c59873e47de Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 27 Dec 2017 16:23:28 +0300 Subject: Messenger bot --- .../com/juick/service/CrosspostServiceImpl.java | 10 +-- .../com/juick/service/MessengerServiceImpl.java | 77 ++++++++++++++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 juick-server-jdbc/src/main/java/com/juick/service/MessengerServiceImpl.java (limited to 'juick-server-jdbc') diff --git a/juick-server-jdbc/src/main/java/com/juick/service/CrosspostServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/CrosspostServiceImpl.java index dec8db5b..fb9ef167 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/CrosspostServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/CrosspostServiceImpl.java @@ -63,13 +63,13 @@ public class CrosspostServiceImpl extends BaseJdbcService implements CrosspostSe @Transactional(readOnly = true) @Override - public Optional getFacebookToken(final int uid) { - List list = getJdbcTemplate().queryForList( - "SELECT access_token FROM facebook WHERE user_id = ? AND access_token IS NOT NULL AND crosspost = 1", - String.class, + public Optional> getFacebookTokens(final int uid) { + List>> list = getJdbcTemplate().query( + "SELECT fb_id, access_token FROM facebook WHERE user_id = ? AND access_token IS NOT NULL AND crosspost = 1", + (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), uid); return list.isEmpty() ? - Optional.empty() : Optional.of(list.get(0)); + Optional.empty() : list.get(0); } @Transactional(readOnly = true) diff --git a/juick-server-jdbc/src/main/java/com/juick/service/MessengerServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/MessengerServiceImpl.java new file mode 100644 index 00000000..fe7165b3 --- /dev/null +++ b/juick-server-jdbc/src/main/java/com/juick/service/MessengerServiceImpl.java @@ -0,0 +1,77 @@ +package com.juick.service; + +import com.juick.User; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import javax.inject.Inject; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +@Repository +public class MessengerServiceImpl extends BaseJdbcService implements MessengerService { + @Inject + protected MessengerServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + + @Transactional(readOnly = true) + @Override + public Integer getUserId(String senderId) { + List list = getJdbcTemplate().queryForList( + "SELECT id FROM users INNER JOIN messenger " + + "ON messenger.user_id = users.id WHERE messenger.sender_id=?", Integer.class, senderId); + + return list.isEmpty() ? 0 : list.get(0); + } + @Transactional(readOnly = true) + @Override + public Optional getSenderId(User user) { + List list = getJdbcTemplate().queryForList( + "SELECT sender_id FROM messenger " + + "WHERE user_id=?", String.class, user.getUid()); + + return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)); + } + + @Transactional + @Override + public boolean createMessengerUser(String senderId, String displayName) { + return getJdbcTemplate().update( + "INSERT INTO messenger(sender_id, display_name, loginhash) VALUES(?,?,?)", + senderId, displayName, UUID.randomUUID().toString()) > 0; + } + @Transactional(readOnly = true) + @Override + public String getDisplayName(String hash) { + try { + return getJdbcTemplate().queryForObject("SELECT display_name FROM messenger WHERE loginhash=?", String.class, hash); + } catch (EmptyResultDataAccessException e) { + return null; + } + } + @Transactional + @Override + public String getSignUpHash(final String senderId, final String username) { + List list = getJdbcTemplate().queryForList( + "SELECT loginhash FROM messenger WHERE sender_id = ? AND user_id IS NULL", + String.class, + senderId); + + if (list.isEmpty()) { + String hash = UUID.randomUUID().toString(); + getJdbcTemplate().update( + "INSERT INTO messenger(sender_id, loginhash, display_name) VALUES (?, ?, ?)", senderId, hash, username); + return hash; + } + return list.get(0); + } + @Transactional + @Override + public boolean linkMessengerUser(String hash, int uid) { + return getJdbcTemplate().update("UPDATE messenger SET user_id=?, loginhash=NULL WHERE loginhash=?", uid, hash) > 0; + } +} -- cgit v1.2.3