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

Merge branch 'issueTwelve' into 'develop'

Merging almost finished product, needs sessions to be complete for testing

See merge request !31
parents affaa70b cabd504f
No related branches found
No related tags found
6 merge requests!56tags will be saved to userFavTags table (needs user ID of current logged in user),!41Develop,!39Develop,!33Develop,!32Branch Update,!31Merging almost finished product, needs sessions to be complete for testing
Showing
with 305 additions and 28 deletions
......@@ -45,7 +45,7 @@ public class Shops {
* @param active - shop active status
*/
public Shops(String name, String website, String description, int earnings,
String image, String countries, boolean active) {
String image, String countries, boolean active, StampBoards stampBoard) {
this.shopName = name;
this.shopDescription = description;
this.shopWebsite = website;
......@@ -53,6 +53,7 @@ public class Shops {
this.shopImage = image;
this.shopCountries = countries;
this.shopActive = active;
this.stampBoard = stampBoard;
}
@ManyToMany(mappedBy="favouriteShops")
......
......@@ -2,8 +2,12 @@ package com.example.clientproject.services;
import com.example.clientproject.data.shops.Shops;
import com.example.clientproject.data.shops.ShopsRepo;
import com.example.clientproject.data.stampBoards.StampBoards;
import com.example.clientproject.data.stampBoards.StampBoardsRepo;
import com.example.clientproject.data.tags.Tags;
import com.example.clientproject.data.tags.TagsRepo;
import com.example.clientproject.data.userStampBoards.UserStampBoards;
import com.example.clientproject.data.userStampBoards.UserStampBoardsRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
......@@ -17,6 +21,9 @@ public class BusinessRegisterSaver {
@Autowired
ShopsRepo shopsRepo;
@Autowired
StampBoardsRepo stampBoards;
@Autowired
TagsRepo tagsRepo;
......@@ -25,13 +32,16 @@ public class BusinessRegisterSaver {
public void save(BusinessRegisterDTO business){
StampBoards stampBoard = stampBoards.findById(1L).get();
Shops shop = new Shops(business.getBusiness_register_name(),
business.getBusiness_register_url(),
business.getBusiness_register_desc(),
business.getEarnings(),
"shopPic.png",
"UK United Kingdom",
false);
false,
stampBoard);
shopsRepo.save(shop);
List<Tags> tagsList = tagsRepo.findAll();
......
package com.example.clientproject.services;
import com.example.clientproject.web.forms.UserFavouriteForm;
import lombok.AllArgsConstructor;
import lombok.Value;
@Value
@AllArgsConstructor
public class UserFavouriteDTO {
long userId;
long shopId;
public UserFavouriteDTO(UserFavouriteForm urf){
this(
urf.getUserId(),
urf.getShopId()
);
}
}
package com.example.clientproject.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service
public class UserFavouriteDeleter {
@Autowired
JdbcTemplate jdbc;
/**
* Takes a userfavourite DTO and removes it from the database.
* @param usfDTO
*/
public void delete(UserFavouriteDTO usfDTO){
String query = "DELETE FROM User_Shop_Links WHERE (Shop_Id = " +
usfDTO.getShopId() +" AND User_Id = " +
usfDTO.getUserId() +")";
//System.out.println(query);
jdbc.execute(query);
}
}
package com.example.clientproject.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service
public class UserFavouriteSaver {
@Autowired
JdbcTemplate jdbc;
/**
* Takes a user dto and saves it to the DB with jdbc
* @param urfDTO UserfavouriteDTO
*/
public void save(UserFavouriteDTO urfDTO){
String query = "INSERT INTO User_Shop_Links (Shop_Id, User_Id) VALUES ("+ urfDTO.getShopId() +
","+urfDTO.getUserId() + ")";
//System.out.println(query);
jdbc.execute(query);
}
}
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.sql.ResultSet;
import java.util.List;
import java.util.Map;
@Service
public class UserFavouriteToggle {
@Autowired
JdbcTemplate jdbc;
/**
* Checks whether the user has already favourited a shop
* @param urfDTO, Userfavourite DTO
* @return Boolean, true if it's already favourited false if not.
* @throws Exception
*/
public boolean alreadyInDb(UserFavouriteDTO urfDTO) throws Exception{
String query = "SELECT s.User_Shop_Link_Id FROM User_Shop_Links s WHERE (Shop_Id = " +
urfDTO.getShopId() + " AND User_Id = " +
urfDTO.getUserId() + " )";
List<Map<String, Object>> rs = jdbc.queryForList(query);
return rs.size() != 0;
}
}
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.searches.UsersSearch;
import com.example.clientproject.services.BusinessRegisterSaver;
import com.example.clientproject.services.UserFavouriteDTO;
import com.example.clientproject.services.UserFavouriteToggle;
import com.example.clientproject.web.forms.UserFavouriteForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList;
import java.util.List;
import static com.example.clientproject.web.controllers.SignInController.loggedIn;
@Controller
public class HomeController {
private ShopsRepo shopsRepo;
private UserFavouriteToggle toggleFavourite;
@Autowired
public HomeController(ShopsRepo ashopsRepo) {
public HomeController(ShopsRepo ashopsRepo, UserFavouriteToggle uft) {
shopsRepo = ashopsRepo;
toggleFavourite = uft;
}
@GetMapping({"/", "dashboard"})
public String index(Model model){
public String index(Model model) throws Exception{
loggedIn=true;
if (!loggedIn) {
model.addAttribute("loggedIn", loggedIn);
return "redirect:/login";
}
//System.out.println(shopsRepo.findAll());
model.addAttribute("shops", shopsRepo.findAll());
List<Shops> allShops = shopsRepo.findAll();
List<Shops> favouriteShops = new ArrayList();
List<Shops> normalShops = new ArrayList();
for(Shops s : allShops){
UserFavouriteForm uff = new UserFavouriteForm(2,s.getShopId());
if(toggleFavourite.alreadyInDb(new UserFavouriteDTO(uff))){
favouriteShops.add(s);
}else{
normalShops.add(s);
}
}
model.addAttribute("normalShops", normalShops);
model.addAttribute("favouriteShops", favouriteShops);
model.addAttribute("tags", new String[]{"Coffee", "Vegan", "Sustainable"});
return "index";
}
......
package com.example.clientproject.web.forms;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserFavouriteForm {
long userId;
long shopId;
}
package com.example.clientproject.web.restControllers;
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;
@RestController
public class BusinessFavouriter {
private UserFavouriteSaver saveFavourite;
private UserFavouriteToggle toggleFavourite;
private UserFavouriteDeleter deleteFavourite;
public BusinessFavouriter(UserFavouriteSaver ufs, UserFavouriteToggle uft, UserFavouriteDeleter ufd) {
saveFavourite = ufs;
toggleFavourite = uft;
deleteFavourite = ufd;
}
/**
*
* @param Submitted form, contains a UserID and ShopID
* @return ERROR or OK depending on whether it any errors are thrown.
*/
@PostMapping("/favouriteBusiness")
public String favouriteBusiness(UserFavouriteForm uff){
UserFavouriteDTO ufDTO = new UserFavouriteDTO(uff);
try{
if(toggleFavourite.alreadyInDb(ufDTO)){
deleteFavourite.delete(ufDTO);
}else{
saveFavourite.save(ufDTO);
}
return "OK";
}catch(Exception e){
e.printStackTrace();
return "ERROR";
}
}
}
......@@ -216,6 +216,7 @@ CREATE TABLE IF NOT EXISTS `mydb`.`User_Stamp_Boards` (
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
INSERT INTO two_factor_methods (`Two_Factor_Method_Id`, `Two_Factor_Method_Name`) VALUES (1, 'None');
INSERT INTO two_factor_methods (`Two_Factor_Method_Id`, `Two_Factor_Method_Name`) VALUES (2, 'GAuth');
......
......@@ -25,4 +25,29 @@
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover;
}
.starContainer .fas.fa-star{
transform: scale(0);
}
.starContainer.active .fas.fa-star{
transform: scale(1);
}
.starContainer{
cursor:pointer;
}
.favouriteStar{
color:gold;
top:2px;
right:2px;
position:absolute;
transition: transform 200ms;
}
.favouriteStar i{
color:gold;
transition: transform 200ms;
}
\ No newline at end of file
function favouriteBusiness(e,shopId){
if(e.classList.contains("active")){
e.classList.remove("active")
}else{
e.classList.add("active")
}
var xhttp = new XMLHttpRequest();
let params= 'userId=' + "2" + "&shopId=" + shopId
xhttp.open("POST", '/favouriteBusiness', true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.onload = function() {
if (xhttp.readyState === 4 && xhttp.status === 200) {
var response = xhttp.responseText
if (response == "success"){
}else{
}
} else {
console.error(xhttp.statusText);
}
};
xhttp.send(params);
}
\ No newline at end of file
......@@ -3,10 +3,19 @@
<head>
<meta charset="UTF-8">
<title>Title</title>
<script th:fragment="card_js" src="/js/favouriteBusiness.js"></script>
</head>
<body th:fragment="business_card">
<div class="business_container box">
<div class="business_container box" style="position:relative;">
<div class="image" th:style="'background-image:url(' + ${img_path} + ');'"></div>
<div class="favouriteStar starContainer" th:onclick="'favouriteBusiness(this,' + ${shopId} + ');'">
<span class="icon favouriteStar">
<i class="far fa-star"></i>
</span>
<span class="icon favouriteStar">
<i class="fas fa-star"></i>
</span>
</div>
<div class="content">
<h1 class="title is-4 mb-1" th:text="${title}"></h1>
<p class="mb-1" th:text="${reward_text}"></p>
......
......@@ -5,9 +5,17 @@
<title>Title</title>
</head>
<body th:fragment="reward_card">
<div class="reward_card">
<div class="reward_card" style="position:relative;">
<div class="favouriteStar starContainer" th:classappend="${isFavourite ? 'active' : ''}" th:onclick="'favouriteBusiness(this,' + ${shopId} + ');'">
<span class="icon favouriteStar">
<i class="far fa-star"></i>
</span>
<span class="icon favouriteStar">
<i class="fas fa-star"></i>
</span>
</div>
<div class="reward_card_content">
<h1 class="title is-4 mb-1" th:text="${title}"></h1>
<h1 class="title is-4 mb-1" th:text="${title}" style="white-space: break-spaces;"></h1>
<p class="mb-1" th:text="'You have ' + ${stamps} + ' stamps!'"></p>
<progress class="progress mb-1" th:value="${stamps}" th:max="${next_reward}"></progress>
<p class="mb-2" th:text="${next_reward}-${stamps} + ' to next reward'"></p>
......
......@@ -5,6 +5,7 @@
<link th:replace="fragments/libs.html :: bulma"/>
<link th:replace="fragments/libs.html :: fa"/>
<link th:replace="fragments/nav.html :: nav-css"/>
<script th:replace="fragments/business_card.html :: card_js"/>
<link rel="stylesheet" href="/css/index.css">
<link rel="stylesheet" href="/css/reward_card.css">
<link rel="stylesheet" href="/css/business_container.css">
......@@ -28,22 +29,16 @@
</div>
<div class="horiz_scroll is-full-width mb-1">
<!--Reward Card Here-->
<div th:replace="fragments/reward_card.html :: reward_card(
title='Coca Cola',
stamps=6,
next_reward=8,
reward_amount_obtained=2,
total_reward_amount=5,
img_path='imgs/coke.jpg'
)"></div>
<div th:replace="fragments/reward_card.html :: reward_card(
title='Cafe Nero',
stamps=2,
next_reward=4,
reward_amount_obtained=0,
total_reward_amount=4,
img_path='imgs/coffee.jpg'
)"></div>
<div th:each="shop,i: ${favouriteShops}" th:include="fragments/reward_card.html :: reward_card"
th:with="title=${shop.shopName},
stamps=6,
next_reward=8,
reward_amount_obtained=2,
isFavourite=true,
total_reward_amount=5,
img_path=${shop.shopImage},
shopId=${shop.shopId}"></div>
</div>
<a href="/">See All ></a>
......@@ -66,9 +61,9 @@
</div>
<div class="container is-full-width is-flex is-justify-content-center is-align-items-center is-flex-wrap-wrap">
<div th:each="shop,i: ${shops}" th:include="fragments/business_card.html :: business_card"
<div th:each="shop,i: ${normalShops}" th:include="fragments/business_card.html :: business_card"
th:with="title=${shop.shopName}, reward_text='Free coffee at 6 stamps', reward_amount=4,
img_path=${shop.shopImage}"></div>
img_path=${shop.shopImage}, shopId=${shop.shopId}"></div>
</div>
</div>
......
package com.example.clientproject.data.shops;
import com.example.clientproject.data.stampBoards.StampBoardsRepo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
......@@ -19,6 +20,9 @@ public class ShopsTests {
@Autowired
ShopsRepo shopsRepo;
@Autowired
StampBoardsRepo stampRepo;
@Test
public void shouldGet11Shops() throws Exception {
List<Shops> shopsList = shopsRepo.findAll();
......@@ -27,7 +31,7 @@ public class ShopsTests {
@Test
public void shouldGet12ShopsAfterInsert() throws Exception {
Shops newShop = new Shops("", "", "", 0, "", "", true);
Shops newShop = new Shops("", "", "", 0, "", "", true, stampRepo.getById(1L));
Shops shop = shopsRepo.save(newShop);
List<Shops> shopsList = shopsRepo.findAll();
......
......@@ -4,6 +4,7 @@ import com.example.clientproject.data.adminTypes.AdminTypes;
import com.example.clientproject.data.adminTypes.AdminTypesRepo;
import com.example.clientproject.data.shops.Shops;
import com.example.clientproject.data.shops.ShopsRepo;
import com.example.clientproject.data.stampBoards.StampBoardsRepo;
import com.example.clientproject.data.twoFactorMethods.TwoFactorMethods;
import com.example.clientproject.data.twoFactorMethods.TwoFactorMethodsRepo;
import com.example.clientproject.data.users.Users;
......@@ -36,6 +37,8 @@ public class UserPermissionsTests {
TwoFactorMethodsRepo twoFactorMethodsRepo;
@Autowired
AdminTypesRepo adminTypesRepo;
@Autowired
StampBoardsRepo stampRepo;
@Test
public void shouldGet157Permissions() {
......@@ -45,7 +48,7 @@ public class UserPermissionsTests {
@Test
public void shouldGet158PermissionsAfterInsert() throws Exception {
Shops newShop = new Shops("", "", "", 0, "", "", true);
Shops newShop = new Shops("", "", "", 0, "", "", true, stampRepo.getById(1L));
shopsRepo.save(newShop);
TwoFactorMethods twoFactorMethods = twoFactorMethodsRepo.findByTwoFactorMethodId(1).get();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment