Skip to content

Commit

Permalink
Merge pull request #4 from Petrovich-A/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tmspavel committed Jul 12, 2023
2 parents 45fd663 + 47baa10 commit 91e64a7
Show file tree
Hide file tree
Showing 62 changed files with 585 additions and 367 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* [Run Locally](#running-run-locally)
* [Database structure](#key-database-structure)
- [Roadmap](#compass-roadmap)
- [Layout color palette](#art-color)
- [Contact](#handshake-contact)
- [Acknowledgements](#gem-acknowledgements)

Expand Down Expand Up @@ -108,6 +109,17 @@ Clone the project
* [x] add Spring Security with In-Memory Authentication
* [x] add Spring Security authentication form Login custom page with Database
* [x] add Spring Security roles
* [x] add Spring Security's remember me feature
* [x] add images for according products

<!-- Color -->
## :art: color:

| HEX | RGB |
|-----|----------------------|
| #04AA6D | rgb: (4, 170, 109) |
| #1ad18c | rgb: (26, 209, 140) |
| #e9e9e9 | rgb: (233, 233, 233) |

## :handshake: Contact

Expand Down
8 changes: 0 additions & 8 deletions src/main/java/by/petrovich/eshop/EShopApplication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package by.petrovich.eshop;

import org.modelmapper.ModelMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
Expand All @@ -12,10 +10,4 @@ public class EShopApplication {
public static void main(String[] args) {
SpringApplication.run(EShopApplication.class, args);
}

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

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

import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConfigBean {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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.Customizer;
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;
Expand All @@ -12,7 +12,6 @@

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

@Bean
Expand All @@ -38,7 +37,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.invalidateHttpSession(true)
.clearAuthentication(true)
.permitAll()
);
)
.rememberMe(Customizer.withDefaults());
return http.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package by.petrovich.eshop.controllers;
package by.petrovich.eshop.controller;

import by.petrovich.eshop.dto.CartDto;
import by.petrovich.eshop.entity.Order;
Expand All @@ -16,8 +16,8 @@
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

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

@Controller
@SessionAttributes({"cartDto", "user"})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package by.petrovich.eshop.controllers;
package by.petrovich.eshop.controller;

import by.petrovich.eshop.entity.Product;
import by.petrovich.eshop.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
Expand All @@ -19,35 +18,41 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

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

@Validated
@RestController
@RequestMapping("/category")
public class CategoryController {
private final int NUMBER_ELEMENTS_ON_PAGE = 3;
private final CategoryService categoryService;

@Autowired
public CategoryController(CategoryService categoryService) {
this.categoryService = categoryService;
}

@GetMapping("/{categoryId}")
@GetMapping("/category")
public ModelAndView showCategoryPage(
@PathVariable Integer categoryId,
@RequestParam Optional<Integer> categoryId,
@RequestParam("page") Optional<Integer> page,
@RequestParam("size") Optional<Integer> size) {
ModelMap model = new ModelMap();
int currentPage = page.orElse(1);
int pageSize = size.orElse(3);
Page<Product> productPage = categoryService.findProductsByCategoryId(categoryId, PageRequest.of(currentPage - 1, pageSize));
model.addAttribute("productPage", productPage);
int totalPages = productPage.getTotalPages();
int pageSize = size.orElse(NUMBER_ELEMENTS_ON_PAGE);
int id = categoryId.orElse(categoryId.orElseThrow(() -> new BadCredentialsException("Bad credentials.")));
Page<Product> products = categoryService.findProductsByCategoryId(id,
PageRequest.of(currentPage - 1, pageSize));

ModelMap model = new ModelMap();
model.addAttribute("products", products);

int totalPages = products.getTotalPages();
if (totalPages > 0) {
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
.boxed()
.collect(Collectors.toList());
model.addAttribute("pageNumbers", pageNumbers);
model.addAttribute("currentPage", currentPage);
model.addAttribute("categoryId", categoryId);
}
return new ModelAndView(CATEGORY_PAGE, model);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
package by.petrovich.eshop.controllers;
package by.petrovich.eshop.controller;

import by.petrovich.eshop.dto.CartDto;
import by.petrovich.eshop.entity.User;
import by.petrovich.eshop.service.CategoryService;
import by.petrovich.eshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
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.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import static by.petrovich.eshop.PageName.ADMIN_PAGE;
import static by.petrovich.eshop.PageName.CART_PAGE;
import static by.petrovich.eshop.PageName.HOME_PAGE;
import static by.petrovich.eshop.PageName.LOGIN_PAGE;
import static by.petrovich.eshop.PageName.PROFILE_PAGE;
import static by.petrovich.eshop.PageName.REGISTRATION_PAGE;
import static by.petrovich.eshop.utils.PageName.ADMIN_PAGE;
import static by.petrovich.eshop.utils.PageName.CART_PAGE;
import static by.petrovich.eshop.utils.PageName.HOME_PAGE;
import static by.petrovich.eshop.utils.PageName.LOGIN_PAGE;
import static by.petrovich.eshop.utils.PageName.PROFILE_PAGE;
import static by.petrovich.eshop.utils.PageName.REGISTRATION_PAGE;

@SessionAttributes({"user", "cartDto"})
@RestController
@RequiredArgsConstructor
@SessionAttributes({"cartDto"})
public class GoToController {
private final CategoryService categoryService;
private final UserService userService;

@Autowired
public GoToController(CategoryService categoryService, UserService userService) {
this.categoryService = categoryService;
this.userService = userService;
}

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

@ModelAttribute("cartDto")
public CartDto initializeCartSessionObject() {
return new CartDto();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package by.petrovich.eshop.controllers;
package by.petrovich.eshop.controller;

import by.petrovich.eshop.entity.User;
import by.petrovich.eshop.service.OrderService;
Expand All @@ -12,9 +12,9 @@
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import static by.petrovich.eshop.PageName.HOME_PAGE;
import static by.petrovich.eshop.PageName.ORDER_HISTORY_PAGE;
import static by.petrovich.eshop.PageName.ORDER_PAGE;
import static by.petrovich.eshop.utils.PageName.HOME_PAGE;
import static by.petrovich.eshop.utils.PageName.ORDER_HISTORY_PAGE;
import static by.petrovich.eshop.utils.PageName.ORDER_PAGE;

@Controller
@SessionAttributes({"user"})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package by.petrovich.eshop.controllers;
package by.petrovich.eshop.controller;

import by.petrovich.eshop.entity.Product;
import by.petrovich.eshop.service.ProductService;
Expand All @@ -20,7 +20,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static by.petrovich.eshop.PageName.PRODUCT_PAGE;
import static by.petrovich.eshop.utils.PageName.PRODUCT_PAGE;

@RestController
@RequestMapping("/product")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package by.petrovich.eshop.controllers;
package by.petrovich.eshop.controller;

import by.petrovich.eshop.dto.CartDto;
import by.petrovich.eshop.dto.RegistrationFormDto;
import by.petrovich.eshop.entity.User;
import by.petrovich.eshop.service.UserService;
import jakarta.validation.Valid;
import jakarta.validation.ValidationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
Expand All @@ -15,7 +14,7 @@
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import static by.petrovich.eshop.PageName.LOGIN_PAGE;
import static by.petrovich.eshop.utils.PageName.LOGIN_PAGE;

@Controller
@SessionAttributes({"user", "cartDto"})
Expand All @@ -42,10 +41,6 @@ public CartDto initializeCartSessionObject() {
public ModelAndView registrate(@ModelAttribute("registration_form_dto") @Valid RegistrationFormDto registrationFormDto,
BindingResult errors,
ModelAndView model) {
if (errors.hasErrors()) {
throw new ValidationException((Throwable) errors);
// TODO: 3 июл. 2023 г.
}
model.addObject("registration_form_dto", registrationFormDto);
userService.register(registrationFormDto);
return new ModelAndView(LOGIN_PAGE);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/by/petrovich/eshop/dto/ImageDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package by.petrovich.eshop.dto;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ImageDto implements Serializable {
private Integer productId;
@NotBlank(message = "Link is required")
private String link;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class RegistrationFormDto {
private String name;
@NotBlank(message = "Password is required")
@Size(min = 2, max = 20, message = "Password id is required")
// TODO: add validator 28 мая 2023 г.
private String password;
@NotNull
@Email(message = "Email is not valid")
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/by/petrovich/eshop/entity/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package by.petrovich.eshop.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.Hibernate;

import java.util.Objects;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Table(name = "images")
public class Image {
@Id
@Column(name = "image_id")
private Integer productId;
@NotBlank(message = "Link is required")
@Column(unique = true)
private String link;

@OneToOne(mappedBy = "image", cascade = CascadeType.ALL)
@MapsId
@JoinColumn(name = "image_id")
private Product product;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Image image = (Image) o;
return productId != null && Objects.equals(productId, image.productId);
}

@Override
public int hashCode() {
return getClass().hashCode();
}
}
6 changes: 6 additions & 0 deletions src/main/java/by/petrovich/eshop/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import jakarta.persistence.PrimaryKeyJoinColumn;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -51,6 +53,10 @@ public class Product {
@JsonIgnore
private List<Order> orders = new ArrayList<>();

@OneToOne(cascade = {CascadeType.ALL})
@PrimaryKeyJoinColumn
private Image image;

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Loading

0 comments on commit 91e64a7

Please sign in to comment.