Retrieve account list for authenticated user

This commit is contained in:
2023-05-07 15:02:17 +02:00
parent f835e6837d
commit caee7b5d4b
6 changed files with 90 additions and 4 deletions

View File

@@ -0,0 +1,27 @@
package net.kapcake.bankingservice.config;
import org.h2.tools.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import java.sql.SQLException;
@Configuration
public class DatabaseConfig {
@Bean(initMethod = "start", destroyMethod = "stop")
@Profile("dev")
public Server h2Server() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
}
// @Bean
// public CommandLineRunner demoUsers(UserRepository userRepository, PasswordEncoder passwordEncoder) {
// return args -> {
// System.out.println(passwordEncoder.encode("user1Password"));
// System.out.println(passwordEncoder.encode("user2Password"));
// System.out.println(passwordEncoder.encode("user3Password"));
// System.out.println(passwordEncoder.encode("user4Password"));
// };
// }
}

View File

@@ -0,0 +1,24 @@
package net.kapcake.bankingservice.controllers;
import net.kapcake.bankingservice.domain.BankAccount;
import net.kapcake.bankingservice.services.AccountService;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class BankingServiceController {
private final AccountService accountService;
public BankingServiceController(AccountService accountService) {
this.accountService = accountService;
}
@GetMapping("/accounts")
public List<BankAccount> getAccounts(@AuthenticationPrincipal User user) {
return accountService.getAccounts(user);
}
}

View File

@@ -3,5 +3,8 @@ package net.kapcake.bankingservice.repositories;
import net.kapcake.bankingservice.domain.BankAccount;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface BankAccountRepository extends JpaRepository<BankAccount, Long> {
List<BankAccount> findAllByUsers_username(String username);
}

View File

@@ -3,5 +3,8 @@ package net.kapcake.bankingservice.repositories;
import net.kapcake.bankingservice.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);
}

View File

@@ -0,0 +1,21 @@
package net.kapcake.bankingservice.services;
import net.kapcake.bankingservice.domain.BankAccount;
import net.kapcake.bankingservice.repositories.BankAccountRepository;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class AccountService {
private final BankAccountRepository bankAccountRepository;
public AccountService(BankAccountRepository bankAccountRepository) {
this.bankAccountRepository = bankAccountRepository;
}
public List<BankAccount> getAccounts(User user) {
return bankAccountRepository.findAllByUsers_username(user.getUsername());
}
}

View File

@@ -1,5 +1,13 @@
INSERT INTO ADDRESS (id, street, number, postal_code, country)
values (1, 'street', 1, 9999, 'Luxembourg');
INSERT INTO BANKING_USER (ID, USERNAME, PASSWORD)
VALUES (1, 'user1', '{bcrypt}$2a$10$4dPs2u01F/UBJtQyKRCRLevJACUkDzSdD.4EFKkf0T0qllqtkxw5e');
INSERT INTO BANKING_USER (ID, USERNAME, PASSWORD)
VALUES (2, 'user2', '{bcrypt}$2a$10$EWrufyYshQUZ4A8ABWPqb.rGC.ZbASqCZiw1vgu68uUOB5t/qJpu6');
INSERT INTO BANKING_USER (ID, USERNAME, PASSWORD)
VALUES (3, 'user3', '{bcrypt}$2a$10$TEMplrWsmM7TA8HxTV8tG..4jxbfkOiq34cwg4wW3RXPRg6KMBdPm');
INSERT INTO BANKING_USER (ID, USERNAME, PASSWORD)
VALUES (4, 'user4', '{bcrypt}$2a$10$1wGifm5b1N6uBL6Zpxn5tegsFNSyTV6iux5MI9ANuP0G5cliBcLWa');
INSERT INTO BANKING_USER (id, username, password, address_id)
values (1, 'user1', 'test', 1);
INSERT INTO BANK_ACCOUNT (ID, ACCOUNT_NAME, ACCOUNT_NUMBER, STATUS)
VALUES (1, 'Personal', 'LU375050297422074000', 'ENABLED');
INSERT INTO BANK_ACCOUNT_USERS (BANK_ACCOUNT_ID, USERS_ID)
VALUES (1, 1);