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

Feature/authorization jwt #74

Closed
wants to merge 2 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
84 changes: 71 additions & 13 deletions starter_code/pom.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?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">
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>com.example</groupId>
<artifactId>auth-course</artifactId>
<groupId>com.udacity</groupId>
<artifactId>ecommerce</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>auth-course</name>
<description>Demo project for Spring Boot</description>
<version>0.0.1</version>
<name>ecommerce</name>
<description>Starter code for eCommerce project with Spring Boot</description>

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

<dependencies>
Expand All @@ -33,7 +33,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand All @@ -46,9 +45,29 @@
</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>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

Expand All @@ -59,8 +78,47 @@
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<!-- Code Coverage report generation -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>generate-code-coverage-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>xml</format>
</formats>
<check/>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
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("com.example.demo.model.persistence.repositories")
@EntityScan("com.example.demo.model.persistence")
@SpringBootApplication
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class SareetaApplication {

public static void main(String[] args) {
SpringApplication.run(SareetaApplication.class, args);
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.Optional;
import java.util.stream.IntStream;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -21,8 +24,10 @@

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


private static final Logger log = LoggerFactory.getLogger(CartController.class);
@Autowired
private UserRepository userRepository;

Expand All @@ -36,33 +41,39 @@ public class CartController {
public ResponseEntity<Cart> addTocart(@RequestBody ModifyCartRequest request) {
User user = userRepository.findByUsername(request.getUsername());
if(user == null) {
log.error("User is not found for creating cart request: user = {}", request.getUsername());
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
Optional<Item> item = itemRepository.findById(request.getItemId());
if(!item.isPresent()) {
log.error("Item id is not found for creating cart request: item 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("cart is added: cart = {}", cart);
return ResponseEntity.ok(cart);
}

@PostMapping("/removeFromCart")
public ResponseEntity<Cart> removeFromcart(@RequestBody ModifyCartRequest request) {
User user = userRepository.findByUsername(request.getUsername());
if(user == null) {
log.error("User is not found for removing cart request: user = {}", request.getUsername());
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
Optional<Item> item = itemRepository.findById(request.getItemId());
if(!item.isPresent()) {
log.error("Item id is not found for removing cart request: item 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("Cart is removed: cart = {}", cart);
return ResponseEntity.ok(cart);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,61 @@

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 org.springframework.web.bind.annotation.*;

import com.example.demo.model.persistence.Item;
import com.example.demo.model.persistence.repositories.ItemRepository;

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

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

@Autowired
private ItemRepository itemRepository;

@GetMapping
public ResponseEntity<List<Item>> getItems() {
return ResponseEntity.ok(itemRepository.findAll());
}

@PostMapping("/create")
public ResponseEntity<Item> createItem(@RequestBody Item item) {
if (item != null && item.getName() != null) {
itemRepository.save(item);
log.info("create Item successfully: item = {}", item);
return ResponseEntity.ok(item);
}
log.error("Item data is error: item = {}", item);
return ResponseEntity.badRequest().body(null);
}

@GetMapping("/{id}")
public ResponseEntity<Item> getItemById(@PathVariable Long id) {
if (!itemRepository.findById(id).isPresent()) {
log.error("Item can not be found by Id {}", id);
return ResponseEntity.notFound().build();
}
log.info("Item can be found by Id {}", id);
return ResponseEntity.of(itemRepository.findById(id));
}

@GetMapping("/name/{name}")
public ResponseEntity<List<Item>> getItemsByName(@PathVariable String name) {
List<Item> items = itemRepository.findByName(name);
return items == null || items.isEmpty() ? ResponseEntity.notFound().build()
: ResponseEntity.ok(items);

if (items == null || items.isEmpty()) {
log.error("Item can not be found by name {}", name);
return ResponseEntity.notFound().build();
}
log.info("Item can be found by name {}", name);
return ResponseEntity.ok(items);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -19,33 +22,48 @@

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


@Autowired
private UserRepository userRepository;

@Autowired
private OrderRepository orderRepository;


@PostMapping("/submit/{username}")
public ResponseEntity<UserOrder> submit(@PathVariable String username) {
User user = userRepository.findByUsername(username);
if(user == null) {
return ResponseEntity.notFound().build();
}
UserOrder order = UserOrder.createFromCart(user.getCart());
orderRepository.save(order);
return ResponseEntity.ok(order);
}

@GetMapping("/history/{username}")
public ResponseEntity<List<UserOrder>> getOrdersForUser(@PathVariable String username) {
User user = userRepository.findByUsername(username);
if(user == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(orderRepository.findByUser(user));
}


private static final Logger log = LoggerFactory.getLogger(OrderController.class);
@Autowired
private UserRepository userRepository;

@Autowired
private OrderRepository orderRepository;


@PostMapping("/submit/{username}")
public ResponseEntity<UserOrder> submit(@PathVariable String username) {
try {
User user = userRepository.findByUsername(username);
if (user == null) {
log.error("User must be registered before submitting an order");
return ResponseEntity.notFound().build();
}
UserOrder order = UserOrder.createFromCart(user.getCart());
orderRepository.save(order);
log.info("create order successfully: {}", order);
return ResponseEntity.ok(order);
} catch (RuntimeException e) {
log.error("There is an error when trying to creating an order");
return ResponseEntity.notFound().build();
}
}

@GetMapping("/history/{username}")
public ResponseEntity<List<UserOrder>> getOrdersForUser(@PathVariable String username) {
try {
User user = userRepository.findByUsername(username);
if (user == null) {
log.error("Order not found!");
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(orderRepository.findByUser(user));
} catch (RuntimeException e) {
log.error("There is an error when trying to find an order by username: {}", username);
return ResponseEntity.notFound().build();
}
}
}
Loading
Loading