View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.jpetstore.service;
17  
18  import java.util.Optional;
19  
20  import org.mybatis.jpetstore.domain.Account;
21  import org.mybatis.jpetstore.mapper.AccountMapper;
22  import org.springframework.stereotype.Service;
23  import org.springframework.transaction.annotation.Transactional;
24  
25  /**
26   * The Class AccountService.
27   *
28   * @author Eduardo Macarron
29   */
30  @Service
31  public class AccountService {
32  
33    private final AccountMapper accountMapper;
34  
35    public AccountService(AccountMapper accountMapper) {
36      this.accountMapper = accountMapper;
37    }
38  
39    public Account getAccount(String username) {
40      return accountMapper.getAccountByUsername(username);
41    }
42  
43    public Account getAccount(String username, String password) {
44      return accountMapper.getAccountByUsernameAndPassword(username, password);
45    }
46  
47    /**
48     * Insert account.
49     *
50     * @param account
51     *          the account
52     */
53    @Transactional
54    public void insertAccount(Account account) {
55      accountMapper.insertAccount(account);
56      accountMapper.insertProfile(account);
57      accountMapper.insertSignon(account);
58    }
59  
60    /**
61     * Update account.
62     *
63     * @param account
64     *          the account
65     */
66    @Transactional
67    public void updateAccount(Account account) {
68      accountMapper.updateAccount(account);
69      accountMapper.updateProfile(account);
70  
71      Optional.ofNullable(account.getPassword()).filter(password -> password.length() > 0)
72          .ifPresent(password -> accountMapper.updateSignon(account));
73    }
74  
75  }