aboutsummaryrefslogtreecommitdiff
path: root/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java
blob: c42641df7527ac0633098428657386b3929b4d26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright (C) 2008-2017, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick.service;

import org.apache.commons.lang3.StringUtils;
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;

/**
 * Created by vitalyster on 09.12.2016.
 */
@Repository
@Transactional
public class EmailServiceImpl extends BaseJdbcService implements EmailService {
    @Inject
    public EmailServiceImpl(JdbcTemplate jdbcTemplate) {
        super(jdbcTemplate, null);
    }

    @Override
    public boolean verifyAddressByCode(Integer userId, String code) {
        try {
            String address = getJdbcTemplate().queryForObject("SELECT account FROM auth WHERE user_id=? AND protocol='email' AND authcode=?",
                    String.class, userId, code);
            getJdbcTemplate().update("INSERT INTO emails(user_id,email) VALUES (?,?)", userId, address);
            getJdbcTemplate().update("DELETE FROM auth WHERE user_id=? AND authcode=?", userId, code);
        } catch (EmptyResultDataAccessException e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean addVerificationCode(Integer userId, String account, String code) {
        return getJdbcTemplate().update("INSERT INTO auth(user_id,protocol,account,authcode) VALUES (?,'email',?,?)",
                userId, account, code) > 0;
    }

    @Override
    public boolean deleteEmail(Integer userId, String account) {
        return getJdbcTemplate().update("DELETE FROM emails WHERE user_id=? AND email=?", userId, account) > 0;
    }

    @Override
    public boolean setSubscriptionHour(Integer userId, String account, String hour) {
        getJdbcTemplate().update("UPDATE emails SET subscr_hour=NULL WHERE user_id=?", userId);
        return StringUtils.isNotEmpty(account) && getJdbcTemplate().update(
                "UPDATE emails SET subscr_hour=? WHERE user_id=? AND email=?", hour, userId, account) > 0;
    }
}