Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #77

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
/starter_code/target/
/eCommerceApplication/target/
15 changes: 0 additions & 15 deletions LICENSE.md

This file was deleted.

Binary file added Metrics_Splunk_Screenshots.zip
Binary file not shown.
File renamed without changes.
File renamed without changes.
103 changes: 103 additions & 0 deletions eCommerceApplication/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>uz.jumanazar</groupId>
<artifactId>eCommerceApplication</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>eCommerceApplication</name>
<description>Final project for Java Developer Nanodegree program using Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.auth0/java-jwt -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.10.3</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/tomcat-maven-plugin -->
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uz.jumanazar.ecommerceapp;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


@EnableJpaRepositories("uz.jumanazar.ecommerceapp.model.persistence.repositories")
@EntityScan("uz.jumanazar.ecommerceapp.model.persistence")
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class ECommerceApplication {
private static final Logger log = LogManager.getLogger(ECommerceApplication.class);

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}

public static void main(String[] args) {
log.info("eCommerce Application started.");
SpringApplication.run(ECommerceApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package uz.jumanazar.ecommerceapp.controllers;

import java.util.Optional;
import java.util.stream.IntStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import uz.jumanazar.ecommerceapp.model.persistence.Cart;
import uz.jumanazar.ecommerceapp.model.persistence.Item;
import uz.jumanazar.ecommerceapp.model.persistence.User;
import uz.jumanazar.ecommerceapp.model.persistence.repositories.CartRepository;
import uz.jumanazar.ecommerceapp.model.persistence.repositories.ItemRepository;
import uz.jumanazar.ecommerceapp.model.persistence.repositories.UserRepository;
import uz.jumanazar.ecommerceapp.model.requests.ModifyCartRequest;

@RestController
@RequestMapping("/api/cart")
public class CartController {

private static final Logger log = LogManager.getLogger(CartController.class);

@Autowired
private UserRepository userRepository;

@Autowired
private CartRepository cartRepository;

@Autowired
private ItemRepository itemRepository;

@PostMapping("/addToCart")
public ResponseEntity<Cart> addTocart(@RequestBody ModifyCartRequest request) {
// log.info("[addTocart] New request for adding an item to user's cart with request body {}", request);
try {
User user = userRepository.findByUsername(request.getUsername());
if(user == null) {
log.error("[addTocart_failure] User not found with username {}", request.getUsername());
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
Optional<Item> item = itemRepository.findById(request.getItemId());
if(!item.isPresent()) {
log.error("[addTocart_failure] Item not found with ID {}", request.getItemId());
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
Cart cart = user.getCart();
IntStream.range(0, request.getQuantity())
.forEach(i -> cart.addItem(item.get()));
cartRepository.save(cart);
log.info("[addTocart_success] New item was added to user's cart with item_id:{}, " +
"item_name:{}, item_price:{}", item.get().getId(), item.get().getName(), item.get().getPrice());
return ResponseEntity.ok(cart);
}catch (Exception e){
log.error("[addTocart_failure] Item add to cart failed. Error {} ", e.getMessage());
return ResponseEntity.status(500).build();
}
}

@PostMapping("/removeFromCart")
public ResponseEntity<Cart> removeFromcart(@RequestBody ModifyCartRequest request) {
// log.info("[removeFromcart] New request for removing an item from user's cart with request body {}", request);
try {
User user = userRepository.findByUsername(request.getUsername());
if(user == null) {
log.error("[removeFromcart_failure] User not found with username {}", request.getUsername());
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
Optional<Item> item = itemRepository.findById(request.getItemId());
if(!item.isPresent()) {
log.error("[removeFromcart_failure] Item not found with ID {}", request.getItemId());
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
Cart cart = user.getCart();
IntStream.range(0, request.getQuantity())
.forEach(i -> cart.removeItem(item.get()));
cartRepository.save(cart);
log.info("[removeFromcart_success] Item was removed from user's cart with item_id:{}, " +
"item_name:{}, item_price:{}", item.get().getId(), item.get().getName(), item.get().getPrice());
return ResponseEntity.ok(cart);
}catch (Exception e){
log.error("[removeFromcart_failure] Item remove from cart failed. Error {}", e.getMessage());
return ResponseEntity.status(500).build();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
package com.example.demo.controllers;
package uz.jumanazar.ecommerceapp.controllers;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.persistence.Item;
import com.example.demo.model.persistence.repositories.ItemRepository;
import uz.jumanazar.ecommerceapp.model.persistence.Item;
import uz.jumanazar.ecommerceapp.model.persistence.repositories.ItemRepository;

@RestController
@RequestMapping("/api/item")
public class ItemController {

private static final Logger log = LogManager.getLogger(ItemController.class);

@Autowired
private ItemRepository itemRepository;

@GetMapping
public ResponseEntity<List<Item>> getItems() {
log.info("[getItems] New request for getting all items");
return ResponseEntity.ok(itemRepository.findAll());
}

@GetMapping("/{id}")
public ResponseEntity<Item> getItemById(@PathVariable Long id) {
log.info("[getItemById] New request for getting an item with ID {}", id);
return ResponseEntity.of(itemRepository.findById(id));
}

@GetMapping("/name/{name}")
public ResponseEntity<List<Item>> getItemsByName(@PathVariable String name) {
log.info("[getItemsByName] New request for getting an item by its name {}", name);
List<Item> items = itemRepository.findByName(name);
return items == null || items.isEmpty() ? ResponseEntity.notFound().build()
: ResponseEntity.ok(items);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package uz.jumanazar.ecommerceapp.controllers;

import java.util.List;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import uz.jumanazar.ecommerceapp.model.persistence.User;
import uz.jumanazar.ecommerceapp.model.persistence.UserOrder;
import uz.jumanazar.ecommerceapp.model.persistence.repositories.OrderRepository;
import uz.jumanazar.ecommerceapp.model.persistence.repositories.UserRepository;

@RestController
@RequestMapping("/api/order")
public class OrderController {

private static final Logger log = LogManager.getLogger(OrderController.class);

@Autowired
private UserRepository userRepository;

@Autowired
private OrderRepository orderRepository;


@PostMapping("/submit/{username}")
public ResponseEntity<UserOrder> submit(@PathVariable String username) {
// log.info("[order_submit] New request for order submissing for user {}", username);
try {
User user = userRepository.findByUsername(username);
if(user == null) {
log.error("[order_submit_failure] User not found with username {}", username);
return ResponseEntity.notFound().build();
}
UserOrder order = UserOrder.createFromCart(user.getCart());
orderRepository.save(order);
log.info("[order_submit_success] User {}'s order was successfully submitted with details {}", username, order);
return ResponseEntity.ok(order);
}catch (Exception e){
log.error("[order_submit_failure] User not found with username {}, Error: {}", username, e.getMessage());
return ResponseEntity.status(500).build();
}
}

@GetMapping("/history/{username}")
public ResponseEntity<List<UserOrder>> getOrdersForUser(@PathVariable String username) {
// log.info("[getOrdersForUser] New request getting orders for user {}", username);
User user = userRepository.findByUsername(username);
if(user == null) {
log.error("[getOrdersForUser_failure] User not found with username {}", username);
return ResponseEntity.notFound().build();
}
log.info("[getOrdersForUser_success] New request getting orders for user {}", username);
return ResponseEntity.ok(orderRepository.findByUser(user));
}
}
Loading
Loading