Skip to content

Commit

Permalink
frontend for new customer registration
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpochat committed Feb 5, 2024
1 parent d8fafb1 commit 88cd572
Show file tree
Hide file tree
Showing 23 changed files with 274 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface CustomerRepository extends CrudRepository<Customer, Integer> {
@Query("""
Expand All @@ -17,4 +18,10 @@ public interface CustomerRepository extends CrudRepository<Customer, Integer> {
INNER JOIN pl.production p
WHERE p.producer = :producer""")
List<Customer> findByProducer(@Param("producer") Producer producer);

@Query("""
SELECT c FROM Customer c
INNER JOIN c.user u
WHERE u.email = :email""")
Optional<Customer> findByEmail(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import eu.viandeendirect.model.Address;
import eu.viandeendirect.model.Producer;
import eu.viandeendirect.repository.AddressRepository;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -19,7 +19,7 @@ public class AddresseService implements AddressesApiDelegate {
AddressRepository addressRepository;

@Autowired
AuthenticationProducerServiceSpecs producerService;
AuthenticationServiceSpecs producerService;

@Override
public ResponseEntity<List<Address>> getAddresses() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
package eu.viandeendirect.service;

import eu.viandeendirect.api.ProducersApiDelegate;
import eu.viandeendirect.model.Customer;
import eu.viandeendirect.model.Producer;
import eu.viandeendirect.model.Sale;
import eu.viandeendirect.repository.CustomerRepository;
import eu.viandeendirect.repository.ProducerRepository;
import eu.viandeendirect.repository.SaleRepository;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.stereotype.Service;

import static org.springframework.http.HttpStatus.CREATED;

@Service
@Profile("!test")
public class AuthenticationProducerService implements AuthenticationProducerServiceSpecs {
public class AuthenticationService implements AuthenticationServiceSpecs {

@Autowired
ProducerRepository producerRepository;

@Autowired
CustomerRepository customerRepository;

@Override
public Producer getAuthenticatedProducer() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String email = ((JwtAuthenticationToken)authentication).getToken().getClaimAsString("email");
Producer producer = producerRepository.findByEmail(email).orElseThrow();
Producer producer = producerRepository.findByEmail(email).orElse(null);
return producer;
}

@Override
public Customer getAuthenticatedCustomer() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String email = ((JwtAuthenticationToken)authentication).getToken().getClaimAsString("email");
Customer customer = customerRepository.findByEmail(email).orElse(null);
return customer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import eu.viandeendirect.api.BeefProductionsApiDelegate;
import eu.viandeendirect.model.BeefProduction;
import eu.viandeendirect.repository.ProductionRepository;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -16,7 +16,7 @@ public class BeefProductionService implements BeefProductionsApiDelegate {
ProductionRepository productionRepository;

@Autowired
AuthenticationProducerServiceSpecs producerService;
AuthenticationServiceSpecs producerService;

@Override
public ResponseEntity<BeefProduction> getBeefProduction(Integer beefProductionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import eu.viandeendirect.api.CustomersApiDelegate;
import eu.viandeendirect.model.Customer;
import eu.viandeendirect.model.Producer;
import eu.viandeendirect.repository.CustomerRepository;
import eu.viandeendirect.repository.UserRepository;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.NOT_FOUND;

@Service
public class CustomerService implements CustomersApiDelegate {
Expand All @@ -23,7 +23,7 @@ public class CustomerService implements CustomersApiDelegate {
UserRepository userRepository;

@Autowired
AuthenticationProducerServiceSpecs producerService;
AuthenticationServiceSpecs authenticationService;


@Override
Expand All @@ -33,9 +33,15 @@ public ResponseEntity<Customer> createCustomer(Customer customer) {
}

@Override
public ResponseEntity<List<Customer>> getCustomers() {
Producer producer = producerService.getAuthenticatedProducer();
return new ResponseEntity<>(customerRepository.findByProducer(producer), HttpStatus.OK);
public ResponseEntity<Customer> getCustomer(String email) {
Customer customer = authenticationService.getAuthenticatedCustomer();
if (customer == null) {
return new ResponseEntity<>(NOT_FOUND);
}
if (customer.getUser().getEmail().equals(email)) {
return new ResponseEntity<>(FORBIDDEN);
}
return new ResponseEntity<>(customer, HttpStatus.OK);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package eu.viandeendirect.service;

import eu.viandeendirect.api.ProducersApiDelegate;
import eu.viandeendirect.model.Customer;
import eu.viandeendirect.model.Producer;
import eu.viandeendirect.model.Sale;
import eu.viandeendirect.repository.CustomerRepository;
import eu.viandeendirect.repository.ProducerRepository;
import eu.viandeendirect.repository.SaleRepository;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

Expand All @@ -25,7 +28,10 @@ public class ProducerService implements ProducersApiDelegate {
SaleRepository saleRepository;

@Autowired
AuthenticationProducerServiceSpecs producerService;
AuthenticationServiceSpecs producerService;

@Autowired
CustomerRepository customerRepository;

@Override
public ResponseEntity<Sale> createProducerSale(Integer producerId, Sale sale) {
Expand Down Expand Up @@ -53,4 +59,13 @@ public ResponseEntity<List<Sale>> getProducerSales(Integer producerId) {
saleRepository.findBySeller(producer).forEach(sales::add);;
return new ResponseEntity<>(sales, OK);
}

@Override
public ResponseEntity<List<Customer>> getProducerCustomers(Integer producerId) {
Producer producer = producerService.getAuthenticatedProducer();
if (!producer.getId().equals(producerId)) {
return new ResponseEntity<>(FORBIDDEN);
}
return new ResponseEntity<>(customerRepository.findByProducer(producer), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import eu.viandeendirect.model.Production;
import eu.viandeendirect.repository.PackageLotRepository;
import eu.viandeendirect.repository.ProductionRepository;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand All @@ -25,7 +25,7 @@ public class ProductionService implements ProductionsApiDelegate {
PackageLotRepository packageLotRepository;

@Autowired
AuthenticationProducerServiceSpecs producerService;
AuthenticationServiceSpecs producerService;

@Override
public ResponseEntity<List<Production>> getProductions(Boolean forSale) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import eu.viandeendirect.repository.OrderRepository;
import eu.viandeendirect.repository.ProductionRepository;
import eu.viandeendirect.repository.SaleRepository;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand All @@ -22,7 +22,7 @@ public class SaleService implements SalesApiDelegate {
SaleRepository saleRepository;

@Autowired
AuthenticationProducerServiceSpecs producerService;
AuthenticationServiceSpecs producerService;

@Autowired
private OrderRepository orderRepository;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package eu.viandeendirect.service.specs;

import eu.viandeendirect.model.Customer;
import eu.viandeendirect.model.Producer;

public interface AuthenticationServiceSpecs {
Producer getAuthenticatedProducer();

Customer getAuthenticatedCustomer();
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package eu.viandeendirect.service;

import eu.viandeendirect.model.Customer;
import eu.viandeendirect.model.Producer;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Service
@Profile("test")
public class AuthenticationProducerServiceForTests implements AuthenticationProducerServiceSpecs {
public class AuthenticationProducerServiceForTests implements AuthenticationServiceSpecs {

@Override
public Producer getAuthenticatedProducer() {
var producer = new Producer();
producer.setId(1000);
return producer;
}

@Override
public Customer getAuthenticatedCustomer() {
var customer = new Customer();
customer.setId(1000);
return customer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import eu.viandeendirect.model.Producer;
import eu.viandeendirect.model.Sale;
import eu.viandeendirect.repository.SaleRepository;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import eu.viandeendirect.service.specs.AuthenticationServiceSpecs;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,7 +27,7 @@
public class TestProducerService {

@Autowired
AuthenticationProducerServiceSpecs authenticationProducerService;
AuthenticationServiceSpecs authenticationProducerService;

@Autowired
SaleRepository saleRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import eu.viandeendirect.model.Order;
import eu.viandeendirect.model.Production;
import eu.viandeendirect.model.Sale;
import eu.viandeendirect.repository.SaleRepository;
import eu.viandeendirect.service.specs.AuthenticationProducerServiceSpecs;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
12 changes: 8 additions & 4 deletions frontend/app/src/api/mock/MockApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ export class MockApi {
callback(undefined, this.mockApiAddresses.getAddresses())
}

getCustomers(callback) {
callback(undefined, this.mockApiCustomers.getCustomers())
createCustomer(customer, callback){
callback(undefined, this.mockApiCustomers.createCustomer(customer))
}

getCutomer(customer, callback){
callback(undefined, this.mockApiCustomers.createCustomer(customer))
getCustomer(options, callback){
callback(undefined, this.mockApiCustomers.getCustomer())
}

getProducerSales(options, callback) {
callback(undefined, this.mockApiProducers.getProducerSales())
}

getProducerCustomers(callback) {
callback(undefined, this.mockApiProducers.getCustomers())
}

getSales(callback) {
callback(undefined, this.mockApiSales.getSales())
}
Expand Down
31 changes: 7 additions & 24 deletions frontend/app/src/api/mock/MockApiCustomers.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
import Customer from "viandeendirect_eu/dist/model/Customer"

export class MockApiCustomers {
getCustomers(): Array<Customer> {
const customer1 = {

getCustomer() {
return {
id: 1,
user: {
id: 1,
firstName: 'Bob',
lastName: 'Sinclair',
email: '[email protected]',
phone: '01 02 03 04 05'
}
}
const customer2 = {
id: 2,
user: {
firstName: 'Amélie',
lastName: 'Poulain',
email: '[email protected]',
phone: '06 02 03 04 05'
}
}
const customer3 = {
id: 3,
user: {
firstName: 'François',
lastName: 'Pinion',
email: '[email protected]',
phone: '07 02 03 04 05'
lastName: 'Marcel',
email: '[email protected]'
}
}
return [customer1, customer2, customer3]
//return undefined
}

createCustomer(customer: Customer) {
Expand Down
Loading

0 comments on commit 88cd572

Please sign in to comment.