-
Notifications
You must be signed in to change notification settings - Fork 1
Remove and edit producs groups #40
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
package ch.wisv.payments.admin.products; | ||
|
||
import ch.wisv.payments.admin.committees.CommitteeRepository; | ||
import ch.wisv.payments.admin.committees.CommitteeService; | ||
import ch.wisv.payments.admin.products.request.ProductGroupRequest; | ||
import ch.wisv.payments.admin.products.request.ProductRequest; | ||
import ch.wisv.payments.exception.CommmitteeNotFoundException; | ||
import ch.wisv.payments.exception.ProductGroupInUseException; | ||
import ch.wisv.payments.exception.ProductInUseException; | ||
import ch.wisv.payments.model.*; | ||
import ch.wisv.payments.rest.OrderService; | ||
import ch.wisv.payments.rest.repository.ProductGroupRepository; | ||
import ch.wisv.payments.rest.repository.ProductRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
@@ -20,14 +19,13 @@ public class ProductServiceImpl implements ProductService { | |
|
||
private ProductRepository productRepository; | ||
private ProductGroupRepository productGroupRepository; | ||
private CommitteeRepository committeeRepository; | ||
private CommitteeService committeeService; | ||
private OrderService orderService; | ||
|
||
@Autowired | ||
public ProductServiceImpl(ProductRepository productRepository, ProductGroupRepository productGroupRepository, CommitteeRepository committeeRepository, OrderService orderService) { | ||
public ProductServiceImpl(ProductRepository productRepository, ProductGroupRepository productGroupRepository, CommitteeService committeeService, OrderService orderService) { | ||
this.productRepository = productRepository; | ||
this.productGroupRepository = productGroupRepository; | ||
this.committeeRepository = committeeRepository; | ||
this.committeeService = committeeService; | ||
this.orderService = orderService; | ||
} | ||
|
||
|
@@ -38,7 +36,7 @@ public List<Product> getAllProducts() { | |
|
||
@Override | ||
public void addProduct(ProductRequest productRequest) { | ||
Committee committee = committeeRepository.findOne(productRequest.getCommitteeId()); | ||
Committee committee = committeeService.getCommitteeById(productRequest.getCommitteeId()); | ||
Product product = new Product(committee, | ||
productRequest.getName(), | ||
productRequest.getDescription(), | ||
|
@@ -56,7 +54,7 @@ public void addProduct(ProductRequest productRequest) { | |
|
||
@Override | ||
public void addProductGroup(ProductGroupRequest productGroupRequest) { | ||
Committee committee = committeeRepository.findOne(productGroupRequest.getCommitteeId()); | ||
Committee committee = committeeService.getCommitteeById(productGroupRequest.getCommitteeId()); | ||
ProductGroup group = new ProductGroup(productGroupRequest.getName(), | ||
productGroupRequest.getDescription(), productGroupRequest.getGroupLimit(), committee); | ||
|
||
|
@@ -79,9 +77,9 @@ public void addProductToGroup(Product product, ProductGroup productGroup) { | |
|
||
@Override | ||
public void editProduct(ProductRequest productRequest) { | ||
if (productRequest.getProductId() != 0) { | ||
Committee committee = committeeRepository.findOne(productRequest.getCommitteeId()); | ||
Product product = productRepository.findOne(productRequest.getProductId()); | ||
if (productRequest.getId() != 0) { | ||
Committee committee = committeeService.getCommitteeById(productRequest.getCommitteeId()); | ||
Product product = productRepository.findOne(productRequest.getId()); | ||
|
||
product.setName(productRequest.getName()); | ||
product.setDescription(productRequest.getDescription()); | ||
|
@@ -102,15 +100,25 @@ public void editProduct(ProductRequest productRequest) { | |
} | ||
} | ||
|
||
@Override | ||
public void deleteProduct(int productId) { | ||
List<Order> orders = orderService.getOrdersByProductId(productId); | ||
|
||
if (orders.size() > 0) { | ||
throw new ProductInUseException("Products are already ordered"); | ||
} else { | ||
productRepository.delete(productId); | ||
} | ||
} | ||
|
||
@Override | ||
public Product getProductById(Integer productId) { | ||
return productRepository.findOne(productId); | ||
} | ||
|
||
@Override | ||
public Set<Product> getProductByCommittee(CommitteeEnum committeeEnum, int year) { | ||
Committee committee = committeeRepository.findOneByNameAndYear(committeeEnum, year) | ||
.orElseThrow(CommmitteeNotFoundException::new); | ||
Committee committee = committeeService.getCommittee(committeeEnum, year); | ||
|
||
return productRepository.findByCommittee(committee); | ||
} | ||
|
@@ -130,13 +138,32 @@ public boolean isProductAvailable(Integer productId) { | |
} | ||
|
||
@Override | ||
public void deleteProduct(int productId) { | ||
List<Order> orders = orderService.getOrdersByProductId(productId); | ||
public ProductGroup getProductGroupById(Integer productGroupId) { | ||
return productGroupRepository.findOne(productGroupId); | ||
} | ||
|
||
if (orders.size() > 0) { | ||
throw new ProductInUseException("Products are already ordered"); | ||
@Override | ||
public void editProductGroup(ProductGroupRequest productGroupRequest) { | ||
if (productGroupRequest.getId() != 0) { | ||
Committee committee = committeeService.getCommitteeById(productGroupRequest.getCommitteeId()); | ||
ProductGroup productGroup = productGroupRepository.findOne(productGroupRequest.getId()); | ||
|
||
productGroup.setCommittee(committee); | ||
productGroup.setName(productGroupRequest.getName()); | ||
productGroup.setDescription(productGroupRequest.getDescription()); | ||
productGroup.setGroupLimit(productGroupRequest.getGroupLimit()); | ||
|
||
productGroupRepository.saveAndFlush(productGroup); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteProductGroup(int productGroupId) { | ||
ProductGroup productGroup = productGroupRepository.findOne(productGroupId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Servicecall here too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So do we want to make a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can put that in one service. Once we need more functionality that requires a lot more methods, we'll separate them. |
||
if (!productGroup.getProducts().isEmpty()) { | ||
throw new ProductGroupInUseException("Product group must be empty"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really the case? What would break if you remove a group with products in it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had previously implemented it in a way where products were removed from the group when the group was removed, but this may be an action where we don't want it to be very easy to perform. I'm also not quite sure which way to go, it made sense to me that since there is a limit involved, you don't want it to be this easy, just like removing a product that is already ordered. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, then perhaps add a section to the editGroup page where you can remove products currently in the group? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also add the removal of tickets from a group as a hoothub issue, since this is not very high prioroty. It is also not relevant to editing the product group, it would make more sense to have it in a different page. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave it like this for now, and create a HootHub issue to implement this nicely. |
||
} else { | ||
productRepository.delete(productId); | ||
productGroupRepository.delete(productGroupId); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package ch.wisv.payments.exception; | ||
|
||
public class CommitteeNotFoundException extends RuntimeException { | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package ch.wisv.payments.exception; | ||
|
||
public class EmptyOrderException extends RuntimeException { | ||
|
||
public EmptyOrderException(String s) { | ||
super(s); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ch.wisv.payments.exception; | ||
|
||
public class ProductGroupInUseException extends RuntimeException { | ||
public ProductGroupInUseException(String s) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This constructor is already in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You unfortunately have to do this to make use of the |
||
super(s); | ||
} | ||
} |
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.
Why this if?
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.
It was also in the
editProduct()
method, could you shed some light on this?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.
Hmm, probably because the id's are
int
, which defaults to 0 instead ofnull