From 8a0fbbd35c13054a947ea0d27ca117542bc452b9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 18 May 2018 15:33:38 +0300 Subject: JPA WIP --- .../java/com/juick/service/EmailServiceImpl.java | 35 +++++++++++----------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'juick-server/src/main/java/com/juick/service/EmailServiceImpl.java') diff --git a/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java b/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java index 0cccc915..47d1efe5 100644 --- a/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java @@ -17,6 +17,7 @@ package com.juick.service; +import com.juick.User; import org.apache.commons.lang3.StringUtils; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -35,12 +36,12 @@ import java.util.List; public class EmailServiceImpl extends BaseJdbcService implements EmailService { @Override - public boolean verifyAddressByCode(Integer userId, String code) { + public boolean verifyAddressByCode(User user, String code) { try { String address = getJdbcTemplate().queryForObject("SELECT account FROM auth WHERE user_id=? AND protocol='email' AND authcode=?", - String.class, userId, code); - addEmail(userId, address); - getJdbcTemplate().update("DELETE FROM auth WHERE user_id=? AND authcode=?", userId, code); + String.class, user.getUid(), code); + addEmail(user, address); + getJdbcTemplate().update("DELETE FROM auth WHERE user_id=? AND authcode=?", user.getUid(), code); } catch (EmptyResultDataAccessException e) { return false; } @@ -48,45 +49,45 @@ public class EmailServiceImpl extends BaseJdbcService implements EmailService { } @Override - public boolean addVerificationCode(Integer userId, String account, String code) { + public boolean addVerificationCode(User user, String account, String code) { return getJdbcTemplate().update("INSERT INTO auth(user_id,protocol,account,authcode) VALUES (?,'email',?,?)", - userId, account, code) > 0; + user.getUid(), account, code) > 0; } @Override - public boolean addEmail(Integer userId, String email) { - return getJdbcTemplate().update("INSERT INTO emails(user_id,email) VALUES (?,?)", userId, email) > 0; + public boolean addEmail(User user, String email) { + return getJdbcTemplate().update("INSERT INTO emails(user_id,email) VALUES (?,?)", user.getUid(), email) > 0; } @Override - public boolean deleteEmail(Integer userId, String account) { + public boolean deleteEmail(User user, String account) { return getNamedParameterJdbcTemplate().update("DELETE FROM emails " + "WHERE (SELECT COUNT(*) cnt FROM (select user_id, email FROM emails e) c WHERE user_id=:uid) > 1 " + "AND user_id=:uid AND email=:email", new MapSqlParameterSource() - .addValue("uid", userId) + .addValue("uid", user.getUid()) .addValue("email", account)) > 0; } @Transactional(readOnly = true) @Override - public String getNotificationsEmail(Integer userId) { + public String getNotificationsEmail(User user) { List list = getJdbcTemplate().queryForList( - "SELECT email FROM emails WHERE user_id=? AND subscr_hour IS NOT NULL", String.class, userId); + "SELECT email FROM emails WHERE user_id=? AND subscr_hour IS NOT NULL", String.class, user.getUid()); return list.isEmpty() ? StringUtils.EMPTY : list.get(0); } @Override - public boolean setNotificationsEmail(Integer userId, String account) { - getJdbcTemplate().update("UPDATE emails SET subscr_hour=NULL WHERE user_id=?", userId); + public boolean setNotificationsEmail(User user, String account) { + getJdbcTemplate().update("UPDATE emails SET subscr_hour=NULL WHERE user_id=?", user.getUid()); return StringUtils.isNotEmpty(account) && getJdbcTemplate().update( - "UPDATE emails SET subscr_hour=1 WHERE user_id=? AND email=?", userId, account) > 0; + "UPDATE emails SET subscr_hour=1 WHERE user_id=? AND email=?", user.getUid(), account) > 0; } @Transactional(readOnly = true) @Override - public List getEmails(Integer userId, boolean active) { + public List getEmails(User user, boolean active) { return getJdbcTemplate().queryForList("SELECT email FROM emails WHERE user_id=? " + - (active ? "AND subscr_hour IS NOT NULL" : ""), String.class, userId); + (active ? "AND subscr_hour IS NOT NULL" : ""), String.class, user.getUid()); } } -- cgit v1.2.3