From 7b727f01e3232f98a89fc8246c0e6039b478f51e Mon Sep 17 00:00:00 2001 From: amechkarov Date: Mon, 18 Mar 2024 21:19:59 +0200 Subject: [PATCH] Add admin permissions to transactions and refactor styling in users page --- .../controllers/mvc/UserMvcController.java | 4 ++-- .../controllers/rest/WalletRestController.java | 1 - .../repositories/TransactionRepositoryImpl.java | 13 +++++++++---- .../resources/templates/TransactionsView.html | 4 ++-- src/main/resources/templates/UsersView.html | 16 ++++++++-------- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/telerikacademy/web/virtualwallet/controllers/mvc/UserMvcController.java b/src/main/java/com/telerikacademy/web/virtualwallet/controllers/mvc/UserMvcController.java index 5ca8257..1eda5c0 100644 --- a/src/main/java/com/telerikacademy/web/virtualwallet/controllers/mvc/UserMvcController.java +++ b/src/main/java/com/telerikacademy/web/virtualwallet/controllers/mvc/UserMvcController.java @@ -198,14 +198,14 @@ public String handleChangeProfilePhoto(@PathVariable String username, Model mode @GetMapping("/transactions") public String get(@ModelAttribute("transactionFilterOptions") TransactionFilterDto filterDto, - @RequestParam(defaultValue = "0") int page,@RequestParam(defaultValue = "10") int pageSize, Model model, HttpSession session,BindingResult errors) { + @RequestParam(defaultValue = "0") int page,@RequestParam(defaultValue = "10") int pageSize, Model model, HttpSession session) { TransactionFilterOptions transactionFilterOptions = new TransactionFilterOptions(filterDto.getDate() ,filterDto.getSender(),filterDto.getReceiver(),filterDto.getDirection(), filterDto.getSortBy(),filterDto.getSortOrder()); try { - User user = userService.getById(1); + User user = authenticationHelper.tryGetCurrentUser(session); Pageable pageable = PageRequest.of(page, pageSize); Page transactions = transactionService.getAll(user, transactionFilterOptions,pageable); model.addAttribute("transactionFilterOptions", filterDto); diff --git a/src/main/java/com/telerikacademy/web/virtualwallet/controllers/rest/WalletRestController.java b/src/main/java/com/telerikacademy/web/virtualwallet/controllers/rest/WalletRestController.java index 1d39c69..5bb590b 100644 --- a/src/main/java/com/telerikacademy/web/virtualwallet/controllers/rest/WalletRestController.java +++ b/src/main/java/com/telerikacademy/web/virtualwallet/controllers/rest/WalletRestController.java @@ -98,7 +98,6 @@ public Transaction createTransaction(@RequestHeader(name = "Authentication") try { User sender = authenticationHelper.tryGetUser(authentication); Transaction transaction = transactionMapper.fromDto(sender, transactionDto); - transactionService.create(transaction,sender.getWallet(),transaction.getReceiver().getWallet()); return transaction; } catch (AuthenticationException | AuthorizationException e){ diff --git a/src/main/java/com/telerikacademy/web/virtualwallet/repositories/TransactionRepositoryImpl.java b/src/main/java/com/telerikacademy/web/virtualwallet/repositories/TransactionRepositoryImpl.java index 6f158d2..75f854c 100644 --- a/src/main/java/com/telerikacademy/web/virtualwallet/repositories/TransactionRepositoryImpl.java +++ b/src/main/java/com/telerikacademy/web/virtualwallet/repositories/TransactionRepositoryImpl.java @@ -7,6 +7,7 @@ import com.telerikacademy.web.virtualwallet.models.User; import com.telerikacademy.web.virtualwallet.repositories.contracts.TransactionRepository; import com.telerikacademy.web.virtualwallet.repositories.contracts.UserRepository; +import com.telerikacademy.web.virtualwallet.services.contracts.UserService; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; @@ -28,10 +29,13 @@ public class TransactionRepositoryImpl extends AbstractCRUDRepository implements TransactionRepository { private final UserRepository userRepository; + + private final UserService userService; @Autowired - public TransactionRepositoryImpl(SessionFactory sessionFactory, UserRepository userRepository) { + public TransactionRepositoryImpl(SessionFactory sessionFactory, UserRepository userRepository, UserService userService) { super(Transaction.class,sessionFactory); this.userRepository = userRepository; + this.userService = userService; } @Override @@ -41,9 +45,10 @@ public Page filterAndSort(User user, TransactionFilterOptions filte Map params = new HashMap<>(); StringBuilder queryString = new StringBuilder("from Transaction "); - - filters.add("(sender = :user OR receiver = :user)"); - params.put("user", user); + if(!userService.isAdmin(user)) { + filters.add("(sender = :user OR receiver = :user)"); + params.put("user", user); + } filterOptions.getReceiver().ifPresent(value -> { if(value.isEmpty()||user.getUsername().equals(value)){ diff --git a/src/main/resources/templates/TransactionsView.html b/src/main/resources/templates/TransactionsView.html index 2397582..085309a 100644 --- a/src/main/resources/templates/TransactionsView.html +++ b/src/main/resources/templates/TransactionsView.html @@ -48,7 +48,7 @@

Filter Opti
Sender
Receiver
Amount
-
Direction
+
Direction
Date
@@ -70,7 +70,7 @@

Filter Opti

-
+

diff --git a/src/main/resources/templates/UsersView.html b/src/main/resources/templates/UsersView.html index 14c23c7..f20eda3 100644 --- a/src/main/resources/templates/UsersView.html +++ b/src/main/resources/templates/UsersView.html @@ -6,9 +6,9 @@
@@ -16,20 +16,20 @@

-
+
Username
Email
Phone