Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantaphocpython committed Mar 25, 2024
1 parent b7a4cb8 commit 8d2ccfd
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public RedirectView registerUser(@Valid @ModelAttribute UserDto userDto,
}
System.out.println(result);
User user = userService.registerUser(userDto);
if(user == null)
return new RedirectView("/registration/register-form?used");
publisher.publishEvent(new RegistrationCompleteEvent(user, ApplicationUrl.getUrl(request)));
return new RedirectView("/registration/register-form?success");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public ModelAndView shop(HttpServletRequest request) {
return modelAndView;
}

@GetMapping("/search")
public ModelAndView search(@RequestParam String search, HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView("shop");
List<Category> categories = categoryService.getCategoryList();
modelAndView.addObject("categories", categories);
modelAndView.addObject("request", request);
modelAndView.addObject("search", search);
return modelAndView;
}

// @GetMapping("searchProduct")
// public List<Product> searchProducts(@RequestParam String search) {
// List<Product> products = productService.searchProducts(search);
// }

@GetMapping("/product/{id}")
public ModelAndView productDetail(@PathVariable Long id, HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView("productDetail");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import jakarta.servlet.http.HttpServletRequest;
import org.ecommerce.spring.boot.vegetable.project.dto.UserDto;
import org.ecommerce.spring.boot.vegetable.project.entity.OrderItem;
import org.ecommerce.spring.boot.vegetable.project.entity.User;
import org.ecommerce.spring.boot.vegetable.project.entity.UserOrder;
import org.ecommerce.spring.boot.vegetable.project.entity.*;
import org.ecommerce.spring.boot.vegetable.project.service.CategoryService;
import org.ecommerce.spring.boot.vegetable.project.service.UserOrderService;
import org.ecommerce.spring.boot.vegetable.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -21,13 +20,17 @@
@RequestMapping("/user")
public class UserController {

@Autowired UserOrderService userOrderService;
@Autowired UserService userService;
@Autowired private UserOrderService userOrderService;
@Autowired private UserService userService;
@Autowired private CategoryService categoryService;

@GetMapping("/purchase")
public ModelAndView purchase(HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView("user");

List<Category> categories = categoryService.getCategoryList();
modelAndView.addObject("categories", categories);

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String userEmail = authentication.getName();

Expand All @@ -43,6 +46,9 @@ public ModelAndView purchase(HttpServletRequest request) {
public ModelAndView account(HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView("user");

List<Category> categories = categoryService.getCategoryList();
modelAndView.addObject("categories", categories);

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String userEmail = authentication.getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class Role {
)
private String name;

// @ManyToMany(mappedBy = "roles")
// private List<User> users = new ArrayList<>();

public Role(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package org.ecommerce.spring.boot.vegetable.project.repository;

import jakarta.transaction.Transactional;
import org.ecommerce.spring.boot.vegetable.project.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);

void deleteByEnabled(Boolean enabled);

@Query(value = "DELETE FROM user_roles WHERE user_id = :id", nativeQuery = true)
@Modifying
void delete1(@Param("id") Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public User registerUser(UserDto userDto) {
if(role == null) {
role = new Role("USER");
}
Optional<User> check = userRepository.findByEmail(userDto.getEmail());
if(check.isPresent()) {
return null;
}
User user = User.builder()
.firstName(userDto.getFirstName())
.lastName(userDto.getLastName())
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
</ul>
</div>
<div class="col-6">
<form class="d-flex search-bar" role="search">
<input class="form-control me-2 search-bar-input" type="search" placeholder="What do you need?">
<form class="d-flex search-bar" role="search" action="/shop/search">
<input class="form-control me-2 search-bar-input" type="search" placeholder="What do you need?" name="search">
<button class="btn btn-outline-success search-bar-submit" type="submit">SEARCH</button>
</form>
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/templates/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<div class="alert alert-info" th:if="${param.success}">
Please check your email to complete your registration.
</div>
<div class="alert alert-danger" th:if="${param.used}">
Email is used.
</div>

<div class="mb-3 row">
<label for="firstName" class="col-sm-4 col-form-label">First Name</label>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.ecommerce.spring.boot.vegetable.project.repository;

import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.ecommerce.spring.boot.vegetable.project.entity.Role;
import org.ecommerce.spring.boot.vegetable.project.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class UserRepositoryTest {

@Autowired private UserRepository userRepository;
@Autowired private RoleRepository repository;
private EntityManager entityManager;


@Test
public void delete1() {
userRepository.delete1(16L);
}

}

0 comments on commit 8d2ccfd

Please sign in to comment.