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