Skip to content
Snippets Groups Projects
Commit c1e2b041 authored by Benjamin Treels's avatar Benjamin Treels
Browse files

Merge branch 'feat/subscribe_to_shopkeeper' into 'develop'

subscribe to shopkeeper

See merge request !31
parents 1bd4effe 8e61c79b
No related branches found
No related tags found
1 merge request!31subscribe to shopkeeper
Pipeline #40057 failed
package com.example.demo.controller;
import com.example.demo.model.account.Account;
import com.example.demo.model.customer.Customer;
import com.example.demo.service.impl.CustomerServiceImpl;
import com.example.demo.service.subscribe.SubscribeServiceImpl;
import com.example.demo.service.token.TokenAccountManagerImpl;
import com.example.demo.service.token.TokenAuthorizationServiceImpl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin(origins = {"http://localhost:3000"})
@RequestMapping("")
public class SubscribeController {
private final TokenAccountManagerImpl tokenAccountManager;
private final TokenAuthorizationServiceImpl tokenAuthorizationService;
private final CustomerServiceImpl customerService;
private final SubscribeServiceImpl subscribeService;
public SubscribeController(TokenAccountManagerImpl tokenAccountManager, TokenAuthorizationServiceImpl tokenAuthorizationService, CustomerServiceImpl customerService, SubscribeServiceImpl subscribeService) {
this.tokenAccountManager = tokenAccountManager;
this.tokenAuthorizationService = tokenAuthorizationService;
this.customerService = customerService;
this.subscribeService = subscribeService;
}
@GetMapping("/customer/is_subscriber/{shopkeeperId}")
public ResponseEntity<Boolean> customerIsSubscriber(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable Long shopkeeperId
){
String[] allowedRoles = new String[]{"customer", "CUSTOMER"};
if (!this.tokenAuthorizationService.isTokenAllowedToAccessResource(authorizationHeader, allowedRoles)){
return ResponseEntity.status(403).body(null);
}
Account account = tokenAccountManager.getAccountByToken(tokenAccountManager.removeBearerFromAuthorization(authorizationHeader));
Customer customer = customerService.findCustomerByAccountId(account.getId());
return ResponseEntity.status(200).body(this.subscribeService.isSubscriber(customer, shopkeeperId));
}
@GetMapping("/subscribers_count/{shopkeeperId}")
public ResponseEntity<Integer> getNumbersOfSubscribers(
@PathVariable Long shopkeeperId
){
return ResponseEntity.status(200)
.body(this.subscribeService.countSubscribers(shopkeeperId));
}
@PostMapping("/subscribe/{shopkeeperId}")
public ResponseEntity<Boolean> subscribeToShopKeeper(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable Long shopkeeperId
){
String[] allowedRoles = new String[]{"customer", "CUSTOMER"};
if (!this.tokenAuthorizationService.isTokenAllowedToAccessResource(authorizationHeader, allowedRoles)){
return ResponseEntity.status(403).body(false);
}
Account account = tokenAccountManager.getAccountByToken(tokenAccountManager.removeBearerFromAuthorization(authorizationHeader));
Customer customer = customerService.findCustomerByAccountId(account.getId());
Boolean hasSubscribed = this.subscribeService.subscribe(customer, shopkeeperId);
return ResponseEntity.status(200).body(hasSubscribed);
}
@PostMapping("/unsubscribe/{shopkeeperId}")
public ResponseEntity<Boolean> unsubscribeCustomer(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable Long shopkeeperId
){
String[] allowedRoles = new String[]{"customer", "CUSTOMER"};
if (!this.tokenAuthorizationService.isTokenAllowedToAccessResource(authorizationHeader, allowedRoles)){
return ResponseEntity.status(403).body(false);
}
Account account = tokenAccountManager.getAccountByToken(tokenAccountManager.removeBearerFromAuthorization(authorizationHeader));
Customer customer = customerService.findCustomerByAccountId(account.getId());
Boolean hasUnSubscribed = this.subscribeService.unsubscribe(customer, shopkeeperId);
return ResponseEntity.status(200).body(hasUnSubscribed);
}
}
......@@ -9,6 +9,7 @@ import com.example.demo.repository.order.OrderEntityRepository;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
......@@ -24,11 +25,11 @@ public class Customer extends TimestampedEntity {
@OneToOne(mappedBy = "customer", cascade = CascadeType.REMOVE)
@JsonIgnore
private FidelityCard fidelityCard;
@ManyToMany(cascade = CascadeType.REMOVE)
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinTable(name = "shopkeeper_subscribers",
joinColumns = @JoinColumn(name = "customer_id"),
inverseJoinColumns = @JoinColumn(name = "shopkeeper_id"))
private Set<ShopKeeper> follows;
private Set<ShopKeeper> follows = new HashSet<>();
private String firstname;
private String lastname;
private String phoneNumber;
......
......@@ -7,6 +7,7 @@ import com.example.demo.model.order.OrderInShop;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
......@@ -26,8 +27,8 @@ public class ShopKeeper extends TimestampedEntity {
@OneToMany(mappedBy="shopkeeper", cascade = CascadeType.REMOVE)
private Set<OrderInShop> ordersInShop;
@ManyToMany(mappedBy="follows", cascade = CascadeType.REMOVE)
private Set<Customer> subscribers;
@ManyToMany(mappedBy="follows", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Customer> subscribers = new HashSet<>();
private String firstName;
private String lastname;
......
package com.example.demo.service.subscribe;
import com.example.demo.model.customer.Customer;
import com.example.demo.model.shop.ShopKeeper;
public interface SubscribeService {
Boolean subscribe(Customer customer, Long shopKeeperId);
Boolean unsubscribe(Customer customer, Long shopKeeperId);
Integer countSubscribers(Long shopkeeperId);
Boolean isSubscriber(Customer customer, Long shopkeeperId);
}
package com.example.demo.service.subscribe;
import com.example.demo.model.customer.Customer;
import com.example.demo.model.shop.ShopKeeper;
import com.example.demo.repository.account.CustomerRepository;
import com.example.demo.repository.account.ShopkeeperRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;
@Service
public class SubscribeServiceImpl implements SubscribeService{
private final ShopkeeperRepository shopkeeperRepository;
private final CustomerRepository customerRepository;
public SubscribeServiceImpl(ShopkeeperRepository shopkeeperRepository, CustomerRepository customerRepository) {
this.shopkeeperRepository = shopkeeperRepository;
this.customerRepository = customerRepository;
}
@Override
public Boolean subscribe(Customer customer, Long shopKeeperId) {
ShopKeeper shopKeeper = shopkeeperRepository.getShopKeeperById(shopKeeperId);
if (shopKeeper == null){ return false;}
customer.getFollows().add(shopKeeper);
shopKeeper.getSubscribers().add(customer);
customerRepository.save(customer);
shopkeeperRepository.save(shopKeeper);
return true;
}
@Override
public Boolean unsubscribe(Customer customer, Long shopKeeperId) {
ShopKeeper shopKeeper = shopkeeperRepository.getShopKeeperById(shopKeeperId);
if (shopKeeper == null){ return false;}
shopKeeper.getSubscribers().remove(customer);
customer.getFollows().remove(shopKeeper);
shopkeeperRepository.saveAndFlush(shopKeeper);
return true;
}
@Override
public Integer countSubscribers(Long shopkeeperId) {
ShopKeeper shopKeeper = shopkeeperRepository.getShopKeeperById(shopkeeperId);
if (shopKeeper == null){ return 0;}
return shopKeeper.getSubscribers().size();
}
@Override
public Boolean isSubscriber(Customer customer, Long shopkeeperId) {
ShopKeeper shopKeeper = shopkeeperRepository.getShopKeeperById(shopkeeperId);
if (shopKeeper == null){ return false;}
return customer.getFollows().contains(shopKeeper);
}
}
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