Skip to content

Commit

Permalink
finish admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantaphocpython committed Mar 24, 2024
1 parent 49a35b2 commit b7a4cb8
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ public class SecurityConfig {
"/user/**"
};

private final static String[] admin = {
"/admin/**"
};

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(
author -> author
.requestMatchers(authenticatedPath).authenticated()
.requestMatchers(admin).hasAnyAuthority("ADMIN")
.requestMatchers(paths).permitAll()
.anyRequest().authenticated()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.ecommerce.spring.boot.vegetable.project.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.ecommerce.spring.boot.vegetable.project.entity.UserOrder;
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;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

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

@GetMapping
public ModelAndView admin(HttpServletRequest request){
ModelAndView modelAndView = new ModelAndView("admin");
modelAndView.addObject("request", request);
return modelAndView;
}

@GetMapping("/getAllOrder")
public List<UserOrder> getAllOrder() {
return userOrderService.getAllOrder();
}

@GetMapping("/confirm/{orderId}")
public RedirectView confirmOrder(@PathVariable Long orderId) {
RedirectView redirectView = new RedirectView("/admin");
userOrderService.confirmOrder(orderId);
return redirectView;
}

@DeleteMapping("/deleteUser")
public String deleteUserById(@PathVariable Long id) {
userService.deleteUserById(id);
return "delete success";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.naming.Binding;
import java.util.Optional;
Expand All @@ -42,35 +43,35 @@ public ModelAndView showRegistrationForm() {
}

@PostMapping("/register")
public String registerUser(@Valid @ModelAttribute UserDto userDto,
BindingResult result, Model model, HttpServletRequest request) {
public RedirectView registerUser(@Valid @ModelAttribute UserDto userDto,
BindingResult result, Model model, HttpServletRequest request) {
if(!userDto.getPassword().equals(userDto.getConfirmPassword())) {
return "redirect:/registration/register-form?invalidc_f";
return new RedirectView("/registration/register-form?invalidc_f");
}
if(result.hasErrors()) {
model.addAttribute("user", userDto);
}
System.out.println(result);
User user = userService.registerUser(userDto);
publisher.publishEvent(new RegistrationCompleteEvent(user, ApplicationUrl.getUrl(request)));
return "redirect:/registration/register-form?success";
return new RedirectView("/registration/register-form?success");
}

@GetMapping("/verifyEmail")
public String verifyEmail(@RequestParam String token) {
public RedirectView verifyEmail(@RequestParam String token) {
Optional<VerificationToken> verificationTokenOptional =
verificationTokenService.findByToken(token);
if(verificationTokenOptional.isPresent() && verificationTokenOptional.get().getUser().getEnabled()) {
return "redirect:/login?verified";
return new RedirectView("/login?verified");
}
String result = verificationTokenService.validateVerificationToken(token);
switch (result.toLowerCase()) {
case "valid":
return "redirect:/login?valid";
return new RedirectView("/login?valid");
case "invalid":
return "redirect:/error?invalid";
return new RedirectView("/error?invalid");
default:
return "redirect:/error?expired";
return new RedirectView("/error?expired");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@ public RedirectView changeNameUser(@ModelAttribute UserDto user) {
userService.changeNameUser(user);
return redirectView;
}

@GetMapping("/deleteUser/{id}")
public String deleteUserById(@PathVariable Long id) {
userService.deleteUserById(id);
return "delete success";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ public class OrderItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(
name = "product_id",
referencedColumnName = "id"
)
private Product productOrder;

private int quantity;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public interface UserOrderRepository extends JpaRepository<UserOrder, Long> {


Page<UserOrder> findAllByUser(User user, Pageable page);

List<UserOrder> findAllByStatusNot(String status);

List<UserOrder> findAllByStatusIs(String status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public interface UserOrderService {
UserOrder findByUserEmail(String userEmail);

List<UserOrder> getAllUserOrder(String userEmail);

List<UserOrder> getAllOrder();

void confirmOrder(Long orderId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface UserService {
User findByEmail(String userEmail);

void changeNameUser(UserDto user);

void deleteUserById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,17 @@ public List<UserOrder> getAllUserOrder(String userEmail) {
return userOrders;
}

@Override
public List<UserOrder> getAllOrder() {
List<UserOrder> userOrders = orderRepository.findAllByStatusIs("in progress");
return userOrders;
}

@Override
public void confirmOrder(Long orderId) {
UserOrder userOrder = orderRepository.findById(orderId).get();
userOrder.setStatus("confirmed");
orderRepository.save(userOrder);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public class UserServiceImp implements UserService {
@Autowired private RoleRepository roleRepository;
@Override
public User registerUser(UserDto userDto) {
Role role = roleRepository.findByName("USER");
Role role = roleRepository.findByName("USER");
if(role == null) {
role = new Role("USER");
}
User user = User.builder()
.firstName(userDto.getFirstName())
.lastName(userDto.getLastName())
Expand Down Expand Up @@ -53,4 +56,9 @@ public void changeNameUser(UserDto user) {
user1.setLastName((user.getLastName()));
userRepository.save(user1);
}

@Override
public void deleteUserById(Long id) {
userRepository.deleteById(id);
}
}
45 changes: 45 additions & 0 deletions src/main/resources/static/css/admin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


.order-detail-item {
padding: 10px 0;
}

.order-detail-item:hover {
/*box-shadow: 0 0 3px 3px rgba(0, 0, 0, 0.1);*/
background-color: rgba(0, 0, 0, 0.1);
cursor: pointer;
}

.order-radius {
border-top-left-radius: 20px;
border-top-right-radius: 20px;
background-color: rgba(0, 0, 0, 0.1);
}

.order-radius + .order-detail-orderItem {
display: block;
border-top: 1px solid rgba(0, 0, 0, 0.1);
padding: 15px 0;
background-color: rgba(0, 0, 0, 0.1);
border-bottom-left-radius: 20px;
border-bottom-right-radius: 20px;
max-height: 1000px;
overflow: hidden;
animation: sweepDown 3s cubic-bezier(0.2, 0, 0.5, 1) forwards;
}

@keyframes sweepDown {
0% {
max-height: 30px;
}
100% {
max-height: 1000px; /* Chọn một giá trị đủ lớn để đảm bảo nội dung được hiển thị đầy đủ */
}
}

.order-detail-orderItem {
display: none;
}



84 changes: 84 additions & 0 deletions src/main/resources/static/js/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

$(document).ready(function() {
getAllOrder();
});

$(document).on('click', '.order-detail-item', function() {
$(this).toggleClass('order-radius');
console.log(this);
});

function getAllOrder() {
$.ajax({
type: "GET",
url: "/admin/getAllOrder",
success: function(orders) {
let html = '';
orders.forEach((order) => {
html +=
"<div class='row mt-3 order-detail-item align-items-center'>" +
"<div class=\"col-3 h-100\">" +
"<div class='shopping-cart-name' style='margin-left: 0;'>"+ order.user.email +"</div>" +
"</div>" +
"<div class=\"col-9 h-100\">" +
"<div class=\"row text-center align-items-center h-100\">" +
"<div class=\"shopping-cart-property col-3\">" +
"<h4 class=\"shopping-cart-product\">"+ order.address +"</h4>" +
"</div>" +
"<div class=\"shopping-cart-property col-3\">" +
"<form method='post' action='/shop/order' class='d-flex justify-content-center'>" +
"<div class='d-flex product-detail_number align-items-center'>" +
"<input type='hidden' name='userId' value='1'/>" +
"<input type='hidden' name='productId' value='"+ order.phoneNumber +"' disabled/>" +
"<h4 class=\"shopping-cart-product\">"+ order.phoneNumber +"</h4>" +
"</div>" +
"</form>" +
"</div>" +
"<div class=\"shopping-cart-property col-3\">" +
"<h4 class=\"shopping-cart-product\">"+ order.orderNote +"</h4>" +
"</div>" +
"<div class=\"shopping-cart-property col-3\">" +
"<a class=\"btn btn-primary\" href='/admin/confirm/"+ order.id +"'>Confirm</a>" +
"</div>" +
" </div>" +
"</div>" +
"</div>";
html +=
'<div class="order-detail-orderItem row">' +
renderOrderItem(order.orderItems) +
'</div>'
});
$('.order-detail').html(html);
}
});
}

function renderOrderItem(orderItems) {
let orderHtml = '';
orderItems.forEach((orderItem, index) => {
orderHtml +=
"<div class='row align-items-center' >" +
"<div class=\"col-3 h-100\">" +
"<div class='shopping-cart-name'>"+ orderItem.productOrder.name +"</div>" +
"</div>" +
"<div class=\"col-9 h-100\">" +
"<div class=\"row text-center align-items-center h-100\">" +
"<div class=\"shopping-cart-property col-3\">" +
"<h4 class=\"shopping-cart-product\">"+ "$"+ Math.min(orderItem.productOrder.cost, orderItem.productOrder.saleCost).toFixed(2) +"</h4>" +
"</div>" +
"<div class=\"shopping-cart-property col-3\">" +
"<form method='post' action='/shop/order' class='d-flex justify-content-center'>" +
"<div class='d-flex product-detail_number align-items-center'>" +
"<h4 class=\"shopping-cart-product\">"+ orderItem.quantity +"</h4>" +
"</div>" +
"</form>" +
"</div>" +
"<div class=\"shopping-cart-property col-3\">" +
"<h4 class=\"shopping-cart-product\">"+ (Math.min(orderItem.productOrder.cost, orderItem.productOrder.saleCost) * orderItem.quantity).toFixed(2) +"</h4>" +
"</div>" +
" </div>" +
"</div>" +
"</div>";
});
return orderHtml;
}
Loading

0 comments on commit b7a4cb8

Please sign in to comment.