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

added buttons to increase/decrease stamp position


Signed-off-by: default avatarc2064724 <allen-harrise@cardiff.ac.uk>
parent 0da0b1f1
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
...@@ -26,7 +26,7 @@ public interface RewardsRepo extends JpaRepository<StampBoards, Long> { ...@@ -26,7 +26,7 @@ public interface RewardsRepo extends JpaRepository<StampBoards, Long> {
* @return - Optional object containing the Reward found, if it's present * @return - Optional object containing the Reward found, if it's present
*/ */
@Query("select r from Rewards r where r.rewardId = ?1") @Query("select r from Rewards r where r.rewardId = ?1")
Optional<Rewards> findByRewardId(int rewardId); Optional<Rewards> findByRewardId(long rewardId);
/** /**
...@@ -35,14 +35,6 @@ public interface RewardsRepo extends JpaRepository<StampBoards, Long> { ...@@ -35,14 +35,6 @@ public interface RewardsRepo extends JpaRepository<StampBoards, Long> {
* @return - Optional Integer containing the reward, if it's present * @return - Optional Integer containing the reward, if it's present
*/ */
@Query("select r.rewardStampLocation from Rewards r where r.rewardId = ?1") @Query("select r.rewardStampLocation from Rewards r where r.rewardId = ?1")
Optional<Integer> getRewardValueFromId(int rewardId); Optional<Integer> getRewardValueFromId(long 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);
} }
package com.example.clientproject.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@Service
public class GetStampBoardIdFromRewardId {
@Autowired
JdbcTemplate jdbc;
/**
* @param rewardId the rewardId of the stampBoardId you want to retrieve
*/
public int getStampBoardId(Integer rewardId){
String query = "SELECT Stamp_Board_Id FROM rewards WHERE Reward_Id = " + rewardId + ";";
try{
List<Map<String, Object>> rs = jdbc.queryForList(query);
System.out.println((int) rs.get(0).get("Stamp_Board_Id"));
return (int) rs.get(0).get("Stamp_Board_Id");
}catch (Exception e){
return 0;
}
}
}
...@@ -9,6 +9,7 @@ import com.example.clientproject.data.userStampBoards.UserStampBoards; ...@@ -9,6 +9,7 @@ import com.example.clientproject.data.userStampBoards.UserStampBoards;
import com.example.clientproject.data.users.Users; import com.example.clientproject.data.users.Users;
import com.example.clientproject.data.users.UsersRepo; import com.example.clientproject.data.users.UsersRepo;
import com.example.clientproject.service.Utils.JWTUtils; import com.example.clientproject.service.Utils.JWTUtils;
import com.example.clientproject.services.GetStampBoardIdFromRewardId;
import com.example.clientproject.services.StampboardUpdater; import com.example.clientproject.services.StampboardUpdater;
import com.example.clientproject.services.UserStampBoardService; import com.example.clientproject.services.UserStampBoardService;
import com.example.clientproject.web.forms.UpdateStampboardForm; import com.example.clientproject.web.forms.UpdateStampboardForm;
...@@ -29,15 +30,17 @@ public class UpdateUserStampPosition { ...@@ -29,15 +30,17 @@ public class UpdateUserStampPosition {
UserPermissionsRepo userPermissionsRepo; UserPermissionsRepo userPermissionsRepo;
RewardsRepo rewardsRepo; RewardsRepo rewardsRepo;
UsersRepo usersRepo; UsersRepo usersRepo;
GetStampBoardIdFromRewardId getStampBoardIdFromRewardId;
public UpdateUserStampPosition(JWTUtils jwt, UserStampBoardService usbs, public UpdateUserStampPosition(JWTUtils jwt, UserStampBoardService usbs,
UserPermissionsRepo upr, RewardsRepo rr, UserPermissionsRepo upr, RewardsRepo rr,
UsersRepo ur){ //need to add service for changing stamp pos UsersRepo ur, GetStampBoardIdFromRewardId gsbifri){ //need to add service for changing stamp pos
jwtUtils = jwt; jwtUtils = jwt;
userStampBoardService = usbs; userStampBoardService = usbs;
userPermissionsRepo = upr; userPermissionsRepo = upr;
rewardsRepo = rr; rewardsRepo = rr;
usersRepo = ur; usersRepo = ur;
getStampBoardIdFromRewardId = gsbifri;
} }
@PostMapping("/changeUserPos") @PostMapping("/changeUserPos")
...@@ -75,26 +78,24 @@ public class UpdateUserStampPosition { ...@@ -75,26 +78,24 @@ public class UpdateUserStampPosition {
} }
} }
@PostMapping @PostMapping("/reedeemReward")
public void reedeemStamps(@RequestParam(name="rewardId", required = true) int rewardId, HttpSession session){ public void reedeemStamps(@RequestParam(name="rewardId", required = true) int rewardId, HttpSession session){
Optional<Rewards> reward = rewardsRepo.findByRewardId(rewardId); Optional<Rewards> reward = rewardsRepo.findByRewardId(Long.valueOf(rewardId));
Optional<Integer> stampBoardId = rewardsRepo.getStampBoardIdById(rewardId); int stampBoardId = getStampBoardIdFromRewardId.getStampBoardId(rewardId);
Optional<Users> user = usersRepo.findById(Long.valueOf(jwtUtils.getLoggedInUserId(session).get())); Optional<Users> user = usersRepo.findById(Long.valueOf(jwtUtils.getLoggedInUserId(session).get()));
Set<UserStampBoards> userStampBoards = user.get().getUserStampBoards(); Set<UserStampBoards> userStampBoards = user.get().getUserStampBoards();
int userStampPos = 0; int userStampPos = 0;
long userStampBoardIdInLink = 0;
boolean userIsLinkedToStampBoard = false; boolean userIsLinkedToStampBoard = false;
for(UserStampBoards u : userStampBoards){ for(UserStampBoards u : userStampBoards){
if(stampBoardId.get() == u.getStampBoard().getStampBoardId()){ if(stampBoardId == u.getStampBoard().getStampBoardId()){
userStampBoardIdInLink = u.getStampBoard().getStampBoardId();
userStampPos = u.getUserStampPosition(); userStampPos = u.getUserStampPosition();
userIsLinkedToStampBoard = true; userIsLinkedToStampBoard = true;
} }
} }
if(userIsLinkedToStampBoard){ if(userIsLinkedToStampBoard){
if(userStampPos >= reward.get().getRewardStampLocation()){ if(userStampPos >= reward.get().getRewardStampLocation()){
userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), -1, userStampPos); userStampBoardService.changeUserStampPosition(jwtUtils.getLoggedInUserId(session).get(), -reward.get().getRewardStampLocation(), userStampPos);
} }
} else { } else {
System.out.println("User is not linked to stampboard you are trying to claim a reward from"); System.out.println("User is not linked to stampboard you are trying to claim a reward from");
......
...@@ -56,7 +56,7 @@ function changeUserStampPos(increment, shopId){ ...@@ -56,7 +56,7 @@ function changeUserStampPos(increment, shopId){
function claimReward(rewardId){ function claimReward(rewardId){
params = 'rewardId=' + rewardId; params = 'rewardId=' + rewardId;
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
xhttp.open("POST", '/changeUserPos', true); // true is asynchronous xhttp.open("POST", '/reedeemReward', true); // true is asynchronous
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.onload = function () { xhttp.onload = function () {
if (xhttp.readyState === 4 && xhttp.status === 200) { if (xhttp.readyState === 4 && xhttp.status === 200) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment