Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #184 from hardingadonis/182-auto-generate-customer-id
Browse files Browse the repository at this point in the history
auto generate customer id when create new customer
  • Loading branch information
yuhtnguyen authored Mar 12, 2024
2 parents 53a9058 + 874cef8 commit cb840a5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String code = request.getParameter("code");
String address = request.getParameter("address");
String email = request.getParameter("email");

try {
if (name.length() > 0 && code.length() > 0 && address.length() > 0 && email.length() > 0) {
if (name.length() > 0 && address.length() > 0 && email.length() > 0) {
Customer customer = new Customer();
customer.setName(name);
customer.setCode(code);
customer.setAddress(address);
customer.setEmail(email);
Singleton.customerDAO.save(customer);
Expand All @@ -48,9 +46,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
customer.setName(name);
customer.setAddress(address);
StringBuilder message = new StringBuilder("Tạo khách hàng không thành công: ");
if (e.getMessage().contains(code)) {
message.append("Không được trùng mã khách hàng.");
}
if (e.getMessage().contains(email)) {
message.append("Không được trùng email.");
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/hardingadonis/saledock/model/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.persistence.*;
import java.time.*;
import java.util.*;

import lombok.*;

@Entity(name = "Customer")
Expand Down Expand Up @@ -38,11 +40,18 @@ public class Customer {

@PrePersist
protected void onCreate() {
this.code = generateRandomCustomerCode();
this.createdAt = LocalDateTime.now(ZoneId.of("Asia/Ho_Chi_Minh"));
}

@PreUpdate
protected void onUpdate() {
this.updatedAt = LocalDateTime.now(ZoneId.of("Asia/Ho_Chi_Minh"));
}

private static String generateRandomCustomerCode() {
UUID uuid = UUID.randomUUID();

return uuid.toString().toUpperCase().replace("-", "").substring(0, 7);
}
}
5 changes: 0 additions & 5 deletions src/main/java/io/hardingadonis/saledock/model/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,4 @@ private static String generateRandomOrderCode() {

return uuid.toString().toUpperCase().replace("-", "").substring(0, 15);
}

public String getTotalToString() {
DecimalFormat decimalFormat = new DecimalFormat("#,###.#");
return decimalFormat.format(this.total);
}
}
29 changes: 6 additions & 23 deletions src/main/webapp/view/jsp/management/customer/add-customer.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,15 @@
<div class="card-body">
<form action="./add-customer" method="post" id="form-add-customer">
<div class="row">
<div class="col">
<div class="mb-3 form-group">
<label class="form-label" for="name">
<strong>Tên khách hàng</strong>
</label>
<div class="mb-3 form-group">
<label class="form-label" for="name">
<strong>Tên khách hàng</strong>
</label>
<div class="mb-3 form-group">
<input class="form-control" type="text" id="name-customer" placeholder="Nhập tên khách hàng" name="name" value="<%= (error != null && customer != null) ? customer.getName() : ""%>">
<span class="form-message text-danger"></span>
</div>
</div>
</div>
<div class="col">
<div class="mb-3">
<label class="form-label" for="code">
<strong>Mã khách hàng</strong>
</label>
<div class="mb-3 form-group">
<input class="form-control" type="text" id="code-customer" placeholder="Mã khách hàng" name="code" >
<span class="form-message text-danger"></span>
</div>
<input class="form-control" type="text" id="name-customer" placeholder="Nhập tên khách hàng" name="name" value="<%= (error != null && customer != null) ? customer.getName() : ""%>">
<span class="form-message text-danger"></span>
</div>
</div>

</div>
<div class="mb-3">
<div class="mb-3">
Expand Down Expand Up @@ -122,11 +108,8 @@
formGroupSelector: '.form-group',
rules: [
Validator.isRequired('#name-customer', 'Vui lòng nhập tên khách hàng!'),
Validator.isRequired('#code-customer', 'Vui lòng nhập mã khách hàng!'),
Validator.isRequired('#address-customer', 'Vui lòng nhập địa chỉ khách hàng!'),
Validator.isRequired('#email-customer', 'Vui lòng nhập email khách hàng!')
]
});
</script>
Expand Down

0 comments on commit cb840a5

Please sign in to comment.