View Javadoc
1   /*
2    *    Copyright 2010-2026 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    /** The account mapper. */
34    private final AccountMapper accountMapper;
35  
36    /**
37     * Instantiates a new account service.
38     *
39     * @param accountMapper
40     *          the account mapper
41     */
42    public AccountService(AccountMapper accountMapper) {
43      this.accountMapper = accountMapper;
44    }
45  
46    /**
47     * Get account.
48     *
49     * @param username
50     *          the username
51     *
52     * @return the account
53     */
54    public Account getAccount(String username) {
55      return accountMapper.getAccountByUsername(username);
56    }
57  
58    /**
59     * Get account.
60     *
61     * @param username
62     *          the username
63     * @param password
64     *          the password
65     *
66     * @return the account
67     */
68    public Account getAccount(String username, String password) {
69      return accountMapper.getAccountByUsernameAndPassword(username, password);
70    }
71  
72    /**
73     * Insert account.
74     *
75     * @param account
76     *          the account
77     */
78    @Transactional
79    public void insertAccount(Account account) {
80      accountMapper.insertAccount(account);
81      accountMapper.insertProfile(account);
82      accountMapper.insertSignon(account);
83    }
84  
85    /**
86     * Update account.
87     *
88     * @param account
89     *          the account
90     */
91    @Transactional
92    public void updateAccount(Account account) {
93      accountMapper.updateAccount(account);
94      accountMapper.updateProfile(account);
95  
96      Optional.ofNullable(account.getPassword()).filter(password -> password.length() > 0)
97          .ifPresent(password -> accountMapper.updateSignon(account));
98    }
99  
100 }