Skip to content

Commit

Permalink
Merge pull request #2 from majimearun/local-hosting
Browse files Browse the repository at this point in the history
final updates from local host to hosted server
  • Loading branch information
majimearun authored Dec 7, 2022
2 parents 1c66012 + 13ffe47 commit 9c0e77c
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 97 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# gomart

E commerce backend built using Spring Boot
E-commerce backend built using Spring Boot

Current API: gomart-production.up.railway.app

Frontend: gomart.vercel.app

By default admin is added every time
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import java.util.List;

@RestController
@CrossOrigin(origins = "https://gomart.vercel.app/")
// @CrossOrigin(origins = "https://gomart.vercel.app/")
@CrossOrigin
@RequestMapping(path = "/admin")
public class AdminController {

Expand Down Expand Up @@ -80,4 +81,9 @@ public List<SendCart> generateReportByDate(@RequestBody GetOrder getOrder){
return adminService.getItemsSoldOnADate(getOrder.getSenderId(), getOrder.getStartDate());
}

@PostMapping(path="/delete/user")
public void deleteUser(@RequestBody GetInfo getInfo){
adminService.deleteUser(getInfo.getSenderId(), getInfo.getUserId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.ecommerce.gomart.GomartUser.Manager.ManagerService;
import com.ecommerce.gomart.GomartUser.Manager.ManagerStatus;
import com.ecommerce.gomart.GomartUser.Role;
import com.ecommerce.gomart.Order.CustomerSnapshot;
import com.ecommerce.gomart.Order.Order;
import com.ecommerce.gomart.Order.OrderRepository;
import com.ecommerce.gomart.Order.ProductSnapshot;
import com.ecommerce.gomart.Product.Product;
import com.ecommerce.gomart.Product.ProductRepository;
import com.ecommerce.gomart.Stubs.SendCart;
Expand All @@ -23,7 +25,10 @@

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -79,9 +84,18 @@ public void removeManagerAccess(Long adminId, Long userId){
public List<SendOrder> getOrdersOfCustomerInDateRange(Long adminId, Long userId, LocalDate startDate, LocalDate endDate){
if(checkAdminStatus(adminId)){
GomartUser user = gomartUserRepository.findById(userId).get();
List<Order> orders = orderRepository.findByCustomerAndOrderDateBetween(user, startDate, endDate);
List<SendOrder> send = orders.stream().map(order -> new SendOrder(order.getOrderTransactionId(), makeSnapshotProduct(order.getProduct(), order), order.getQuantity(), order.getOrderDate())).collect(Collectors.toList());
return send;
CustomerSnapshot customerSnapshot = new CustomerSnapshot().builder()
.userId(user.getUserId())
.firstName(user.getFirstName())
.lastName(user.getLastName())
.email(user.getEmail())
.phoneNumber(user.getPhoneNumber())
.address(user.getAddress())
.build();
List<Order> orders = orderRepository.findByCustomerAndOrderDateBetween(customerSnapshot, startDate, endDate);
List<SendOrder> send = orders.stream().map(order -> new SendOrder(order.getOrderTransactionId(), order.getProduct(), order.getQuantity(), order.getOrderDate())).collect(Collectors.toList());
// reverse the list so that the most recent order is at the top
return send.stream().sorted((o1, o2) -> o2.getOrderDate().compareTo(o1.getOrderDate())).collect(Collectors.toList());
}
else{
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User does not have Admin level access or is not logged in");
Expand All @@ -93,6 +107,7 @@ public List<SendOrder> getOrdersOfCustomerInDateRange(Long adminId, Long userId,
public List<UserInfo> getCustomers(Long adminId){
if(checkAdminStatus(adminId)){
List<GomartUser> users = gomartUserRepository.findAll();
users.removeIf(user -> user.getRole() == Role.ADMIN);
List<UserInfo> send = users.stream().map(user -> new UserInfo(user.getUserId(), user.getFirstName(), user.getMiddleName(), user.getLastName(), user.getEmail(), user.getDob(), user.getAddress(), user.getPhoneNumber())).collect(Collectors.toList());
return send;
}
Expand Down Expand Up @@ -130,25 +145,55 @@ public List<UserInfo> getPendingManagers(Long adminId){
public List<SendCart> getItemsSoldOnADate(Long adminId, LocalDate date){
if(checkAdminStatus(adminId)){
List<Order> orders = orderRepository.findByOrderDate(date);
List<SendCart> send = orders.stream().map(order -> new SendCart(makeSnapshotProduct(order.getProduct(), order), order.getQuantity())).collect(Collectors.toList());
Map<ProductSnapshot, Integer> productToQuantity = new HashMap<>();
for(Order order : orders){
if(productToQuantity.containsKey(order.getProduct())){
productToQuantity.put(order.getProduct(), productToQuantity.get(order.getProduct()) + order.getQuantity());
}
else{
productToQuantity.put(order.getProduct(), order.getQuantity());
}
}
List<SendCart> send = new ArrayList<>();
for(Map.Entry<ProductSnapshot, Integer> entry : productToQuantity.entrySet()){
send.add(new SendCart(snapshotToProduct(entry.getKey()), entry.getValue()));
}
return send;

}
else{
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User does not have Admin level access or is not logged in");
}

}
private Product makeSnapshotProduct(Product product, Order order){
Product snapshotProduct = new Product();
snapshotProduct.setProductId(product.getProductId());
snapshotProduct.setName(order.getProductNameSnapshot());
snapshotProduct.setCategory(product.getCategory());
snapshotProduct.setPrice(order.getProductPriceSnapshot());
snapshotProduct.setQuantity(product.getQuantity());
snapshotProduct.setOffer(order.getProductOfferSnapshot());
snapshotProduct.setDeliveryTime(product.getDeliveryTime());
snapshotProduct.setImage(product.getImage());
return snapshotProduct;

@Transactional
public void deleteUser(Long adminId, Long userId){
if(checkAdminStatus(adminId)){
GomartUser user = gomartUserRepository.findById(userId).get();
if(user.getRole() == Role.ADMIN){
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Cannot delete an Admin");
}
else{
Email email = new Email(user.getEmail(), "Account Deleted", "Your account has been deletedby the Admin. Please contact the Admin for more information.");
emailService.sendSimpleMail(email);
gomartUserRepository.delete(user);
}
}
else{
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User does not have Admin level access or is not logged in");
}
}

private Product snapshotToProduct(ProductSnapshot productSnapshot){
Product product = new Product();
product.setName(productSnapshot.getName());
product.setPrice(productSnapshot.getPrice());
product.setOffer(productSnapshot.getOffer());
product.setImage(productSnapshot.getImage());
product.setDeliveryTime(productSnapshot.getDeliveryTime());
return product;


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import java.util.List;

@RestController
@CrossOrigin(origins = "https://gomart.vercel.app/")
// @CrossOrigin(origins = "https://gomart.vercel.app/")
@CrossOrigin
@RequestMapping(path = "/user")
public class CustomerController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import com.ecommerce.gomart.GomartUser.Manager.Manager;
import com.ecommerce.gomart.GomartUser.Manager.ManagerStatus;
import com.ecommerce.gomart.GomartUser.Role;
import com.ecommerce.gomart.Order.CustomerSnapshot;
import com.ecommerce.gomart.Order.Order;
import com.ecommerce.gomart.Order.OrderRepository;
import com.ecommerce.gomart.Order.ProductSnapshot;
import com.ecommerce.gomart.Product.Category;
import com.ecommerce.gomart.Product.Product;
import com.ecommerce.gomart.Product.ProductRepository;
Expand Down Expand Up @@ -138,13 +140,28 @@ public List<Product> getProductsByCategory(int category) {
public List<Product> getProductsByName(String name) {
List<Product> products = productRepository.findByNameIgnoreCaseContaining(name);
if(products.isEmpty()){
return getProductsByFuzzyName(name);
List<Product> productsByDescription = productRepository.findByDescriptionIgnoreCaseContaining(name);

if(productsByDescription.isEmpty()){
List<Product> fuzzyName = getProductsByFuzzyName(name);
if(fuzzyName.isEmpty()){
return getProductsByFuzzyDescription(name);
}
else{
return fuzzyName;
}
}
else{
return productsByDescription;
}
}
else{
return products;
}
return products;
}

@Transactional
public List<Product> getProductsByFuzzyName(String name) {
private List<Product> getProductsByFuzzyName(String name) {
List<Product> products = getProducts();
List<Product> filtered = products.stream()
.filter(product -> FuzzySearch.weightedRatio(product.getName(), name) > 50)
Expand All @@ -155,6 +172,18 @@ public List<Product> getProductsByFuzzyName(String name) {
return fProducts.subList(0, Math.min(5, fProducts.size()));
}

@Transactional
private List<Product> getProductsByFuzzyDescription(String name) {
List<Product> products = getProducts();
List<Product> filtered = products.stream()
.filter(product -> FuzzySearch.weightedRatio(product.getDescription(), name) > 50)
.collect(Collectors.toList());
List<Product> fProducts = filtered.stream()
.sorted((p1, p2) -> FuzzySearch.weightedRatio(p2.getDescription(), name) - FuzzySearch.weightedRatio(p1.getDescription(), name))
.collect(Collectors.toList());
return fProducts.subList(0, Math.min(5, fProducts.size()));
}

@Transactional
public List<Product> getProductsInCategoryByPriceRange(int category, double min, double max) {
return productRepository.findByCategoryAndPriceBetween(Category.values()[category], min, max);
Expand Down Expand Up @@ -191,36 +220,40 @@ public List<SendCart> getCart(Long userId){
public List<SendOrder> getOrders(Long userId){
if(checkIfUserLoggedIn(userId)){
GomartUser user = gomartUserRepository.findById(userId).get();
List<Order> orders = orderRepository.findByCustomer(user);
List<SendOrder> send = orders.stream().map(order -> new SendOrder(order.getOrderTransactionId(),makeSnapshotProduct(order.getProduct(), order), order.getQuantity(), order.getOrderDate())).collect(Collectors.toList());
return send;
CustomerSnapshot customerSnapshot = new CustomerSnapshot().builder()
.userId(user.getUserId())
.firstName(user.getFirstName())
.lastName(user.getLastName())
.email(user.getEmail())
.phoneNumber(user.getPhoneNumber())
.address(user.getAddress())
.build();
List<Order> orders = orderRepository.findByCustomer(customerSnapshot);
List<SendOrder> send = orders.stream().map(order -> new SendOrder(order.getOrderTransactionId(),order.getProduct(), order.getQuantity(), order.getOrderDate())).collect(Collectors.toList());
return send.stream().sorted((o1, o2) -> o2.getOrderDate().compareTo(o1.getOrderDate())).collect(Collectors.toList());
}
else{
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User not logged in");
}
}

private Product makeSnapshotProduct(Product product, Order order){
Product snapshotProduct = new Product();
snapshotProduct.setProductId(product.getProductId());
snapshotProduct.setName(order.getProductNameSnapshot());
snapshotProduct.setCategory(product.getCategory());
snapshotProduct.setPrice(order.getProductPriceSnapshot());
snapshotProduct.setQuantity(product.getQuantity());
snapshotProduct.setOffer(order.getProductOfferSnapshot());
snapshotProduct.setDeliveryTime(product.getDeliveryTime());
snapshotProduct.setImage(product.getImage());
return snapshotProduct;

}

@Transactional
public List<SendOrder> getOrdersByOrderDate(Long userId, LocalDate date){
if(checkIfUserLoggedIn(userId)){
GomartUser user = gomartUserRepository.findById(userId).get();
List<Order> orders = orderRepository.findByCustomerAndOrderDate(user, date);
CustomerSnapshot customerSnapshot = new CustomerSnapshot().builder()
.userId(user.getUserId())
.firstName(user.getFirstName())
.lastName(user.getLastName())
.email(user.getEmail())
.phoneNumber(user.getPhoneNumber())
.address(user.getAddress())
.build();
List<Order> orders = orderRepository.findByCustomerAndOrderDate(customerSnapshot, date);
List<SendOrder> send = orders.stream().map(order -> new SendOrder(order.getOrderTransactionId(),order.getProduct(), order.getQuantity(), order.getOrderDate())).collect(Collectors.toList());
return send;
// reverse the list to get the latest order first
return send.stream().sorted((o1, o2) -> o2.getOrderDate().compareTo(o1.getOrderDate())).collect(Collectors.toList());
}
else{
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User not logged in");
Expand All @@ -231,9 +264,18 @@ public List<SendOrder> getOrdersByOrderDate(Long userId, LocalDate date){
public List<SendOrder> getOrdersByOrderDateRange(Long userId, LocalDate startDate, LocalDate endDate){
if(checkIfUserLoggedIn(userId)){
GomartUser user = gomartUserRepository.findById(userId).get();
List<Order> orders = orderRepository.findByCustomerAndOrderDateBetween(user, startDate, endDate);
CustomerSnapshot customerSnapshot = new CustomerSnapshot().builder()
.userId(user.getUserId())
.firstName(user.getFirstName())
.lastName(user.getLastName())
.email(user.getEmail())
.phoneNumber(user.getPhoneNumber())
.address(user.getAddress())
.build();
List<Order> orders = orderRepository.findByCustomerAndOrderDateBetween(customerSnapshot, startDate, endDate);
List<SendOrder> send = orders.stream().map(order -> new SendOrder(order.getOrderTransactionId(),order.getProduct(), order.getQuantity(), order.getOrderDate())).collect(Collectors.toList());
return send;
// reverse the list so that the most recent order is at the top
return send.stream().sorted((o1, o2) -> o2.getOrderDate().compareTo(o1.getOrderDate())).collect(Collectors.toList());

}
else{
Expand Down Expand Up @@ -262,8 +304,14 @@ public ResponseEntity<String> updateUserInfo(Long userId, String firstName, Stri

public ResponseEntity<String> deleteUserInfo(Long userId){
if(checkIfUserLoggedIn(userId)){
Email email = new Email();
email.setTo(gomartUserRepository.findById(userId).get().getEmail());
email.setSubject("Account Deletion");
email.setBody("Your GoMart account has been deleted successfully. We're sorry to see you go.");
emailService.sendSimpleMail(email);
gomartUserRepository.deleteById(userId);
return new ResponseEntity<String>("User info deleted successfully", HttpStatus.OK);

}
else{
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User not logged in");
Expand Down Expand Up @@ -365,14 +413,24 @@ public ResponseEntity<String> checkOutFromCart(Long userId){
}
for(Cart cart: cartList){
Order order = new Order().builder()
.customer(cart.getCustomer())
.product(cart.getProduct())
.quantity(cart.getQuantity())
.orderDate(LocalDate.now())
.productNameSnapshot(cart.getProduct().getName())
.productPriceSnapshot(cart.getProduct().getPrice())
.productOfferSnapshot(cart.getProduct().getOffer())
.build();
CustomerSnapshot customerSnapshot = new CustomerSnapshot();
ProductSnapshot productSnapshot = new ProductSnapshot();
customerSnapshot.setAddress(user.getAddress());
customerSnapshot.setEmail(user.getEmail());
customerSnapshot.setPhoneNumber(user.getPhoneNumber());
customerSnapshot.setFirstName(user.getFirstName());
customerSnapshot.setLastName(user.getLastName());
customerSnapshot.setUserId(user.getUserId());
order.setCustomer(customerSnapshot);
productSnapshot.setName(cart.getProduct().getName());
productSnapshot.setPrice(cart.getProduct().getPrice());
productSnapshot.setOffer(cart.getProduct().getOffer());
productSnapshot.setImage(cart.getProduct().getImage());
productSnapshot.setDeliveryTime(cart.getProduct().getDeliveryTime());
order.setProduct(productSnapshot);
orderRepository.save(order);
cartRepository.deleteById(cart.getEntryId());
// decrease quantity of product
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/ecommerce/gomart/GomartUser/GomartUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ public class GomartUser {
@JsonIgnore
private List<Cart> carts;

// one to many relationship with orders
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
@JsonIgnore
private List<Order> orders;


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import java.io.IOException;

@RestController
@CrossOrigin(origins = "https://gomart.vercel.app/")
// @CrossOrigin(origins = "https://gomart.vercel.app/")
@CrossOrigin
@RequestMapping(path = "/manager")
public class ManagerController {
private final ManagerService managerService;
Expand Down
Loading

0 comments on commit 9c0e77c

Please sign in to comment.