Fix DataInitializer and make products publicly accessible
This commit is contained in:
@@ -19,23 +19,38 @@ public class DataInitializer implements CommandLineRunner {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(String... args) {
|
public void run(String... args) {
|
||||||
|
System.out.println("==== DataInitializer: Starting user creation ====");
|
||||||
|
|
||||||
if (userRepository.findByUsername("admin").isEmpty()) {
|
if (userRepository.findByUsername("admin").isEmpty()) {
|
||||||
|
System.out.println("Creating admin user...");
|
||||||
User admin = new User();
|
User admin = new User();
|
||||||
admin.setUsername("admin");
|
admin.setUsername("admin");
|
||||||
admin.setPassword(passwordEncoder.encode("admin123"));
|
admin.setPassword(passwordEncoder.encode("admin123"));
|
||||||
|
admin.setEmail("admin@petshop.com");
|
||||||
|
admin.setFullName("Admin User");
|
||||||
admin.setRole(User.Role.ADMIN);
|
admin.setRole(User.Role.ADMIN);
|
||||||
userRepository.save(admin);
|
userRepository.save(admin);
|
||||||
|
System.out.println("Admin user created successfully");
|
||||||
|
} else {
|
||||||
|
System.out.println("Admin user already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userRepository.findByUsername("staff").isEmpty()) {
|
if (userRepository.findByUsername("staff").isEmpty()) {
|
||||||
|
System.out.println("Creating staff user...");
|
||||||
User staff = new User();
|
User staff = new User();
|
||||||
staff.setUsername("staff");
|
staff.setUsername("staff");
|
||||||
staff.setPassword(passwordEncoder.encode("staff123"));
|
staff.setPassword(passwordEncoder.encode("staff123"));
|
||||||
|
staff.setEmail("staff@petshop.com");
|
||||||
|
staff.setFullName("Staff User");
|
||||||
staff.setRole(User.Role.STAFF);
|
staff.setRole(User.Role.STAFF);
|
||||||
userRepository.save(staff);
|
userRepository.save(staff);
|
||||||
|
System.out.println("Staff user created successfully");
|
||||||
|
} else {
|
||||||
|
System.out.println("Staff user already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userRepository.findByUsername("customer").isEmpty()) {
|
if (userRepository.findByUsername("customer").isEmpty()) {
|
||||||
|
System.out.println("Creating customer user...");
|
||||||
User customer = new User();
|
User customer = new User();
|
||||||
customer.setUsername("customer");
|
customer.setUsername("customer");
|
||||||
customer.setPassword(passwordEncoder.encode("customer123"));
|
customer.setPassword(passwordEncoder.encode("customer123"));
|
||||||
@@ -43,6 +58,11 @@ public class DataInitializer implements CommandLineRunner {
|
|||||||
customer.setFullName("Test Customer");
|
customer.setFullName("Test Customer");
|
||||||
customer.setRole(User.Role.CUSTOMER);
|
customer.setRole(User.Role.CUSTOMER);
|
||||||
userRepository.save(customer);
|
userRepository.save(customer);
|
||||||
|
System.out.println("Customer user created successfully");
|
||||||
|
} else {
|
||||||
|
System.out.println("Customer user already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("==== DataInitializer: Completed ====");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/products")
|
@RequestMapping("/api/v1/products")
|
||||||
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
|
|
||||||
public class ProductController {
|
public class ProductController {
|
||||||
|
|
||||||
private final ProductService productService;
|
private final ProductService productService;
|
||||||
@@ -36,11 +35,13 @@ public class ProductController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
|
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
|
||||||
public ResponseEntity<ProductResponse> createProduct(@Valid @RequestBody ProductRequest request) {
|
public ResponseEntity<ProductResponse> createProduct(@Valid @RequestBody ProductRequest request) {
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(productService.createProduct(request));
|
return ResponseEntity.status(HttpStatus.CREATED).body(productService.createProduct(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
|
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
|
||||||
public ResponseEntity<ProductResponse> updateProduct(
|
public ResponseEntity<ProductResponse> updateProduct(
|
||||||
@PathVariable Long id,
|
@PathVariable Long id,
|
||||||
@Valid @RequestBody ProductRequest request) {
|
@Valid @RequestBody ProductRequest request) {
|
||||||
@@ -48,12 +49,14 @@ public class ProductController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
|
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
|
||||||
productService.deleteProduct(id);
|
productService.deleteProduct(id);
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping
|
@DeleteMapping
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
public ResponseEntity<Void> bulkDeleteProducts(@Valid @RequestBody BulkDeleteRequest request) {
|
public ResponseEntity<Void> bulkDeleteProducts(@Valid @RequestBody BulkDeleteRequest request) {
|
||||||
productService.bulkDeleteProducts(request);
|
productService.bulkDeleteProducts(request);
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
|
|||||||
@@ -16,10 +16,7 @@ spring:
|
|||||||
|
|
||||||
sql:
|
sql:
|
||||||
init:
|
init:
|
||||||
mode: always
|
mode: never
|
||||||
schema-locations: classpath:schema.sql
|
|
||||||
data-locations: classpath:data.sql
|
|
||||||
continue-on-error: false
|
|
||||||
|
|
||||||
jpa:
|
jpa:
|
||||||
hibernate:
|
hibernate:
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.petshop.backend.util;
|
||||||
|
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
|
||||||
|
public class PasswordHashGenerator {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||||
|
|
||||||
|
System.out.println("admin123: " + encoder.encode("admin123"));
|
||||||
|
System.out.println("staff123: " + encoder.encode("staff123"));
|
||||||
|
System.out.println("customer123: " + encoder.encode("customer123"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user