From 590c650f8ac3478b31fd3fd3382c9b35284844b7 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sun, 13 Nov 2016 23:45:32 +0700 Subject: jdbc service (repository) layer is added --- .../main/java/com/juick/service/AdsService.java | 11 +++ .../java/com/juick/service/AdsServiceImpl.java | 43 +++++++++++ .../main/java/com/juick/service/BaseJdbcDao.java | 42 ++++++++++ .../java/com/juick/service/CrosspostService.java | 24 ++++++ .../com/juick/service/CrosspostServiceImpl.java | 89 ++++++++++++++++++++++ .../java/com/juick/service/MessagesService.java | 7 ++ .../com/juick/service/MessagesServiceImpl.java | 10 +++ .../java/com/juick/service/PMQueriesService.java | 7 ++ .../com/juick/service/PMQueriesServiceImpl.java | 10 +++ .../com/juick/service/PrivacyQueriesService.java | 7 ++ .../juick/service/PrivacyQueriesServiceImpl.java | 10 +++ .../java/com/juick/service/PushQueriesService.java | 7 ++ .../com/juick/service/PushQueriesServiceImpl.java | 10 +++ .../java/com/juick/service/ShowQueriesService.java | 7 ++ .../com/juick/service/ShowQueriesServiceImpl.java | 10 +++ .../com/juick/service/SubscriptionService.java | 7 ++ .../com/juick/service/SubscriptionServiceImpl.java | 10 +++ .../main/java/com/juick/service/TagService.java | 7 ++ .../java/com/juick/service/TagServiceImpl.java | 10 +++ .../main/java/com/juick/service/UserService.java | 7 ++ .../java/com/juick/service/UserServiceImpl.java | 10 +++ 21 files changed, 345 insertions(+) create mode 100644 server-core/src/main/java/com/juick/service/AdsService.java create mode 100644 server-core/src/main/java/com/juick/service/AdsServiceImpl.java create mode 100644 server-core/src/main/java/com/juick/service/BaseJdbcDao.java create mode 100644 server-core/src/main/java/com/juick/service/CrosspostService.java create mode 100644 server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java create mode 100644 server-core/src/main/java/com/juick/service/MessagesService.java create mode 100644 server-core/src/main/java/com/juick/service/MessagesServiceImpl.java create mode 100644 server-core/src/main/java/com/juick/service/PMQueriesService.java create mode 100644 server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java create mode 100644 server-core/src/main/java/com/juick/service/PrivacyQueriesService.java create mode 100644 server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java create mode 100644 server-core/src/main/java/com/juick/service/PushQueriesService.java create mode 100644 server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java create mode 100644 server-core/src/main/java/com/juick/service/ShowQueriesService.java create mode 100644 server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java create mode 100644 server-core/src/main/java/com/juick/service/SubscriptionService.java create mode 100644 server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java create mode 100644 server-core/src/main/java/com/juick/service/TagService.java create mode 100644 server-core/src/main/java/com/juick/service/TagServiceImpl.java create mode 100644 server-core/src/main/java/com/juick/service/UserService.java create mode 100644 server-core/src/main/java/com/juick/service/UserServiceImpl.java (limited to 'server-core/src/main/java/com/juick/service') diff --git a/server-core/src/main/java/com/juick/service/AdsService.java b/server-core/src/main/java/com/juick/service/AdsService.java new file mode 100644 index 00000000..f8a0f5cf --- /dev/null +++ b/server-core/src/main/java/com/juick/service/AdsService.java @@ -0,0 +1,11 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface AdsService { + + int getAdMid(int uid); + + int logAdMid(int uid, int mid); +} diff --git a/server-core/src/main/java/com/juick/service/AdsServiceImpl.java b/server-core/src/main/java/com/juick/service/AdsServiceImpl.java new file mode 100644 index 00000000..32effd21 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/AdsServiceImpl.java @@ -0,0 +1,43 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class AdsServiceImpl extends BaseJdbcDao implements AdsService { + + @Transactional(readOnly = true) + @Override + public int getAdMid(final int uid) { + List list; + + if (uid > 0) + list = getJdbcTemplate().queryForList( + "SELECT message_id FROM ads_messages " + + "WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id = ? " + + "AND ts > UNIX_TIMESTAMP() - 60*60*24 GROUP BY message_id HAVING COUNT(*) > 2) ORDER BY RAND() LIMIT 1", + Integer.class, + uid); + + else + list = getJdbcTemplate().queryForList( + "SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", + Integer.class); + + return list.isEmpty() ? + 0 : list.get(0); + } + + @Transactional + @Override + public int logAdMid(final int uid, final int mid) { + return getJdbcTemplate().update( + "INSERT INTO ads_messages_log(user_id, message_id, ts) VALUES (?, ?, UNIX_TIMESTAMP())", + uid, mid); + } +} diff --git a/server-core/src/main/java/com/juick/service/BaseJdbcDao.java b/server-core/src/main/java/com/juick/service/BaseJdbcDao.java new file mode 100644 index 00000000..948e6e6b --- /dev/null +++ b/server-core/src/main/java/com/juick/service/BaseJdbcDao.java @@ -0,0 +1,42 @@ +package com.juick.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.util.Assert; + +import javax.inject.Inject; + +/** + * Created by aalexeev on 11/13/16. + */ +public class BaseJdbcDao implements InitializingBean { + protected final Logger logger = LoggerFactory.getLogger(getClass()); + + private JdbcTemplate jdbcTemplate; + private NamedParameterJdbcTemplate namedParameterJdbcTemplate; + + protected JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + protected NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { + return namedParameterJdbcTemplate; + } + + @Override + public void afterPropertiesSet() throws Exception { + logger.debug("Checking required beans"); + + Assert.notNull(jdbcTemplate); + Assert.notNull(namedParameterJdbcTemplate); + } + + @Inject + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate); + } +} diff --git a/server-core/src/main/java/com/juick/service/CrosspostService.java b/server-core/src/main/java/com/juick/service/CrosspostService.java new file mode 100644 index 00000000..88821b60 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/CrosspostService.java @@ -0,0 +1,24 @@ +package com.juick.service; + +import com.juick.server.helpers.ApplicationStatus; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.Optional; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface CrosspostService { + + Optional> getTwitterTokens(int uid); + + Optional getFacebookToken(int uid); + + ApplicationStatus getFbCrossPostStatus(int uid); + + String getTwitterName(int uid); + + String getTelegramName(int uid); + + Optional> getVkTokens(int uid); +} diff --git a/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java b/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java new file mode 100644 index 00000000..6c6f7442 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/CrosspostServiceImpl.java @@ -0,0 +1,89 @@ +package com.juick.service; + +import com.juick.server.helpers.ApplicationStatus; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class CrosspostServiceImpl extends BaseJdbcDao implements CrosspostService { + + @Transactional(readOnly = true) + @Override + public Optional> getTwitterTokens(final int uid) { + List>> list = getJdbcTemplate().query( + "SELECT access_token,access_token_secret FROM twitter WHERE user_id = ? AND crosspost = 1", + (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), + uid); + + return list.isEmpty() ? + Optional.empty() : list.get(0); + } + + @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, + uid); + return list.isEmpty() ? + Optional.empty() : Optional.of(list.get(0)); + } + + @Override + public ApplicationStatus getFbCrossPostStatus(final int uid) { + List list = getJdbcTemplate().query( + "SELECT 1, crosspost FROM facebook WHERE user_id = ? LIMIT 1", + (rs, num) -> { + ApplicationStatus status = new ApplicationStatus(); + + status.setConnected(rs.getInt(1) > 0); + status.setCrosspostEnabled(rs.getBoolean(2)); + + return status; + }, + uid); + + return list.isEmpty() ? + new ApplicationStatus() : list.get(0); + } + + @Override + public String getTwitterName(final int uid) { + List list = getJdbcTemplate().queryForList( + "SELECT uname FROM twitter WHERE user_id = ?", + String.class, + uid); + + return list.isEmpty() ? + "" : list.get(0); + } + + @Override + public String getTelegramName(final int uid) { + List list = getJdbcTemplate().queryForList( + "SELECT tg_name FROM telegram WHERE user_id = ?", + String.class, + uid); + + return list.isEmpty() ? + "" : list.get(0); + } + + @Override + public Optional> getVkTokens(final int uid) { + List>> list = getJdbcTemplate().query( + "SELECT vk_id, access_token FROM vk WHERE user_id = ? AND crosspost = 1", + (rs, num) -> Optional.of(Pair.of(rs.getString(1), rs.getString(2))), + uid); + + return list.isEmpty() ? + Optional.empty() : list.get(0); + } +} diff --git a/server-core/src/main/java/com/juick/service/MessagesService.java b/server-core/src/main/java/com/juick/service/MessagesService.java new file mode 100644 index 00000000..23ccf8e9 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/MessagesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface MessagesService { +} diff --git a/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java b/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java new file mode 100644 index 00000000..1955d9f9 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/MessagesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class MessagesServiceImpl extends BaseJdbcDao implements MessagesService { +} diff --git a/server-core/src/main/java/com/juick/service/PMQueriesService.java b/server-core/src/main/java/com/juick/service/PMQueriesService.java new file mode 100644 index 00000000..5e2e2b75 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PMQueriesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface PMQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java new file mode 100644 index 00000000..4c8f4ae9 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PMQueriesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class PMQueriesServiceImpl extends BaseJdbcDao implements PushQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java b/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java new file mode 100644 index 00000000..29116081 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PrivacyQueriesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface PrivacyQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java new file mode 100644 index 00000000..d4e43728 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PrivacyQueriesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class PrivacyQueriesServiceImpl extends BaseJdbcDao implements PrivacyQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PushQueriesService.java b/server-core/src/main/java/com/juick/service/PushQueriesService.java new file mode 100644 index 00000000..dbb5ac06 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PushQueriesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface PushQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java new file mode 100644 index 00000000..0381f913 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/PushQueriesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class PushQueriesServiceImpl extends BaseJdbcDao implements PushQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/ShowQueriesService.java b/server-core/src/main/java/com/juick/service/ShowQueriesService.java new file mode 100644 index 00000000..2d42988d --- /dev/null +++ b/server-core/src/main/java/com/juick/service/ShowQueriesService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface ShowQueriesService { +} diff --git a/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java b/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java new file mode 100644 index 00000000..d3470c75 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/ShowQueriesServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class ShowQueriesServiceImpl extends BaseJdbcDao implements SubscriptionService { +} diff --git a/server-core/src/main/java/com/juick/service/SubscriptionService.java b/server-core/src/main/java/com/juick/service/SubscriptionService.java new file mode 100644 index 00000000..0ed3f55a --- /dev/null +++ b/server-core/src/main/java/com/juick/service/SubscriptionService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface SubscriptionService { +} diff --git a/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java b/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java new file mode 100644 index 00000000..811f5af0 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/SubscriptionServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class SubscriptionServiceImpl extends BaseJdbcDao implements SubscriptionService { +} diff --git a/server-core/src/main/java/com/juick/service/TagService.java b/server-core/src/main/java/com/juick/service/TagService.java new file mode 100644 index 00000000..1083d663 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/TagService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface TagService { +} diff --git a/server-core/src/main/java/com/juick/service/TagServiceImpl.java b/server-core/src/main/java/com/juick/service/TagServiceImpl.java new file mode 100644 index 00000000..9649ce74 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/TagServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class TagServiceImpl extends BaseJdbcDao implements TagService { +} diff --git a/server-core/src/main/java/com/juick/service/UserService.java b/server-core/src/main/java/com/juick/service/UserService.java new file mode 100644 index 00000000..ecf8ad07 --- /dev/null +++ b/server-core/src/main/java/com/juick/service/UserService.java @@ -0,0 +1,7 @@ +package com.juick.service; + +/** + * Created by aalexeev on 11/13/16. + */ +public interface UserService { +} diff --git a/server-core/src/main/java/com/juick/service/UserServiceImpl.java b/server-core/src/main/java/com/juick/service/UserServiceImpl.java new file mode 100644 index 00000000..93dc90dc --- /dev/null +++ b/server-core/src/main/java/com/juick/service/UserServiceImpl.java @@ -0,0 +1,10 @@ +package com.juick.service; + +import org.springframework.stereotype.Repository; + +/** + * Created by aalexeev on 11/13/16. + */ +@Repository +public class UserServiceImpl extends BaseJdbcDao implements UserService { +} -- cgit v1.2.3