Fix DataInitializer and make products publicly accessible

This commit is contained in:
2026-03-08 14:38:52 -06:00
parent cce4de566c
commit 992e610e4b
4 changed files with 38 additions and 5 deletions

View File

@@ -19,23 +19,38 @@ public class DataInitializer implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("==== DataInitializer: Starting user creation ====");
if (userRepository.findByUsername("admin").isEmpty()) {
System.out.println("Creating admin user...");
User admin = new User();
admin.setUsername("admin");
admin.setPassword(passwordEncoder.encode("admin123"));
admin.setEmail("admin@petshop.com");
admin.setFullName("Admin User");
admin.setRole(User.Role.ADMIN);
userRepository.save(admin);
System.out.println("Admin user created successfully");
} else {
System.out.println("Admin user already exists");
}
if (userRepository.findByUsername("staff").isEmpty()) {
System.out.println("Creating staff user...");
User staff = new User();
staff.setUsername("staff");
staff.setPassword(passwordEncoder.encode("staff123"));
staff.setEmail("staff@petshop.com");
staff.setFullName("Staff User");
staff.setRole(User.Role.STAFF);
userRepository.save(staff);
System.out.println("Staff user created successfully");
} else {
System.out.println("Staff user already exists");
}
if (userRepository.findByUsername("customer").isEmpty()) {
System.out.println("Creating customer user...");
User customer = new User();
customer.setUsername("customer");
customer.setPassword(passwordEncoder.encode("customer123"));
@@ -43,6 +58,11 @@ public class DataInitializer implements CommandLineRunner {
customer.setFullName("Test Customer");
customer.setRole(User.Role.CUSTOMER);
userRepository.save(customer);
System.out.println("Customer user created successfully");
} else {
System.out.println("Customer user already exists");
}
System.out.println("==== DataInitializer: Completed ====");
}
}

View File

@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/v1/products")
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
public class ProductController {
private final ProductService productService;
@@ -36,11 +35,13 @@ public class ProductController {
}
@PostMapping
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
public ResponseEntity<ProductResponse> createProduct(@Valid @RequestBody ProductRequest request) {
return ResponseEntity.status(HttpStatus.CREATED).body(productService.createProduct(request));
}
@PutMapping("/{id}")
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
public ResponseEntity<ProductResponse> updateProduct(
@PathVariable Long id,
@Valid @RequestBody ProductRequest request) {
@@ -48,12 +49,14 @@ public class ProductController {
}
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
productService.deleteProduct(id);
return ResponseEntity.noContent().build();
}
@DeleteMapping
@PreAuthorize("hasRole('ADMIN')")
public ResponseEntity<Void> bulkDeleteProducts(@Valid @RequestBody BulkDeleteRequest request) {
productService.bulkDeleteProducts(request);
return ResponseEntity.noContent().build();

View File

@@ -16,10 +16,7 @@ spring:
sql:
init:
mode: always
schema-locations: classpath:schema.sql
data-locations: classpath:data.sql
continue-on-error: false
mode: never
jpa:
hibernate:

View File

@@ -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"));
}
}