Skip to content

Commit

Permalink
Merge pull request #3 from Petrovich-A/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tmspavel committed Jul 6, 2023
2 parents 35ce87b + c015aac commit 45fd663
Show file tree
Hide file tree
Showing 71 changed files with 1,215 additions and 689 deletions.
36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<div align="center">
<h1>E-shop app</h1>

![e-shop-logo](src/main/resources/static/img/logo_readme.png)

<!-- Badges -->
<p>
<a>
<a>
<img alt="GitHub last commit (branch)" src="https://img.shields.io/github/last-commit/Petrovich-A/E-shop/develop?style=for-the-badge">
</a>
</a>
<a>
<img alt="GitHub commit activity (branch)" src="https://img.shields.io/github/commit-activity/m/Petrovich-A/E-shop/develop?style=for-the-badge">
</a>
Expand Down Expand Up @@ -43,9 +45,7 @@ by providing a user-friendly interface and incorporating the latest Java technol

### :camera: Screenshots

<div>
<img src="https://github.com/Petrovich-A/E-shop/blob/develop/main_page.png" alt="Alt text" title="Optional title">
</div>
![profile page](src/main/resources/static/img/profile_page.png)

<!-- TechStack -->

Expand All @@ -55,11 +55,16 @@ by providing a user-friendly interface and incorporating the latest Java technol
<summary>Show</summary>
<ul>
<li><a>Java 17</a></li>
<li><a>Spring boot</a></li>
<li><a>Spring 3.0.6 (Spring boot, Spring Security, Spring web)</a></li>
<li><a>Maven 3.8.1</a></li>
<li><a>Postgresql 42.6.0</a></li>
<li><a>Lombok 1.18.24</a></li>
<li><a>Modelmapper 3.1.1</a></li>
<li><a>FlyWay 9.19.1</a></li>
<li><a>ThymeLeaf 3.0.6</a></li>
<li><a>Hibernate 6.1.7</a></li>
<li><a>Spring data JPA 3.0.5</a></li>
<li><a>JUnit 5</a></li>
</ul>
</details>

Expand All @@ -78,13 +83,10 @@ Clone the project
git clone https://github.com/Petrovich-A/E-shop.git
```

Go to the project directory and `run` this [script](https://github.com/Petrovich-A/E-shop/blob/develop/src/main/resources/script/create_and_populate_tables)
to populate the database tables.

<!-- Database struct -->
### :key: Database structure

![DB_structure](src/main/resources/static/styles/img/DB_structure.png)
![DB_structure](src/main/resources/static/img/DB_structure.png)

<!-- Roadmap -->
## :compass: roadmap:
Expand All @@ -102,6 +104,10 @@ to populate the database tables.
* [x] add orders storing to DB
* [x] display user's order history
* [x] add the ability to view the list of products of each user order
* [x] add admin page with user's list
* [x] add Spring Security with In-Memory Authentication
* [x] add Spring Security authentication form Login custom page with Database
* [x] add Spring Security roles

## :handshake: Contact

Expand All @@ -111,9 +117,11 @@ Petrovich Alexandr - [@Petrovich Alexandr](https://www.linkedin.com/in/alexandr-

## :gem: Acknowledgements

Use this section to mention useful resources and libraries that you have used in your projects.
Here are useful resources for readme writing and designing that I've used in my projects.

- [Shields.io](https://shields.io/)
- [Awesome README](https://github.com/matiassingers/awesome-readme)
- [Emoji Cheat Sheet](https://github.com/ikatyang/emoji-cheat-sheet/blob/master/README.md#travel--places)
- [Readme Template](https://github.com/othneildrew/Best-README-Template)
- [Templates for UI](https://www.w3schools.com/howto/default.asp)
- [Git Emoji for your commit messages](https://gitmoji.dev)
- [Readme Template](https://github.com/othneildrew/Best-README-Template)
- [Technical support](https://www.baeldung.com)
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@
<artifactId>flyway-core</artifactId>
<version>9.19.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<build>
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/by/petrovich/eshop/EShopApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
@SpringBootApplication
@RestController
public class EShopApplication {

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

@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
return modelMapper;
return new ModelMapper();
}

}
16 changes: 16 additions & 0 deletions src/main/java/by/petrovich/eshop/PageName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package by.petrovich.eshop;

public class PageName {
public static final String HOME_PAGE = "home";
public static final String CATEGORY_PAGE = "category";
public static final String LOGIN_PAGE = "login";
public static final String REGISTRATION_PAGE = "registration";
public static final String PRODUCT_PAGE = "product";
public static final String PROFILE_PAGE = "profile";
public static final String CART_PAGE = "cart";
public static final String ORDER_HISTORY_PAGE = "order_history";
public static final String ORDER_PAGE = "order";
public static final String ADMIN_PAGE = "admin";
public static final String ERROR_PAGE = "error";

}
24 changes: 0 additions & 24 deletions src/main/java/by/petrovich/eshop/PathToPage.java

This file was deleted.

50 changes: 50 additions & 0 deletions src/main/java/by/petrovich/eshop/config/WebSecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package by.petrovich.eshop.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = "by")
public class WebSecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeHttpRequests(requests -> requests
.requestMatchers("/javaScript/**", "/img/**", "/CSS/**").permitAll()
.requestMatchers("/", "/home", "/registration", "/users/registrate", "/login").permitAll()
.requestMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated()
)
.formLogin(form -> form
.loginPage("/login")
.loginProcessingUrl("/login")
.usernameParameter("username")
.passwordParameter("password")
.defaultSuccessUrl("/home", true)
.permitAll()
)
.logout(logout -> logout
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.invalidateHttpSession(true)
.clearAuthentication(true)
.permitAll()
);
return http.build();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

}
35 changes: 0 additions & 35 deletions src/main/java/by/petrovich/eshop/controllers/AuthController.java

This file was deleted.

60 changes: 28 additions & 32 deletions src/main/java/by/petrovich/eshop/controllers/CartController.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package by.petrovich.eshop.controllers;

import by.petrovich.eshop.entity.Cart;
import by.petrovich.eshop.dto.CartDto;
import by.petrovich.eshop.entity.Order;
import by.petrovich.eshop.entity.User;
import by.petrovich.eshop.service.CartService;
import by.petrovich.eshop.service.OrderService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
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.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import static by.petrovich.eshop.PathToPage.CART_PAGE;
import static by.petrovich.eshop.PathToPage.HOME_PAGE;
import static by.petrovich.eshop.PageName.CART_PAGE;
import static by.petrovich.eshop.PageName.HOME_PAGE;

@Controller
@SessionAttributes({"cart", "user"})
@SessionAttributes({"cartDto", "user"})
@RequestMapping("/cart")
public class CartController {
private final CartService cartService;
Expand All @@ -32,68 +32,64 @@ public CartController(CartService cartService, OrderService orderService) {
this.orderService = orderService;
}

@ModelAttribute("cart")
public Cart Cart() {
return new Cart();
@ModelAttribute("cartDto")
public CartDto initializeCartSessionObject() {
return new CartDto();
}

@ModelAttribute("user")
public User User() {
public User initializeUserSessionObject() {
return new User();
}

@GetMapping("/redirectToCartPage")
public ModelAndView showCartPage() {
return new ModelAndView(CART_PAGE.getPath());
}

@PostMapping("/add/{productId}")
public ModelAndView addProductToCart(@PathVariable("productId") String productId,
@ModelAttribute("cart") Cart cart) {
@Valid @ModelAttribute CartDto cartDto) {
ModelMap modelParams = new ModelMap();
if (productId != null) {
Integer id = Integer.parseInt(productId);
modelParams.addAttribute("cart", cartService.addProduct(id, cart));
} else {
return new ModelAndView(HOME_PAGE.getPath(), modelParams);
modelParams.addAttribute("cartDto", cartService.addProduct(id, cartDto));
return new ModelAndView(CART_PAGE, modelParams);
}
return new ModelAndView(CART_PAGE.getPath(), modelParams);
return new ModelAndView(HOME_PAGE, modelParams);
}

@PostMapping("/remove/{productId}")
public ModelAndView removeProductFromCart(@PathVariable("productId") String productId,
@ModelAttribute("cart") Cart cart) {
@Valid @ModelAttribute("cartDto") CartDto cartDto) {
ModelMap modelParams = new ModelMap();
if (productId != null) {
Integer id = Integer.parseInt(productId);
modelParams.addAttribute("cart", cartService.removeProduct(id, cart));
modelParams.addAttribute("cartDto", cartService.removeProduct(id, cartDto));
return new ModelAndView(CART_PAGE, modelParams);
}
return new ModelAndView(CART_PAGE.getPath(), modelParams);
return new ModelAndView(HOME_PAGE, modelParams);
}

@PostMapping("/clear")
public ModelAndView clearCart(@ModelAttribute("cart") Cart cart) {
public ModelAndView clearCart(@ModelAttribute("cartDto") CartDto cartDto) {
ModelMap modelParams = new ModelMap();
clearCart(cart, modelParams);
return new ModelAndView(CART_PAGE.getPath(), modelParams);
clearCart(cartDto, modelParams);
return new ModelAndView(CART_PAGE, modelParams);
}

@PostMapping("/order/{userId}")
public ModelAndView order(@ModelAttribute("cart") Cart cart,
@PathVariable("userId") String userId) {
public ModelAndView saveOrder(@ModelAttribute("cartDto") CartDto cartDto,
@PathVariable String userId) {
ModelMap modelParams = new ModelMap();
if (cart != null && userId != null) {
if (cartDto != null && userId != null) {
Integer id = Integer.valueOf(userId);
Order savedCart = orderService.save(cart, id);
Order savedCart = orderService.save(cartDto, id);
if (savedCart != null) {
clearCart(cart, modelParams);
clearCart(cartDto, modelParams);
}
}
return new ModelAndView(CART_PAGE.getPath(), modelParams);
return new ModelAndView(CART_PAGE, modelParams);
}

private void clearCart(Cart cart, ModelMap modelParams) {
modelParams.addAttribute("cart", cartService.clear(cart));
private void clearCart(CartDto cartDto, ModelMap modelParams) {
modelParams.addAttribute("cartDto", cartService.clear(cartDto));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static by.petrovich.eshop.PathToPage.CATEGORY_PAGE;
import static by.petrovich.eshop.PageName.CATEGORY_PAGE;

@Validated
@RestController
Expand Down Expand Up @@ -49,7 +49,7 @@ public ModelAndView showCategoryPage(
.collect(Collectors.toList());
model.addAttribute("pageNumbers", pageNumbers);
}
return new ModelAndView(CATEGORY_PAGE.getPath(), model);
return new ModelAndView(CATEGORY_PAGE, model);
}

}
Loading

0 comments on commit 45fd663

Please sign in to comment.