Skip to content
Snippets Groups Projects
Commit 0da0b1f1 authored by Ethan Allen-Harris's avatar Ethan Allen-Harris Committed by Ethan Allen-Harris
Browse files

added buttons to increase/decrease stamp position

parent 1a2601a4
No related branches found
No related tags found
3 merge requests!114LoggingService service class, new method to add a log to the "Logs" table when...,!98added buttons to increase/decrease stamp position,!96merge into develop
......@@ -2,6 +2,7 @@ package com.example.clientproject.data.rewards;
import com.example.clientproject.data.shops.Shops;
import com.example.clientproject.data.stampBoards.StampBoards;
import com.example.clientproject.data.twoFactorMethods.TwoFactorMethods;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
......@@ -29,4 +30,7 @@ public class Rewards {
rewardStampLocation = rewardLocation;
}
@ManyToOne
@JoinColumn(name="Stamp_Board_Id", nullable=false)
private StampBoards stampBoards;
}
......@@ -22,10 +22,27 @@ public interface RewardsRepo extends JpaRepository<StampBoards, Long> {
/**
* Find a Reward by the name
* @param rewardName - name of the Reward to find
* @param rewardId Id of reward to find
* @return - Optional object containing the Reward found, if it's present
*/
@Query("select r from Rewards r where r.rewardId = ?1")
Optional<Rewards> findByRewardName(String rewardName);
Optional<Rewards> findByRewardId(int rewardId);
/**
* Find a Reward by the name
* @param rewardId Id of reward to find
* @return - Optional Integer containing the reward, if it's present
*/
@Query("select r.rewardStampLocation from Rewards r where r.rewardId = ?1")
Optional<Integer> getRewardValueFromId(int rewardId);
/**
* Find relating stampBoardId by rewardId
* @param rewardId Id of relating stampBoardId to find
* @return - Optional Integer containing the Id, if it's present
*/
@Query("select r.stampBoards.stampBoardId from Rewards r where r.rewardId = ?1")
Optional<Integer> getStampBoardIdById(int rewardId);
}
......@@ -10,9 +10,10 @@ import org.springframework.stereotype.Service;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Service
public class UserStampBoardRetriever {
public class UserStampBoardService {
@Autowired
JdbcTemplate jdbc;
......@@ -29,7 +30,6 @@ public class UserStampBoardRetriever {
public int getUserStampPos(int userID, int stampBoardID){
String query = "SELECT User_Stamp_Position FROM user_stamp_boards WHERE User_Id = " + userID + " AND Stamp_Board_Id = " + stampBoardID + ";";
try{
System.out.println(query);
List<Map<String, Object>> rs = jdbc.queryForList(query);
System.out.println((int) rs.get(0).get("User_Stamp_Position"));
......@@ -39,4 +39,10 @@ public class UserStampBoardRetriever {
}
}
public void changeUserStampPosition(int userID, int incrementValue, int currentUserStampPos){
int newStampPos = currentUserStampPos + incrementValue;
String query = "UPDATE user_stamp_boards SET User_Stamp_Position = " + newStampPos + " WHERE User_Id = " + userID + ";";
jdbc.execute(query);
}
}
......@@ -12,7 +12,7 @@ import com.example.clientproject.data.users.Users;
import com.example.clientproject.data.users.UsersRepo;
import com.example.clientproject.service.Utils.JWTUtils;
import com.example.clientproject.services.UserFavouriteTagSaver;
import com.example.clientproject.services.UserStampBoardRetriever;
import com.example.clientproject.services.UserStampBoardService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -35,13 +35,13 @@ public class BusinessDetails {
private JWTUtils jwtUtils;
private UserStampBoardRetriever userStampService;
private UserStampBoardService userStampService;
private SocialsRepo socialsRepo;
public BusinessDetails(ShopsRepo aShopRepo, StampBoardsRepo aStampBoard,
UsersRepo aUsersRepo, UserStampBoardRetriever aUserStampService,
UsersRepo aUsersRepo, UserStampBoardService aUserStampService,
JWTUtils ajwtUtils, SocialsRepo aSocialsRepo){
shopsRepo = aShopRepo;
stampRepo = aStampBoard;
......
package com.example.clientproject.web.restControllers;
import com.example.clientproject.data.rewards.Rewards;
import com.example.clientproject.data.rewards.RewardsRepo;
import com.example.clientproject.data.stampBoards.StampBoards;
import com.example.clientproject.data.userPermissions.UserPermissions;
import com.example.clientproject.data.userPermissions.UserPermissionsRepo;
import com.example.clientproject.data.userStampBoards.UserStampBoards;
import com.example.clientproject.data.users.Users;
import com.example.clientproject.data.users.UsersRepo;
import com.example.clientproject.service.Utils.JWTUtils;
import com.example.clientproject.services.StampboardUpdater;
import com.example.clientproject.services.UserStampBoardService;
import com.example.clientproject.web.forms.UpdateStampboardForm;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@RestController
public class UpdateUserStampPosition {
JWTUtils jwtUtils;
UserStampBoardService userStampBoardService;
UserPermissionsRepo userPermissionsRepo;
RewardsRepo rewardsRepo;
UsersRepo usersRepo;
public UpdateUserStampPosition(JWTUtils jwt, UserStampBoardService usbs,
UserPermissionsRepo upr, RewardsRepo rr,
UsersRepo ur){ //need to add service for changing stamp pos
jwtUtils = jwt;
userStampBoardService = usbs;
userPermissionsRepo = upr;
rewardsRepo = rr;
usersRepo = ur;
}
@PostMapping("/changeUserPos")
public void updateUserPos(@RequestParam(name="direction", required = true) String direction,
@RequestParam(name="shopId", required = true) String shopId, HttpSession session) {
//will firstly check that user has permission to do action
//Optional<Users> user = jwtUtils.getLoggedInUserRow(session);
long shopPermissionLevel = 0;
int currentUserStampPos = 0;
long shopStampBoardId = 0;
int shopStampBoardSize = 0;
int shopIdConverted = Integer.valueOf(shopId);
List<UserPermissions> userShops = userPermissionsRepo.findByUserId(jwtUtils.getLoggedInUserId(session).get());
for (UserPermissions u : userShops) { //loops through userPermissions and saves it to variable to be checked
if (u.getShop().getShopId() == shopIdConverted) {
shopPermissionLevel = u.getAdminType().getAdminTypeId();
shopStampBoardId = u.getShop().getStampBoard().getStampBoardId();
shopStampBoardSize = u.getShop().getStampBoard().getStampBoardSize();
}
}
System.out.println(shopPermissionLevel);
System.out.println(shopStampBoardId);
System.out.println(shopStampBoardSize);
if(shopPermissionLevel > 1){//user has the correct level to add/subtract their own stampBoard place
currentUserStampPos = userStampBoardService.getUserStampPos(jwtUtils.getLoggedInUserId(session).get(), (int) shopStampBoardId );
if(Objects.equals(direction, "subtract")){
if(currentUserStampPos != 0){
userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), -1, currentUserStampPos);
}
} else if(Objects.equals(direction, "add")){
if(currentUserStampPos != shopStampBoardSize){
userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), 1, currentUserStampPos);
}
}
}
}
@PostMapping
public void reedeemStamps(@RequestParam(name="rewardId", required = true) int rewardId, HttpSession session){
Optional<Rewards> reward = rewardsRepo.findByRewardId(rewardId);
Optional<Integer> stampBoardId = rewardsRepo.getStampBoardIdById(rewardId);
Optional<Users> user = usersRepo.findById(Long.valueOf(jwtUtils.getLoggedInUserId(session).get()));
Set<UserStampBoards> userStampBoards = user.get().getUserStampBoards();
int userStampPos = 0;
long userStampBoardIdInLink = 0;
boolean userIsLinkedToStampBoard = false;
for(UserStampBoards u : userStampBoards){
if(stampBoardId.get() == u.getStampBoard().getStampBoardId()){
userStampBoardIdInLink = u.getStampBoard().getStampBoardId();
userStampPos = u.getUserStampPosition();
userIsLinkedToStampBoard = true;
}
}
if(userIsLinkedToStampBoard){
if(userStampPos >= reward.get().getRewardStampLocation()){
userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), -1, userStampPos);
}
} else {
System.out.println("User is not linked to stampboard you are trying to claim a reward from");
}
}
}
......@@ -183,3 +183,12 @@ img.stamp{
align-items: center;
position: relative;
}
.change-stamp-progress-container{
position: absolute;
display: flex;
flex-direction: column;
top: 70%;
right: 3%;
width: 10px;
}
......@@ -40,9 +40,9 @@ function submitCategories(listSize){
status.innerHTML = "There was an error try again later.";
}
};
xhttp.onerror = function () {
status.innerHTML = "There was an error. Please try again later.";
}
xhttp.onerror = function () {
status.innerHTML = "There was an error. Please try again later.";
}
xhttp.send(params);
return false;
}
......
var currentPage = 1;
function pageNav(direction){
//this checks that current page will not go to 0 if "direction" = -1 / left
if (direction === -1){ //"-1" = left
......@@ -26,6 +27,46 @@ function pageNav(direction){
}
}
function toBusinessWebsite(URL){
window.location.href = URL;
function changeUserStampPos(increment, shopId){
if (increment === "subtract"){ //take away a stamp position from user
params = 'direction=subtract';
} else if(increment === "add"){ //add a stamp to user
params = 'direction=add';
} else {
return null;
}
params = params + '&shopId=' + shopId;
var xhttp = new XMLHttpRequest();
xhttp.open("POST", '/changeUserPos', true); // true is asynchronous
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.onload = function () {
if (xhttp.readyState === 4 && xhttp.status === 200) {
console.log(xhttp.responseText);
window.location.reload(true);
}
}
xhttp.onerror = function () {
alert("There was an error. Database has not been updated.");
}
xhttp.send(params);
return false;
}
function claimReward(rewardId){
params = 'rewardId=' + rewardId;
var xhttp = new XMLHttpRequest();
xhttp.open("POST", '/changeUserPos', true); // true is asynchronous
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.onload = function () {
if (xhttp.readyState === 4 && xhttp.status === 200) {
console.log(xhttp.responseText);
window.location.reload(true);
}
}
xhttp.onerror = function () {
alert("There was an error. Database has not been updated.");
}
xhttp.send(params);
return false;
}
......@@ -41,7 +41,7 @@
<div th:each="reward : ${stampBoard.rewards}" class="reward is-fullwidth">
<p class="reward-text" th:text="${reward.rewardName}"></p>
<button th:unless="${UserStampPos[0]>=reward.rewardStampLocation}" class="button is-small buttonRewardNotReady">Cannot afford</button>
<button th:if="${UserStampPos[0]>=reward.rewardStampLocation}" class="button is-small buttonRewardisReady">Click to claim!</button>
<button th:if="${UserStampPos[0]>=reward.rewardStampLocation}" class="button is-small buttonRewardisReady" th:onclick="claimReward([[${reward.rewardId}]])">Click to claim!</button>
<progress class="progress is-small marginBottom7px is-success" th:value="${UserStampPos[0]}" th:max="${reward.rewardStampLocation}"></progress>
<!-- TH:STYLE HAS NOT BEEN SET SO EXPECT AN ERROR -->
<p class="stampProgressText" th:unless="${UserStampPos[0]>=reward.rewardStampLocation}" th:text="${reward.rewardStampLocation - UserStampPos[0]} + ' away from reward'"/>
......@@ -132,5 +132,10 @@
<!-- <img th:src="${shop.shopImage}" alt=""> -->
<!-- <h1 th:text="${stampBoard.stampBoardSize}"></h1> -->
</div>
<div class="change-stamp-progress-container">
<button class="button is-rounded is-info" th:onclick="changeUserStampPos('add', [[${shop.getShopId}]])">+</button>
<button class="button is-rounded is-info" th:onclick="changeUserStampPos('subtract', [[${shop.getShopId}]])">-</button>
</div>
<span th:value="${shop.getShopId}" id="shopId"/>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment