Controller.java
@RestController
@RequestMapping(value = "/v2/customers/")
@Api(value = "account", description = "Operations pertaining to account operations")
public class AccountController {
private final AccountService accountService;
public AccountController(AccountService accountService) {
this.accountService = accountService;
}
@GetMapping(value = "accounts/{CIFs}")
public Response getAccountByCIF(@PathVariable String CIFs) {
return Response.ok().setData(accountService.findByCustomerNumberIn(CIFs));
}
}
AccountRepository.java
@Repository
public interface AccountRepository extends JpaRepository<AccountView, String> {
List<AccountView> getByCustomerNumber(String cif);
List<AccountView> getByCustomerNumberIn(List<String> cifList);
}
AccountService.java
public interface AccountService {
List<AccountDto> findByCustomerNumber(String cifList);
List<AccountDto> findByCustomerNumberIn(String cifList);
}
AccountServiceImpl.java
@Service
public class AccountServiceImpl implements AccountService {
private final AccountRepository accountRepository;
@Autowired
private AccountMapper accountMapper;
public AccountServiceImpl(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
@Override
public List<AccountDto> findByCustomerNumber(String cif) {
List<AccountView> account = accountRepository.getByCustomerNumber(cif);
System.out.println(account);
if (!account.isEmpty()) {
return accountMapper.toAccountDtoList(account).stream().collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}
@Override
public List<AccountDto> findByCustomerNumberIn(String cifList) {
List<String> cif = Arrays.asList(cifList.split(","));
List<AccountView> account = accountRepository.getByCustomerNumberIn(cif);
System.out.println(account);
if (!account.isEmpty()) {
return accountMapper.toAccountDtoList(account).stream().collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}
尽管数据库具有不同的记录,但我运行应用程序时
它一次又一次返回相同的值。
这是json的结果。对于查询的客户,数据库中有6条记录,但是端点返回与上面相同的6条记录。
{
"name": "ACDESC33811018409238803204",
"customer_number": "9238803",
"account_number": "33811018409238803204",
"book_ACC": "33811018409238803204",
"account_branch": {
"branch_number": "204",
"branch_name": "MERKEZI FILIAL"
},
"balance": 88.45,
"currency": "USD",
"status": "NORM",
"class": "33811",
"open_date": "2006-05-26",
"location": null,
"ibanrgd": "AZ16IBAZ33811018409238803204",
"expcat": "GENERAL",
"user_defined_fields": [],
"type": "S",
"number": "2388030001",
"start_tod_limit_date": null,
"end_tod_limit_date": null,
"tod_limit_Interest": null,
"tod_limit_amount": null,
"tod_limit_total_amount": null
},
AccountMapper.java
package az.iba.ms.account.dto.mapper;
import az.iba.ms.account.dto.model.AccountBranchDto;
import az.iba.ms.account.dto.model.AccountDto;
import az.iba.ms.account.dto.model.FieldDto;
import az.iba.ms.account.dto.model.UserDefinedFieldsDto;
import az.iba.ms.account.entity.AccountView;
import org.springframework.stereotype.Component;
import java.sql.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class AccountMapper {
public List<AccountDto> toAccountDtoList(List<AccountView> accountViews) {
return accountViews.stream()
.map(accountView -> toAccountDto(accountView))
.collect(Collectors.toList());
}
public static AccountDto toAccountDto(AccountView account) {
return new AccountDto()
.setName(account.getName())
.setCustomerNumber(account.getCustomerNumber())
.setAccountNumber(account.getAccountNumber())
.setBookACC(account.getBookACC())
.setAccountBranch(new AccountBranchDto( account.getBranchCode(), account.getBranchName()))
.setBalance(account.getBalance())
.setCurrency(account.getCurrency())
.setStatus(account.getStatus())
.set_class(account.get_class())
.setOpenDate(account.getOpenDate())
.setLocation(account.getLocation())
.setIbanRgd(account.getIbanRgd())
.setExpcat(account.getExpcat())
.setUser_defined_fields(null ) // UserDefinedFieldsDto[] = new FieldDto(account.getFieldValue(),account.getFieldName() ))
.setAccType(account.getAccType())
.setNumber(account.getNumber())
.setTodLimitStartDate(account.getTodLimitStartDate())
.setTodLimitEndDate(account.getTodLimitEndDate())
.setTodLimitInterest(account.getTodLimitInterest())
.setTodLimitAmount(account.getTodLimitAmount())
.setTodLimitTotalAmount(account.getTodLimitTotalAmount());
}
}