Skip to content

Commit

Permalink
Merge pull request #86 from TeamSevenWeb/change_currency_added
Browse files Browse the repository at this point in the history
change currency option added to wallets views
  • Loading branch information
GeorgiIliev3 authored Mar 19, 2024
2 parents f3c468e + d4001a2 commit 7f22f1f
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,11 @@ public String requestURI(final HttpServletRequest request) {
public String showAllUsers(Model model, HttpSession session) {
try {
User user = authenticationHelper.tryGetCurrentUser(session);
List<Currency> currencies = currencyService.getAll(user);
List<Currency> currencies = currencyService.getAll();
model.addAttribute("currencies", currencies);
return "CurrenciesView";
} catch (AuthenticationException e) {
return "redirect:/auth/login";
} catch (AuthorizationException e) {
model.addAttribute("statusCode", (HttpStatus.UNAUTHORIZED));
model.addAttribute("error", e.getMessage());
return "ErrorView";
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.telerikacademy.web.virtualwallet.models.Transaction;
import com.telerikacademy.web.virtualwallet.models.Transfer;
import com.telerikacademy.web.virtualwallet.models.User;
import com.telerikacademy.web.virtualwallet.models.dtos.TransactionDto;
import com.telerikacademy.web.virtualwallet.models.dtos.TransactionToJoinDto;
import com.telerikacademy.web.virtualwallet.models.dtos.TransferDto;
import com.telerikacademy.web.virtualwallet.models.dtos.UserToWalletDto;
import com.telerikacademy.web.virtualwallet.models.dtos.*;
import com.telerikacademy.web.virtualwallet.models.wallets.JoinWallet;
import com.telerikacademy.web.virtualwallet.models.wallets.Wallet;
import com.telerikacademy.web.virtualwallet.services.contracts.*;
Expand All @@ -17,7 +14,6 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -39,25 +35,28 @@ public class JoinWalletMvcController {

private final UserService userService;

private final CurrencyService currencyService;

private final TransactionService transactionService;

private final AuthenticationHelper authenticationHelper;

private final TransactionMapper transactionMapper;

private final TransferMapper transferMapper;

private final CardService cardService;

private final TransactionService transactionService;


public JoinWalletMvcController(WalletService walletService, JoinWalletService joinWalletService, UserService userService, AuthenticationHelper authenticationHelper, TransactionMapper transactionMapper, TransferMapper transferMapper, CardService cardService, TransactionService transactionService) {
public JoinWalletMvcController(WalletService walletService, JoinWalletService joinWalletService
,UserService userService, CurrencyService currencyService, AuthenticationHelper authenticationHelper
,TransactionMapper transactionMapper, TransferMapper transferMapper, TransactionService transactionService) {
this.walletService = walletService;
this.joinWalletService = joinWalletService;
this.userService = userService;
this.currencyService = currencyService;
this.authenticationHelper = authenticationHelper;
this.transactionMapper = transactionMapper;
this.transferMapper = transferMapper;
this.cardService = cardService;
this.transactionService = transactionService;
}
@ModelAttribute("isAuthenticated")
Expand All @@ -82,6 +81,8 @@ public String showJoinWallet(Model model, HttpSession session,@PathVariable int
model.addAttribute("wallet", wallet);
model.addAttribute("userId", user.getId());
model.addAttribute("newUser",new UserToWalletDto());
model.addAttribute("currency", new ChangeCurrencyDto());
model.addAttribute("allCurrencies", currencyService.getAll());
return "JoinWalletView";
} catch (AuthenticationException e) {
return "redirect:/auth/login";
Expand Down Expand Up @@ -286,10 +287,36 @@ public String removeJoinWallet(HttpSession session, @PathVariable int id, Model
}
}

@GetMapping("/{id}/currency")
public String changeCurrency(HttpSession session, @PathVariable int id, Model model,
@Valid @ModelAttribute("currency") ChangeCurrencyDto currencyDto, BindingResult errors){
try {
User user = authenticationHelper.tryGetCurrentUser(session);
JoinWallet wallet = joinWalletService.get(id,user);
walletService.changeCurrency(wallet.getId(),currencyDto.getCurrencyId());
return "redirect:/join wallet/{id}";
} catch (AuthenticationException e) {
return "redirect:/auth/login";
} catch (AuthorizationException e) {
model.addAttribute("statusCode", (HttpStatus.UNAUTHORIZED));
model.addAttribute("error", e.getMessage());
return "ErrorView";
} catch (EntityNotFoundException e) {
model.addAttribute("statusCode", HttpStatus.NOT_FOUND.getReasonPhrase());
model.addAttribute("error", e.getMessage());
return "ErrorView";
}
}

@GetMapping("/{id}/fund")
public String showWalletFundPage(Model model){
model.addAttribute("transfer",new TransferDto());
return "FundWalletView";
public String showWalletFundPage(HttpSession session, Model model){
try {
User user = authenticationHelper.tryGetCurrentUser(session);
model.addAttribute("currentUser", user);
return "FundWalletView";
}catch (AuthenticationException | AuthorizationException e) {
return "redirect:/auth/login";
}
}

@GetMapping("/{id}/withdraw")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.telerikacademy.web.virtualwallet.exceptions.*;
import com.telerikacademy.web.virtualwallet.models.*;
import com.telerikacademy.web.virtualwallet.models.dtos.ChangeCurrencyDto;
import com.telerikacademy.web.virtualwallet.models.dtos.TransactionDto;
import com.telerikacademy.web.virtualwallet.models.dtos.TransactionToJoinDto;
import com.telerikacademy.web.virtualwallet.models.dtos.TransferDto;
Expand All @@ -14,7 +15,6 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.springframework.boot.Banner;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -35,26 +35,29 @@ public class WalletMvcController {

private final UserService userService;

private final AuthenticationHelper authenticationHelper;
private final CurrencyService currencyService;

private final TransactionMapper transactionMapper;
private final TransactionService transactionService;

private final TransferMapper transferMapper;
private final VerificationService verificationService;

private final CardService cardService;
private final AuthenticationHelper authenticationHelper;

private final TransactionService transactionService;
private final TransactionMapper transactionMapper;

private final VerificationService verificationService;
private final TransferMapper transferMapper;

public WalletMvcController(WalletService walletService, JoinWalletService joinWalletService, UserService userService, AuthenticationHelper authenticationHelper, TransactionMapper transactionMapper, TransferMapper transferMapper, CardService cardService, TransactionService transactionService, VerificationService verificationService) {
public WalletMvcController(WalletService walletService, JoinWalletService joinWalletService, UserService userService
, CurrencyService currencyService, AuthenticationHelper authenticationHelper, TransactionMapper transactionMapper
, TransferMapper transferMapper, TransactionService transactionService
, VerificationService verificationService) {
this.walletService = walletService;
this.joinWalletService = joinWalletService;
this.userService = userService;
this.currencyService = currencyService;
this.authenticationHelper = authenticationHelper;
this.transactionMapper = transactionMapper;
this.transferMapper = transferMapper;
this.cardService = cardService;
this.transactionService = transactionService;
this.verificationService = verificationService;
}
Expand All @@ -77,6 +80,8 @@ public String showPersonalWallet(Model model, HttpSession session){
int userWalletsCount = joinWalletService.getAllByUser(user).size() + 1;
model.addAttribute("userWalletsCount", userWalletsCount);
model.addAttribute("wallet", user.getWallet());
model.addAttribute("currency", new ChangeCurrencyDto());
model.addAttribute("allCurrencies", currencyService.getAll());
return "WalletView";
} catch (AuthenticationException e) {
return "redirect:/auth/login";
Expand Down Expand Up @@ -164,6 +169,26 @@ public String createTransferToOtherWallet(@Valid @ModelAttribute("transfer") Tra
}
}

@GetMapping("/currency")
public String changeCurrency(HttpSession session,Model model
, @Valid @ModelAttribute("currency") ChangeCurrencyDto currencyDto, BindingResult errors){
try {
User user = authenticationHelper.tryGetCurrentUser(session);
walletService.changeCurrency(user.getWallet().getId(),currencyDto.getCurrencyId());
return "redirect:/wallet" ;
} catch (AuthenticationException e) {
return "redirect:/auth/login";
} catch (AuthorizationException e) {
model.addAttribute("statusCode", (HttpStatus.UNAUTHORIZED));
model.addAttribute("error", e.getMessage());
return "ErrorView";
} catch (EntityNotFoundException e) {
model.addAttribute("statusCode", HttpStatus.NOT_FOUND.getReasonPhrase());
model.addAttribute("error", e.getMessage());
return "ErrorView";
}
}

@GetMapping("/fund")
public String showWalletFundPage(HttpSession session, Model model){
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public List<Currency> getAllCurrencies(@RequestHeader(name = "Authentication")
String authentication){
try {
User user = authenticationHelper.tryGetUser(authentication);
return currencyService.getAll(user);
return currencyService.getAll();
} catch (AuthenticationException e){
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.telerikacademy.web.virtualwallet.models.dtos;

import jakarta.validation.constraints.NotNull;

public class ChangeCurrencyDto {

@NotNull(message = "Currency can not be empty")
private int currencyId;

public int getCurrencyId() {
return currencyId;
}

public void setCurrencyId(int currencyId) {
this.currencyId = currencyId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CurrencyServiceImpl implements CurrencyService {
Expand All @@ -30,9 +31,8 @@ public Currency getById(int id) {
}

@Override
public List<Currency> getAll(User user) {
checkAdmin(user);
return currencyRepository.getAll();
public List<Currency> getAll() {
return new ArrayList<>(currencyRepository.getAll());
}
@Override
public void create(Currency currency,User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface CurrencyService {

Currency getById(int id);

List<Currency> getAll(User user);
List<Currency> getAll();

void create(Currency currency, User user);

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -2318,8 +2318,10 @@ header{

.join-fields {
padding: 4px;
border-width: 2px;
border-radius: 10px;
border-style: double;
border-width: 6px;
border-color: deepskyblue;
}

.w-20{
Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/templates/JoinWalletView.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,26 @@ <h2 class="font-aquire" th:text="${wallet.getName()}"></h2>
<span th:text="${wallet.getHoldingsByRating()}"></span>
<span th:text="${wallet.getCurrency().getCurrencyCode()}"></span>
<br/><br/>
<form action="#" th:action="@{__${requestURI}__/currency}" th:object="${currency}" method="get">
<select class="join-fields" th:field="*{currencyId}">
<option th:each="currency1 : ${allCurrencies}"
th:value="${currency1.getId()}"
th:text="${currency1.getCurrencyCode()}">
Currency
</option>
</select>
<br/>
<input class="py-3 wallet-button rounded font-aquire" type="submit" value="Change currency"/>
</form>
<br/>
<label style="font-weight: bold" th:text="#{users}">Users</label>
<br/>
<span style="font-size: 12pt" th:text="${wallet.getHolder().getUsername()}"></span>
<div th:each="user : ${wallet.getUsers()}">
<span style="font-size: 12pt" th:text="${user.getUsername()}"></span>
<br/>
</div>
<br/>
</div>
<a class="center" href="#" th:href="@{/join wallet/__${wallet.getId()}__/transactions/new}">
<div class="py-3 wallet-button rounded font-aquire" >Send</div></a>
Expand Down Expand Up @@ -50,6 +63,7 @@ <h2 class="font-aquire" th:text="${wallet.getName()}"></h2>
<br/>
<input class="py-3 wallet-button rounded font-aquire" type="submit" value="Remove user"/>
</form>
<br/>
</div>
<div th:unless="${userId == wallet.getHolder().getId()}">
<a class="center" href="#" th:href="@{__${requestURI}__/remove}">
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/templates/WalletView.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ <h2 class="font-aquire" th:text="${wallet.getName()}"></h2>
<span th:text="${wallet.getHoldingsByRating()}"></span>
<span th:text="${wallet.getCurrency().getCurrencyCode()}"></span>
<br/><br/>
<form action="#" th:action="@{__${requestURI}__/currency}" th:object="${currency}" method="get">
<select class="join-fields" th:field="*{currencyId}">
<option th:each="currency1 : ${allCurrencies}"
th:value="${currency1.getId()}"
th:text="${currency1.getCurrencyCode()}">
Currency
</option>
</select>
<br/>
<input class="py-3 wallet-button rounded font-aquire" type="submit" value="Change currency"/>
</form>
<br/>
</div>
<a class="center" href="#" th:href="@{/wallet/transactions/new}">
<div class="py-3 wallet-button rounded font-aquire" >Send</div></a>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/fragments/PageFrame.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<li th:if="${isAuthenticated && session.isAdmin}" class="nav-item mx-0 mx-lg-1">
<a class="nav-link py-3 px-0 px-lg-3 rounded" href="#" th:href="@{/users}"
th:text="#{navigation.all-users}">Browse all Users</a></li>
<li th:if="${isAuthenticated && session.isAdmin}" class="nav-item mx-0 mx-lg-1">
<li th:if="${isAuthenticated}" class="nav-item mx-0 mx-lg-1">
<a class="nav-link py-3 px-0 px-lg-3 rounded" href="#" th:href="@{/currencies}"
th:text="#{currencies}">Currencies</a></li>
<li th:if="${isAuthenticated}" class="nav-item mx-0 mx-lg-1">
Expand Down

0 comments on commit 7f22f1f

Please sign in to comment.