Skip to content
Snippets Groups Projects
Commit 2c6a1f92 authored by Seb Barnard's avatar Seb Barnard :speech_balloon:
Browse files

Added session functionality to favourite button, now user specific

parent 4f0c8141
Branches
No related tags found
6 merge requests!56tags will be saved to userFavTags table (needs user ID of current logged in user),!50Merging for latest changes,!46Develop,!44Branch update,!43Develop,!42Issue twelve
......@@ -10,9 +10,9 @@ public class UserFavouriteDTO {
long userId;
long shopId;
public UserFavouriteDTO(UserFavouriteForm urf){
public UserFavouriteDTO(UserFavouriteForm urf, long userId){
this(
urf.getUserId(),
userId,
urf.getShopId()
);
}
......
......@@ -2,6 +2,7 @@ package com.example.clientproject.web.controllers;
import com.example.clientproject.data.shops.Shops;
import com.example.clientproject.data.shops.ShopsRepo;
import com.example.clientproject.service.Utils.JWTUtils;
import com.example.clientproject.service.searches.UsersSearch;
import com.example.clientproject.services.BusinessRegisterSaver;
import com.example.clientproject.services.UserFavouriteDTO;
......@@ -12,6 +13,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
......@@ -30,8 +32,7 @@ public class HomeController {
}
@GetMapping({"/", "dashboard"})
public String index(Model model) throws Exception{
loggedIn=true;
public String index(Model model, HttpSession session) throws Exception{
if (!loggedIn) {
model.addAttribute("loggedIn", loggedIn);
return "redirect:/login";
......@@ -43,8 +44,8 @@ public class HomeController {
List<Shops> normalShops = new ArrayList();
for(Shops s : allShops){
UserFavouriteForm uff = new UserFavouriteForm(2,s.getShopId());
if(toggleFavourite.alreadyInDb(new UserFavouriteDTO(uff))){
UserFavouriteForm uff = new UserFavouriteForm(s.getShopId());
if(toggleFavourite.alreadyInDb(new UserFavouriteDTO(uff, JWTUtils.getLoggedInUserId(session).get()))){
favouriteShops.add(s);
}else{
normalShops.add(s);
......
......@@ -84,7 +84,8 @@ public class SignInController {
@PostMapping("login")
public String signInChecks(@Valid LoginForm loginForm,
BindingResult bindingResult,
Model model) {
Model model,
HttpSession session) {
if (bindingResult.hasErrors()) {
model.addAttribute("loggedIn", loggedIn);
......@@ -104,6 +105,7 @@ public class SignInController {
// If they match, set the loggedIn flag to true
if (passwordMatch) {
JWTUtils.makeUserJWT((int)usersDTOOptional.get().getUserId(), session);
loggedIn = true;
// Otherwise, throw an exception with the correct error message
} else {
......
......@@ -8,7 +8,6 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public class UserFavouriteForm {
long userId;
long shopId;
......
package com.example.clientproject.web.restControllers;
import com.example.clientproject.service.Utils.JWTUtils;
import com.example.clientproject.service.searches.UsersSearch;
import com.example.clientproject.services.*;
import com.example.clientproject.web.forms.UserFavouriteForm;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
public class BusinessFavouriter {
......@@ -26,8 +29,8 @@ public class BusinessFavouriter {
* @return ERROR or OK depending on whether it any errors are thrown.
*/
@PostMapping("/favouriteBusiness")
public String favouriteBusiness(UserFavouriteForm uff){
UserFavouriteDTO ufDTO = new UserFavouriteDTO(uff);
public String favouriteBusiness(UserFavouriteForm uff, HttpSession session){
UserFavouriteDTO ufDTO = new UserFavouriteDTO(uff, JWTUtils.getLoggedInUserId(session).get());
try{
if(toggleFavourite.alreadyInDb(ufDTO)){
deleteFavourite.delete(ufDTO);
......
......@@ -7,7 +7,7 @@ function favouriteBusiness(e,shopId){
}
var xhttp = new XMLHttpRequest();
let params= 'userId=' + "2" + "&shopId=" + shopId
let params= "shopId=" + shopId
xhttp.open("POST", '/favouriteBusiness', true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.onload = function() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment