This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Events integration #64
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3ffcd29
Add application.yml to ignore
svenpopping d8b17c2
Add .iml files to ignore
svenpopping 1e0ef37
Add context-path to test properties
svenpopping 14b1b1d
Add get product by key
svenpopping c2f70e7
Add BESTUUR to CommitteeEnum
svenpopping 0e72338
Enable XAuth in RestAssured
svenpopping ce07036
Implement and test events sync
svenpopping 7a49557
Remove copyrights above class
svenpopping 3ea6260
Disable new test
svenpopping 14e1226
Enable test again
svenpopping b9e45db
Change determineCreateOrUpdate to creatOrUpdate
svenpopping af590d2
Replace strings with enum
svenpopping b8d8976
Fix trigger not supported
svenpopping File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/ch/wisv/payments/model/eventsync/EventsSync.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ch.wisv.payments.model.eventsync; | ||
|
||
import lombok.Data; | ||
import lombok.Getter; | ||
|
||
@Data | ||
class EventsSync { | ||
|
||
@Getter | ||
private String trigger; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/ch/wisv/payments/model/eventsync/EventsSyncEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ch.wisv.payments.model.eventsync; | ||
|
||
public enum EventsSyncEnum { | ||
|
||
PRODUCT_CREATE_EDIT, | ||
PRODUCT_DELETE | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/ch/wisv/payments/model/eventsync/ProductEventsSync.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ch.wisv.payments.model.eventsync; | ||
|
||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
@EqualsAndHashCode(callSuper = false) | ||
@Data | ||
public class ProductEventsSync extends EventsSync { | ||
|
||
/** | ||
* Price of the product. | ||
*/ | ||
private Double price; | ||
|
||
/** | ||
* Description of the product. | ||
*/ | ||
private String description; | ||
|
||
/** | ||
* Title of the product. | ||
*/ | ||
private String title; | ||
|
||
/** | ||
* UUID of the product. | ||
*/ | ||
private String key; | ||
|
||
/** | ||
* Enum string of the organizing Committee. | ||
*/ | ||
private String organizedBy; | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/ch/wisv/payments/rest/eventsync/EventsSyncProductService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ch.wisv.payments.rest.eventsync; | ||
|
||
import ch.wisv.payments.model.eventsync.ProductEventsSync; | ||
|
||
public interface EventsSyncProductService { | ||
|
||
/** | ||
* Create a Product from a ProductEventsSync model. | ||
* | ||
* @param productEventsSync of type ProductEventsSync | ||
*/ | ||
void createProduct(ProductEventsSync productEventsSync); | ||
|
||
/** | ||
* Edit a Product from a ProductEventsSync model. | ||
* | ||
* @param productEventsSync of type ProductEventsSync | ||
*/ | ||
void editProduct(ProductEventsSync productEventsSync); | ||
|
||
/** | ||
* Delete a Product from a ProductEventsSync model. | ||
* | ||
* @param productEventsSync of type ProductEventsSync | ||
*/ | ||
void deleteProduct(ProductEventsSync productEventsSync); | ||
} |
116 changes: 116 additions & 0 deletions
116
src/main/java/ch/wisv/payments/rest/eventsync/EventsSyncProductServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package ch.wisv.payments.rest.eventsync; | ||
|
||
import ch.wisv.payments.admin.committees.CommitteeRepository; | ||
import ch.wisv.payments.exception.ProductNotFoundException; | ||
import ch.wisv.payments.model.Committee; | ||
import ch.wisv.payments.model.CommitteeEnum; | ||
import ch.wisv.payments.model.Product; | ||
import ch.wisv.payments.model.eventsync.ProductEventsSync; | ||
import ch.wisv.payments.rest.repository.ProductRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class EventsSyncProductServiceImpl implements EventsSyncProductService { | ||
|
||
/** | ||
* ProductRepository. | ||
*/ | ||
private final ProductRepository productRepository; | ||
|
||
/** | ||
* CommitteeRepository. | ||
*/ | ||
private final CommitteeRepository committeeRepository; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param productRepository of type ProductRepository | ||
* @param committeeRepository of type CommitteeRepository | ||
*/ | ||
public EventsSyncProductServiceImpl(ProductRepository productRepository, CommitteeRepository committeeRepository) { | ||
this.productRepository = productRepository; | ||
this.committeeRepository = committeeRepository; | ||
} | ||
|
||
/** | ||
* @param productEventsSync of type ProductEventsSync | ||
*/ | ||
@Override | ||
public void createProduct(ProductEventsSync productEventsSync) { | ||
Product product = new Product(); | ||
product.setKey(productEventsSync.getKey()); | ||
|
||
this.changeProductValues(product, productEventsSync); | ||
|
||
productRepository.save(product); | ||
} | ||
|
||
/** | ||
* @param productEventsSync of type ProductEventsSync | ||
*/ | ||
@Override | ||
public void editProduct(ProductEventsSync productEventsSync) { | ||
Product product = this.getProductByKey(productEventsSync.getKey()); | ||
|
||
this.changeProductValues(product, productEventsSync); | ||
|
||
productRepository.save(product); | ||
} | ||
|
||
/** | ||
* Change the Product values using a ProductEventsSync model. | ||
* | ||
* @param product of type Product. | ||
* @param productEventsSync of type ProductEventsSync. | ||
*/ | ||
private void changeProductValues(Product product, ProductEventsSync productEventsSync) { | ||
if (product.getCommittee() == null || product.getCommittee().toString().equals(productEventsSync.getOrganizedBy())) { | ||
this.changeProductCommittee(product, productEventsSync); | ||
} | ||
|
||
product.setDescription(productEventsSync.getDescription()); | ||
product.setName(productEventsSync.getTitle()); | ||
product.setPrice(productEventsSync.getPrice().floatValue()); | ||
} | ||
|
||
/** | ||
* Change the Committee of Product. | ||
* | ||
* @param product of type Product | ||
* @param productEventsSync of type ProductEventsSync | ||
*/ | ||
private void changeProductCommittee(Product product, ProductEventsSync productEventsSync) { | ||
CommitteeEnum committeeEnum = CommitteeEnum.valueOf(productEventsSync.getOrganizedBy()); | ||
int year = 2017; | ||
|
||
Committee committee = committeeRepository.findOneByNameAndYear(committeeEnum, year) | ||
.orElse(new Committee(committeeEnum, year)); | ||
|
||
if (committee.getId() == 0) { | ||
committeeRepository.save(committee); | ||
} | ||
|
||
product.setCommittee(committee); | ||
} | ||
|
||
/** | ||
* @param productEventsSync of type ProductEventsSync | ||
*/ | ||
@Override | ||
public void deleteProduct(ProductEventsSync productEventsSync) { | ||
Product product = this.getProductByKey(productEventsSync.getKey()); | ||
|
||
productRepository.delete(product.getId()); | ||
} | ||
|
||
/** | ||
* Get a Product by its key. | ||
* | ||
* @param key of type Key | ||
* @return Product | ||
*/ | ||
private Product getProductByKey(String key) { | ||
return productRepository.findOneByKey(key).orElseThrow(ProductNotFoundException::new); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/main/java/ch/wisv/payments/rest/eventsync/EventsSyncRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package ch.wisv.payments.rest.eventsync; | ||
|
||
import ch.wisv.payments.admin.products.ProductService; | ||
import ch.wisv.payments.exception.ProductNotFoundException; | ||
import ch.wisv.payments.model.eventsync.ProductEventsSync; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.validation.constraints.NotNull; | ||
import java.nio.charset.Charset; | ||
import java.util.Base64; | ||
|
||
import static ch.wisv.payments.model.eventsync.EventsSyncEnum.valueOf; | ||
import static ch.wisv.payments.util.ResponseEntityBuilder.createResponseEntity; | ||
|
||
@RestController | ||
@CrossOrigin | ||
@RequestMapping(value = "/api/chevents/sync") | ||
@Validated | ||
public class EventsSyncRestController { | ||
|
||
@Value("${events.username}") | ||
@NotNull | ||
private String username; | ||
|
||
@Value("${events.password}") | ||
@NotNull | ||
private String password; | ||
|
||
/** | ||
* ProductService. | ||
*/ | ||
private final ProductService productService; | ||
|
||
private final EventsSyncProductService eventsSyncProductService; | ||
|
||
/** | ||
* Constructor ProductService. | ||
* | ||
* @param productService of type ProductService | ||
* @param eventsSyncProductService of type EventsSyncProductService | ||
*/ | ||
public EventsSyncRestController(ProductService productService, EventsSyncProductService eventsSyncProductService) { | ||
this.productService = productService; | ||
this.eventsSyncProductService = eventsSyncProductService; | ||
} | ||
|
||
@PostMapping("/product/") | ||
public ResponseEntity<?> eventSync(HttpServletRequest request, @RequestBody ProductEventsSync productEventsSync) { | ||
String[] credentials = this.decryptBasicAuthHeader(request.getHeader("Authorization")); | ||
|
||
if (!this.authChEvents(credentials)) { | ||
return createResponseEntity(HttpStatus.UNAUTHORIZED, "User is not authorized", null); | ||
} | ||
|
||
try { | ||
switch (valueOf(productEventsSync.getTrigger())) { | ||
case PRODUCT_CREATE_EDIT: | ||
this.createOrUpdate(productEventsSync); | ||
break; | ||
case PRODUCT_DELETE: | ||
eventsSyncProductService.deleteProduct(productEventsSync); | ||
break; | ||
} | ||
} catch (IllegalArgumentException e) { | ||
return createResponseEntity(HttpStatus.BAD_REQUEST, "Events trigger not supported!", null); | ||
} | ||
|
||
return createResponseEntity(HttpStatus.OK, null, null); | ||
} | ||
|
||
/** | ||
* Check if user is CH Events. | ||
* | ||
* @param credentials of type String[] | ||
*/ | ||
private boolean authChEvents(String[] credentials) { | ||
return credentials[0].equals(this.username) && credentials[1].equals(this.password); | ||
} | ||
|
||
/** | ||
* Determine if a Product should be updated or created. | ||
* | ||
* @param productEventsSync of type ProductEventsSync | ||
*/ | ||
private void createOrUpdate(ProductEventsSync productEventsSync) { | ||
try { | ||
productService.getProductByKey(productEventsSync.getKey()); | ||
|
||
eventsSyncProductService.editProduct(productEventsSync); | ||
} catch (ProductNotFoundException e) { | ||
eventsSyncProductService.createProduct(productEventsSync); | ||
} | ||
} | ||
|
||
/** | ||
* Decrypt the Basic Auth header. | ||
* | ||
* @param basicAuth of type String | ||
* @return String[] with username and password | ||
*/ | ||
private String[] decryptBasicAuthHeader(String basicAuth) { | ||
String base64Credentials = basicAuth.substring("Basic".length()).trim(); | ||
String credentials = new String(Base64.getDecoder().decode(base64Credentials), | ||
Charset.forName("UTF-8")); | ||
|
||
return credentials.split(":", 2); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be dynamic!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue #65