Compare commits

3 Commits

Author SHA1 Message Date
e986fa97ca running finally.. 2025-12-04 15:20:42 -05:00
9096ddd165 running finally.. 2025-12-04 15:06:29 -05:00
3d1501cc87 running finally.. 2025-12-04 14:43:07 -05:00
70 changed files with 2915 additions and 2637 deletions

View File

@@ -1,34 +0,0 @@
# Stage 1: Build the application (The Build Stage)
# Use a Java SDK image with Maven pre-installed
FROM maven:3.9-jdk-17-slim AS build
# Set the working directory inside the container
WORKDIR /app
# Copy the Maven project files (pom.xml) first to leverage Docker layer caching
COPY pom.xml .
# Copy the source code
COPY src ./src
# Build the Spring Boot application, skipping tests to speed up the Docker build
# This creates the executable JAR file in the 'target' directory
RUN mvn clean package -DskipTests
# Stage 2: Create the final lightweight image (The Runtime Stage)
# Use a smaller Java Runtime Environment (JRE) image for a smaller footprint
FROM openjdk:17-jre-slim
# Set the working directory in the final image
WORKDIR /app
# Copy the built JAR file from the 'build' stage into the final image
# The JAR file is typically named 'target/<your-app-name>-<version>.jar'
# You may need to adjust the name if you have a non-standard pom.xml
COPY --from=build /app/target/*.jar app.jar
# Expose the default Spring Boot port
EXPOSE 8080
# Define the command to run the application
ENTRYPOINT ["java", "-jar", "app.jar"]

View File

@@ -0,0 +1,17 @@
# Stage 1: Build the application
FROM openjdk:17-jdk-slim as build
WORKDIR /app
COPY gradlew .
COPY settings.gradle .
COPY build.gradle .
COPY src ./src
# Adjust the build command for Maven: ./mvnw package -DskipTests
RUN ./gradlew bootJar
# Stage 2: Create the final lightweight image
FROM openjdk:17-jre-slim
WORKDIR /app
# Get the built JAR from the build stage
COPY --from=build /app/build/libs/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

View File

@@ -2,18 +2,20 @@ version: '3.8'
services: services:
# --- 1. Spring API Service (Backend) --- # --- 1. Spring API Service (Backend) ---
ss_builder-api: spring-api:
build: build:
context: ./backend # Path to your Spring project's root folder context: ./backend # Path to your Spring project's root folder
dockerfile: Dockerfile # Assumes you have a Dockerfile in ./backend dockerfile: Dockerfile # Assumes you have a Dockerfile in ./backend
container_name: ss_builder-api container_name: spring-api
ports: ports:
- "8080:8080" # Map host port 8080 to container port 8080 - "8080:8080" # Map host port 8080 to container port 8080
environment: environment:
# These environment variables link the API to the database service defined below # These environment variables link the API to the database service defined below
- SPRING_DATASOURCE_URL=jdbc:postgresql://r710.dev.gofwd.group:5433/ss_builder - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/mydatabase
- SPRING_DATASOURCE_USERNAME=dba - SPRING_DATASOURCE_USERNAME=myuser
- SPRING_DATASOURCE_PASSWORD=!@#Qwerty - SPRING_DATASOURCE_PASSWORD=mypassword
depends_on:
- db
networks: networks:
- app-network - app-network
@@ -22,20 +24,38 @@ services:
build: build:
context: ./frontend # Path to your Next.js project's root folder context: ./frontend # Path to your Next.js project's root folder
dockerfile: Dockerfile # Assumes you have a Dockerfile in ./frontend dockerfile: Dockerfile # Assumes you have a Dockerfile in ./frontend
container_name: ss_builder-app container_name: nextjs-app
ports: ports:
- "3000:3000" # Map host port 3000 to container port 3000 - "3000:3000" # Map host port 3000 to container port 3000
environment: environment:
# This variable is crucial: Next.js needs the URL for the Spring API # This variable is crucial: Next.js needs the URL for the Spring API
# Use the Docker internal service name 'spring-api' and its port 8080 # Use the Docker internal service name 'spring-api' and its port 8080
- NEXT_PUBLIC_API_URL=http://ss_builder-api:8080 - NEXT_PUBLIC_API_URL=http://spring-api:8080
# For local testing, you might need the host IP for Next.js to call back # For local testing, you might need the host IP for Next.js to call back
# - NEXT_PUBLIC_API_URL_LOCAL=http://localhost:8080 # - NEXT_PUBLIC_API_URL_LOCAL=http://localhost:8080
depends_on: depends_on:
- ss_builder-api - spring-api
networks: networks:
- app-network - app-network
# --- 3. PostgreSQL Database Service (Example Dependency) ---
db:
image: postgres:15-alpine # Lightweight and stable PostgreSQL image
container_name: postgres-db
environment:
- POSTGRES_DB=mydatabase
- POSTGRES_USER=myuser
- POSTGRES_PASSWORD=mypassword
volumes:
- postgres_data:/var/lib/postgresql/data # Persist the database data
ports:
- "5432:5432" # Optional: Map DB port for external access (e.g., DBeaver)
networks:
- app-network
# --- Docker Volume for Persistent Data ---
volumes:
postgres_data:
# --- Docker Network for Inter-Container Communication --- # --- Docker Network for Inter-Container Communication ---
networks: networks:

View File

@@ -30,8 +30,13 @@ public class CorsConfig {
"https://localhost:8080", "https://localhost:8080",
"http://localhost:3000", "http://localhost:3000",
"https://localhost:3000", "https://localhost:3000",
"https://localhost:3000/gunbuilder", "http://192.168.11.210:8070",
"http://localhost:3000/gunbuilder" "https://192.168.11.210:8070",
"http://citysites.gofwd.group",
"https://citysites.gofwd.group",
"http://citysites.gofwd.group:8070",
"https://citysites.gofwd.group:8070"
)); ));
// Allow all headers // Allow all headers

View File

@@ -24,14 +24,19 @@ public class SecurityConfig {
sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS) sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
) )
.authorizeHttpRequests(auth -> auth .authorizeHttpRequests(auth -> auth
// Auth endpoints always open // Auth endpoints always open
.requestMatchers("/api/auth/**").permitAll() .requestMatchers("/api/auth/**").permitAll()
// Swagger / docs // Swagger / docs
.requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll() .requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll()
// Health // Health
.requestMatchers("/actuator/health", "/actuator/info").permitAll() .requestMatchers("/actuator/health", "/actuator/info").permitAll()
// Public product endpoints // Public product endpoints
.requestMatchers("/api/products/gunbuilder/**").permitAll() .requestMatchers("/api/products/gunbuilder/**").permitAll()
// Everything else (for now) also open we can tighten later // Everything else (for now) also open we can tighten later
.anyRequest().permitAll() .anyRequest().permitAll()
); );

View File

@@ -1,51 +0,0 @@
package group.goforward.ballistic.controllers;
import group.goforward.ballistic.model.Brand;
import group.goforward.ballistic.model.State;
import group.goforward.ballistic.repos.BrandRepository;
import group.goforward.ballistic.services.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/brands")
public class BrandController {
@Autowired
private BrandRepository repo;
@Autowired
private BrandService brandService;
//@Cacheable(value="getAllStates")
@GetMapping("/all")
public ResponseEntity<List<Brand>> getAllBrands() {
List<Brand> brand = repo.findAll();
return ResponseEntity.ok(brand);
}
@GetMapping("/{id}")
public ResponseEntity<Brand> getAllBrandsById(@PathVariable Integer id) {
return repo.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@PostMapping("/add")
public ResponseEntity<Brand> createbrand(@RequestBody Brand item) {
Brand created = brandService.save(item);
return ResponseEntity.status(HttpStatus.CREATED).body(created);
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable Integer id) {
return brandService.findById(id)
.map(item -> {
brandService.deleteById(id);
return ResponseEntity.noContent().<Void>build();
})
.orElse(ResponseEntity.notFound().build());
}
}

View File

@@ -0,0 +1,34 @@
package group.goforward.ballistic.controllers;
import group.goforward.ballistic.repos.PartCategoryRepository;
import group.goforward.ballistic.web.dto.admin.PartCategoryDto;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/categories")
@CrossOrigin // you can tighten origins later
public class CategoryController {
private final PartCategoryRepository partCategories;
public CategoryController(PartCategoryRepository partCategories) {
this.partCategories = partCategories;
}
@GetMapping
public List<PartCategoryDto> list() {
return partCategories.findAllByOrderByGroupNameAscSortOrderAscNameAsc()
.stream()
.map(pc -> new PartCategoryDto(
pc.getId(),
pc.getSlug(),
pc.getName(),
pc.getDescription(),
pc.getGroupName(),
pc.getSortOrder()
))
.toList();
}
}

View File

@@ -5,7 +5,6 @@ import group.goforward.ballistic.model.State;
import group.goforward.ballistic.repos.StateRepository; import group.goforward.ballistic.repos.StateRepository;
import group.goforward.ballistic.services.StatesService; import group.goforward.ballistic.services.StatesService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@@ -14,38 +13,44 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/api/states") @RequestMapping()
public class StateController { public class StateController {
@Autowired @Autowired
private StateRepository repo; private StateRepository repo;
@Autowired @Autowired
private StatesService statesService; private StatesService statesService;
//@Cacheable(value="getAllStates")
@GetMapping("/all") @GetMapping("/api/getAllStates")
public ResponseEntity<List<State>> getAllStates() { public ResponseEntity<List<State>> getAllStates() {
List<State> state = repo.findAll(); List<State> state = repo.findAll();
return ResponseEntity.ok(state); return ResponseEntity.ok(state);
} }
@GetMapping("/{id}") @GetMapping("/api/getAllStatesTest")
public ApiResponse<List<State>> getAllStatesTest() {
List<State> state = repo.findAll();
return ApiResponse.success(state);
}
@GetMapping("/api/getAllStatesById/{id}")
public ResponseEntity<State> getAllStatesById(@PathVariable Integer id) { public ResponseEntity<State> getAllStatesById(@PathVariable Integer id) {
return repo.findById(id) return repo.findById(id)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build()); .orElse(ResponseEntity.notFound().build());
} }
@GetMapping("/byAbbrev/{abbreviation}") @GetMapping("/api/getAllStatesByAbbreviation/{abbreviation}")
public ResponseEntity<State> getAllStatesByAbbreviation(@PathVariable String abbreviation) { public ResponseEntity<State> getAllStatesByAbbreviation(@PathVariable String abbreviation) {
return repo.findByAbbreviation(abbreviation) return repo.findByAbbreviation(abbreviation)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build()); .orElse(ResponseEntity.notFound().build());
} }
@PostMapping("/addState") @PostMapping("/api/addState")
public ResponseEntity<State> createState(@RequestBody State item) { public ResponseEntity<State> createState(@RequestBody State item) {
State created = statesService.save(item); State created = statesService.save(item);
return ResponseEntity.status(HttpStatus.CREATED).body(created); return ResponseEntity.status(HttpStatus.CREATED).body(created);
} }
@DeleteMapping("/deleteState/{id}") @DeleteMapping("/api/deleteState/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable Integer id) { public ResponseEntity<Void> deleteItem(@PathVariable Integer id) {
return statesService.findById(id) return statesService.findById(id)
.map(item -> { .map(item -> {

View File

@@ -12,36 +12,33 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/api/user") @RequestMapping()
public class UserController { public class UserController {
private final UserRepository repo; @Autowired
private final UsersService usersService; private UserRepository repo;
@Autowired
private UsersService usersService;
public UserController(UserRepository repo, UsersService usersService) { @GetMapping("/api/getAllUsers")
this.repo = repo;
this.usersService = usersService;
}
@GetMapping("/all")
public ResponseEntity<List<User>> getAllUsers() { public ResponseEntity<List<User>> getAllUsers() {
List<User> data = repo.findAll(); List<User> data = repo.findAll();
return ResponseEntity.ok(data); return ResponseEntity.ok(data);
} }
@GetMapping("/byId/{id}") @GetMapping("/api/getAllUsersById/{id}")
public ResponseEntity<User> getAllStatesById(@PathVariable Integer id) { public ResponseEntity<User> getAllStatesById(@PathVariable Integer id) {
return repo.findById(id) return repo.findById(id)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build()); .orElse(ResponseEntity.notFound().build());
} }
@PostMapping("/addUser") @PostMapping("/api/addUser")
public ResponseEntity<User> createUser(@RequestBody User item) { public ResponseEntity<User> createUser(@RequestBody User item) {
User created = usersService.save(item); User created = usersService.save(item);
return ResponseEntity.status(HttpStatus.CREATED).body(created); return ResponseEntity.status(HttpStatus.CREATED).body(created);
} }
@DeleteMapping("/deleteUser/{id}") @DeleteMapping("/api/deleteUser/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable Integer id) { public ResponseEntity<Void> deleteItem(@PathVariable Integer id) {
return usersService.findById(id) return usersService.findById(id)
.map(item -> { .map(item -> {

View File

@@ -1,13 +1,15 @@
package group.goforward.ballistic.controllers.admin; package group.goforward.ballistic.controllers.admin;
import group.goforward.ballistic.model.AffiliateCategoryMap; import group.goforward.ballistic.model.CategoryMapping;
import group.goforward.ballistic.model.Merchant;
import group.goforward.ballistic.model.PartCategory; import group.goforward.ballistic.model.PartCategory;
import group.goforward.ballistic.repos.CategoryMappingRepository; import group.goforward.ballistic.repos.CategoryMappingRepository;
import group.goforward.ballistic.repos.MerchantRepository;
import group.goforward.ballistic.repos.PartCategoryRepository; import group.goforward.ballistic.repos.PartCategoryRepository;
import group.goforward.ballistic.web.dto.admin.PartRoleMappingDto; import group.goforward.ballistic.web.dto.admin.MerchantCategoryMappingDto;
import group.goforward.ballistic.web.dto.admin.PartRoleMappingRequest; import group.goforward.ballistic.web.dto.admin.SimpleMerchantDto;
import group.goforward.ballistic.web.dto.admin.UpdateMerchantCategoryMappingRequest;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
@@ -15,111 +17,101 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/api/admin/category-mappings") @RequestMapping("/api/admin/category-mappings")
@CrossOrigin @CrossOrigin // you can tighten origins later
public class AdminCategoryMappingController { public class AdminCategoryMappingController {
private final CategoryMappingRepository categoryMappingRepository; private final CategoryMappingRepository categoryMappingRepository;
private final MerchantRepository merchantRepository;
private final PartCategoryRepository partCategoryRepository; private final PartCategoryRepository partCategoryRepository;
public AdminCategoryMappingController( public AdminCategoryMappingController(
CategoryMappingRepository categoryMappingRepository, CategoryMappingRepository categoryMappingRepository,
MerchantRepository merchantRepository,
PartCategoryRepository partCategoryRepository PartCategoryRepository partCategoryRepository
) { ) {
this.categoryMappingRepository = categoryMappingRepository; this.categoryMappingRepository = categoryMappingRepository;
this.merchantRepository = merchantRepository;
this.partCategoryRepository = partCategoryRepository; this.partCategoryRepository = partCategoryRepository;
} }
// GET /api/admin/category-mappings?platform=AR-15 /**
@GetMapping * Merchants that have at least one category_mappings row.
public List<PartRoleMappingDto> list( * Used for the "All Merchants" dropdown in the UI.
@RequestParam(name = "platform", defaultValue = "AR-15") String platform */
) { @GetMapping("/merchants")
List<AffiliateCategoryMap> mappings = public List<SimpleMerchantDto> listMerchantsWithMappings() {
categoryMappingRepository.findBySourceTypeAndPlatformOrderById("PART_ROLE", platform); List<Merchant> merchants = categoryMappingRepository.findDistinctMerchantsWithMappings();
return merchants.stream()
return mappings.stream() .map(m -> new SimpleMerchantDto(m.getId(), m.getName()))
.map(this::toDto)
.toList(); .toList();
} }
// POST /api/admin/category-mappings /**
@PostMapping * List mappings for a specific merchant, or all mappings if no merchantId is provided.
public ResponseEntity<PartRoleMappingDto> create( * GET /api/admin/category-mappings?merchantId=1
@RequestBody PartRoleMappingRequest request */
@GetMapping
public List<MerchantCategoryMappingDto> listByMerchant(
@RequestParam(name = "merchantId", required = false) Integer merchantId
) { ) {
if (request.platform() == null || request.partRole() == null || request.categorySlug() == null) { List<CategoryMapping> mappings;
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "platform, partRole, and categorySlug are required");
if (merchantId != null) {
mappings = categoryMappingRepository.findByMerchantIdOrderByRawCategoryPathAsc(merchantId);
} else {
// fall back to all mappings; you can add a more specific repository method later if desired
mappings = categoryMappingRepository.findAll();
} }
PartCategory category = partCategoryRepository.findBySlug(request.categorySlug()) return mappings.stream()
.orElseThrow(() -> new ResponseStatusException( .map(cm -> new MerchantCategoryMappingDto(
HttpStatus.BAD_REQUEST, cm.getId(),
"Unknown category slug: " + request.categorySlug() cm.getMerchant().getId(),
)); cm.getMerchant().getName(),
cm.getRawCategoryPath(),
AffiliateCategoryMap mapping = new AffiliateCategoryMap(); cm.getPartCategory() != null ? cm.getPartCategory().getId() : null,
mapping.setSourceType("PART_ROLE"); cm.getPartCategory() != null ? cm.getPartCategory().getName() : null
mapping.setSourceValue(request.partRole()); ))
mapping.setPlatform(request.platform()); .toList();
mapping.setPartCategory(category);
mapping.setNotes(request.notes());
AffiliateCategoryMap saved = categoryMappingRepository.save(mapping);
return ResponseEntity.status(HttpStatus.CREATED).body(toDto(saved));
} }
// PUT /api/admin/category-mappings/{id} /**
@PutMapping("/{id}") * Update a single mapping's part_category.
public PartRoleMappingDto update( * POST /api/admin/category-mappings/{id}
* Body: { "partCategoryId": 24 }
*/
@PostMapping("/{id}")
public MerchantCategoryMappingDto updateMapping(
@PathVariable Integer id, @PathVariable Integer id,
@RequestBody PartRoleMappingRequest request @RequestBody UpdateMerchantCategoryMappingRequest request
) { ) {
AffiliateCategoryMap mapping = categoryMappingRepository.findById(id) CategoryMapping mapping = categoryMappingRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Mapping not found")); .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Mapping not found"));
if (request.platform() != null) { PartCategory partCategory = null;
mapping.setPlatform(request.platform()); if (request.partCategoryId() != null) {
} partCategory = partCategoryRepository.findById(request.partCategoryId())
if (request.partRole() != null) { .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Part category not found"));
mapping.setSourceValue(request.partRole());
}
if (request.categorySlug() != null) {
PartCategory category = partCategoryRepository.findBySlug(request.categorySlug())
.orElseThrow(() -> new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Unknown category slug: " + request.categorySlug()
));
mapping.setPartCategory(category);
}
if (request.notes() != null) {
mapping.setNotes(request.notes());
} }
AffiliateCategoryMap saved = categoryMappingRepository.save(mapping); mapping.setPartCategory(partCategory);
return toDto(saved); mapping = categoryMappingRepository.save(mapping);
}
// DELETE /api/admin/category-mappings/{id} return new MerchantCategoryMappingDto(
@DeleteMapping("/{id}") mapping.getId(),
@ResponseStatus(HttpStatus.NO_CONTENT) mapping.getMerchant().getId(),
public void delete(@PathVariable Integer id) { mapping.getMerchant().getName(),
if (!categoryMappingRepository.existsById(id)) { mapping.getRawCategoryPath(),
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Mapping not found"); mapping.getPartCategory() != null ? mapping.getPartCategory().getId() : null,
} mapping.getPartCategory() != null ? mapping.getPartCategory().getName() : null
categoryMappingRepository.deleteById(id);
}
private PartRoleMappingDto toDto(AffiliateCategoryMap map) {
PartCategory cat = map.getPartCategory();
return new PartRoleMappingDto(
map.getId(),
map.getPlatform(),
map.getSourceValue(), // partRole
cat != null ? cat.getSlug() : null, // categorySlug
cat != null ? cat.getGroupName() : null,
map.getNotes()
); );
} }
@PutMapping("/{id}")
public MerchantCategoryMappingDto updateMappingPut(
@PathVariable Integer id,
@RequestBody UpdateMerchantCategoryMappingRequest request
) {
// just delegate so POST & PUT behave the same
return updateMapping(id, request);
}
} }

View File

@@ -0,0 +1,123 @@
package group.goforward.ballistic.controllers.admin;
import group.goforward.ballistic.model.PartCategory;
import group.goforward.ballistic.model.PartRoleMapping;
import group.goforward.ballistic.repos.PartCategoryRepository;
import group.goforward.ballistic.repos.PartRoleMappingRepository;
import group.goforward.ballistic.web.dto.admin.AdminPartRoleMappingDto;
import group.goforward.ballistic.web.dto.admin.CreatePartRoleMappingRequest;
import group.goforward.ballistic.web.dto.admin.UpdatePartRoleMappingRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
@RestController
@RequestMapping("/api/admin/part-role-mappings")
@CrossOrigin
public class AdminPartRoleMappingController {
private final PartRoleMappingRepository partRoleMappingRepository;
private final PartCategoryRepository partCategoryRepository;
public AdminPartRoleMappingController(
PartRoleMappingRepository partRoleMappingRepository,
PartCategoryRepository partCategoryRepository
) {
this.partRoleMappingRepository = partRoleMappingRepository;
this.partCategoryRepository = partCategoryRepository;
}
// GET /api/admin/part-role-mappings?platform=AR-15
@GetMapping
public List<AdminPartRoleMappingDto> list(
@RequestParam(name = "platform", required = false) String platform
) {
List<PartRoleMapping> mappings;
if (platform != null && !platform.isBlank()) {
mappings = partRoleMappingRepository.findByPlatformOrderByPartRoleAsc(platform);
} else {
mappings = partRoleMappingRepository.findAll();
}
return mappings.stream()
.map(this::toDto)
.toList();
}
// POST /api/admin/part-role-mappings
@PostMapping
public AdminPartRoleMappingDto create(
@RequestBody CreatePartRoleMappingRequest request
) {
PartCategory category = partCategoryRepository.findBySlug(request.categorySlug())
.orElseThrow(() -> new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"PartCategory not found for slug: " + request.categorySlug()
));
PartRoleMapping mapping = new PartRoleMapping();
mapping.setPlatform(request.platform());
mapping.setPartRole(request.partRole());
mapping.setPartCategory(category);
mapping.setNotes(request.notes());
mapping = partRoleMappingRepository.save(mapping);
return toDto(mapping);
}
// PUT /api/admin/part-role-mappings/{id}
@PutMapping("/{id}")
public AdminPartRoleMappingDto update(
@PathVariable Integer id,
@RequestBody UpdatePartRoleMappingRequest request
) {
PartRoleMapping mapping = partRoleMappingRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Mapping not found"));
if (request.platform() != null) {
mapping.setPlatform(request.platform());
}
if (request.partRole() != null) {
mapping.setPartRole(request.partRole());
}
if (request.categorySlug() != null) {
PartCategory category = partCategoryRepository.findBySlug(request.categorySlug())
.orElseThrow(() -> new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"PartCategory not found for slug: " + request.categorySlug()
));
mapping.setPartCategory(category);
}
if (request.notes() != null) {
mapping.setNotes(request.notes());
}
mapping = partRoleMappingRepository.save(mapping);
return toDto(mapping);
}
// DELETE /api/admin/part-role-mappings/{id}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Integer id) {
if (!partRoleMappingRepository.existsById(id)) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Mapping not found");
}
partRoleMappingRepository.deleteById(id);
}
private AdminPartRoleMappingDto toDto(PartRoleMapping mapping) {
PartCategory cat = mapping.getPartCategory();
return new AdminPartRoleMappingDto(
mapping.getId(),
mapping.getPlatform(),
mapping.getPartRole(),
cat != null ? cat.getSlug() : null,
cat != null ? cat.getGroupName() : null,
mapping.getNotes()
);
}
}

View File

@@ -0,0 +1,98 @@
// src/main/java/group/goforward/ballistic/model/CategoryMapping.java
package group.goforward.ballistic.model;
import jakarta.persistence.*;
import java.time.OffsetDateTime;
@Entity
@Table(name = "category_mappings")
public class CategoryMapping {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "merchant_id", nullable = false)
private Merchant merchant;
@Column(name = "raw_category_path", nullable = false)
private String rawCategoryPath;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "part_category_id")
private PartCategory partCategory;
@Column(name = "created_at", nullable = false)
private OffsetDateTime createdAt = OffsetDateTime.now();
@Column(name = "updated_at", nullable = false)
private OffsetDateTime updatedAt = OffsetDateTime.now();
@PrePersist
public void onCreate() {
OffsetDateTime now = OffsetDateTime.now();
if (createdAt == null) {
createdAt = now;
}
if (updatedAt == null) {
updatedAt = now;
}
}
@PreUpdate
public void onUpdate() {
this.updatedAt = OffsetDateTime.now();
}
// --- getters & setters ---
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Merchant getMerchant() {
return merchant;
}
public void setMerchant(Merchant merchant) {
this.merchant = merchant;
}
public String getRawCategoryPath() {
return rawCategoryPath;
}
public void setRawCategoryPath(String rawCategoryPath) {
this.rawCategoryPath = rawCategoryPath;
}
public PartCategory getPartCategory() {
return partCategory;
}
public void setPartCategory(PartCategory partCategory) {
this.partCategory = partCategory;
}
public OffsetDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(OffsetDateTime createdAt) {
this.createdAt = createdAt;
}
public OffsetDateTime getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(OffsetDateTime updatedAt) {
this.updatedAt = updatedAt;
}
}

View File

@@ -0,0 +1,65 @@
package group.goforward.ballistic.model;
import jakarta.persistence.*;
@Entity
@Table(name = "part_role_mappings")
public class PartRoleMapping {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false)
private String platform; // e.g. "AR-15"
@Column(name = "part_role", nullable = false)
private String partRole; // e.g. "UPPER", "BARREL", etc.
@ManyToOne(optional = false)
@JoinColumn(name = "part_category_id")
private PartCategory partCategory;
@Column(columnDefinition = "text")
private String notes;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public String getPartRole() {
return partRole;
}
public void setPartRole(String partRole) {
this.partRole = partRole;
}
public PartCategory getPartCategory() {
return partCategory;
}
public void setPartCategory(PartCategory partCategory) {
this.partCategory = partCategory;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
}

View File

@@ -1,29 +1,22 @@
package group.goforward.ballistic.repos; package group.goforward.ballistic.repos;
import group.goforward.ballistic.model.AffiliateCategoryMap; import group.goforward.ballistic.model.CategoryMapping;
import group.goforward.ballistic.model.Merchant;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List; import java.util.List;
import java.util.Optional;
public interface CategoryMappingRepository extends JpaRepository<AffiliateCategoryMap, Integer> { public interface CategoryMappingRepository extends JpaRepository<CategoryMapping, Integer> {
// Match by source_type + source_value + platform (case-insensitive) // All mappings for a merchant, ordered nicely
Optional<AffiliateCategoryMap> findBySourceTypeAndSourceValueAndPlatformIgnoreCase( List<CategoryMapping> findByMerchantIdOrderByRawCategoryPathAsc(Integer merchantId);
String sourceType,
String sourceValue,
String platform
);
// Fallback: match by source_type + source_value when platform is null/ignored // Merchants that actually have mappings (for the dropdown)
Optional<AffiliateCategoryMap> findBySourceTypeAndSourceValueIgnoreCase( @Query("""
String sourceType, select distinct cm.merchant
String sourceValue from CategoryMapping cm
); order by cm.merchant.name asc
""")
// Used by AdminCategoryMappingController: list mappings for a given source_type + platform List<Merchant> findDistinctMerchantsWithMappings();
List<AffiliateCategoryMap> findBySourceTypeAndPlatformOrderById(
String sourceType,
String platform
);
} }

View File

@@ -0,0 +1,12 @@
package group.goforward.ballistic.repos;
import group.goforward.ballistic.model.PartRoleMapping;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface PartRoleMappingRepository extends JpaRepository<PartRoleMapping, Integer> {
// List mappings for a platform, ordered nicely for the UI
List<PartRoleMapping> findByPlatformOrderByPartRoleAsc(String platform);
}

View File

@@ -1,16 +0,0 @@
package group.goforward.ballistic.services;
import group.goforward.ballistic.model.Brand;
import java.util.List;
import java.util.Optional;
public interface BrandService {
List<Brand> findAll();
Optional<Brand> findById(Integer id);
Brand save(Brand item);
void deleteById(Integer id);
}

View File

@@ -1,8 +1,7 @@
package group.goforward.ballistic.services; package group.goforward.ballistic.services;
import group.goforward.ballistic.model.AffiliateCategoryMap;
import group.goforward.ballistic.model.PartCategory; import group.goforward.ballistic.model.PartCategory;
import group.goforward.ballistic.repos.CategoryMappingRepository; import group.goforward.ballistic.repos.PartCategoryRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Optional; import java.util.Optional;
@@ -10,29 +9,33 @@ import java.util.Optional;
@Service @Service
public class PartCategoryResolverService { public class PartCategoryResolverService {
private final CategoryMappingRepository categoryMappingRepository; private final PartCategoryRepository partCategoryRepository;
public PartCategoryResolverService(CategoryMappingRepository categoryMappingRepository) { public PartCategoryResolverService(PartCategoryRepository partCategoryRepository) {
this.categoryMappingRepository = categoryMappingRepository; this.partCategoryRepository = partCategoryRepository;
} }
/** /**
* Resolve a part category from a platform + partRole (what gunbuilder cares about). * Resolve the canonical PartCategory for a given platform + partRole.
* Returns Optional.empty() if we have no mapping yet. *
* For now we keep it simple:
* - We treat partRole as the slug (e.g. "barrel", "upper", "trigger").
* - Normalize to lower-kebab (spaces -> dashes, lowercased).
* - Look up by slug in part_categories.
*
* Later, if we want per-merchant / per-platform overrides using category_mappings,
* we can extend this method without changing callers.
*/ */
public Optional<PartCategory> resolveForPlatformAndPartRole(String platform, String partRole) { public Optional<PartCategory> resolveForPlatformAndPartRole(String platform, String partRole) {
// sourceType is a convention you can also enum this if (partRole == null || partRole.isBlank()) {
String sourceType = "PART_ROLE"; return Optional.empty();
}
// First try with platform String normalizedSlug = partRole
return categoryMappingRepository .trim()
.findBySourceTypeAndSourceValueAndPlatformIgnoreCase(sourceType, partRole, platform) .toLowerCase()
.map(AffiliateCategoryMap::getPartCategory) .replace(" ", "-");
// if that fails, fall back to ANY platform
.or(() -> return partCategoryRepository.findBySlug(normalizedSlug);
categoryMappingRepository
.findBySourceTypeAndSourceValueIgnoreCase(sourceType, partRole)
.map(AffiliateCategoryMap::getPartCategory)
);
} }
} }

View File

@@ -1,38 +0,0 @@
package group.goforward.ballistic.services.impl;
import group.goforward.ballistic.model.Brand;
import group.goforward.ballistic.repos.BrandRepository;
import group.goforward.ballistic.services.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class BrandServiceImpl implements BrandService {
@Autowired
private BrandRepository repo;
@Override
public List<Brand> findAll() {
return repo.findAll();
}
@Override
public Optional<Brand> findById(Integer id) {
return repo.findById(id);
}
@Override
public Brand save(Brand item) {
return null;
}
@Override
public void deleteById(Integer id) {
deleteById(id);
}
}

View File

@@ -0,0 +1,10 @@
package group.goforward.ballistic.web.dto.admin;
public record AdminPartRoleMappingDto(
Integer id,
String platform,
String partRole,
String categorySlug,
String groupName,
String notes
) {}

View File

@@ -0,0 +1,8 @@
package group.goforward.ballistic.web.dto.admin;
public record CreatePartRoleMappingRequest(
String platform,
String partRole,
String categorySlug,
String notes
) {}

View File

@@ -0,0 +1,11 @@
// src/main/java/group/goforward/ballistic/web/dto/admin/MerchantCategoryMappingDto.java
package group.goforward.ballistic.web.dto.admin;
public record MerchantCategoryMappingDto(
Integer id,
Integer merchantId,
String merchantName,
String rawCategoryPath,
Integer partCategoryId,
String partCategoryName
) {}

View File

@@ -0,0 +1,6 @@
package group.goforward.ballistic.web.dto.admin;
public record SimpleMerchantDto(
Integer id,
String name
) { }

View File

@@ -0,0 +1,5 @@
package group.goforward.ballistic.web.dto.admin;
public record UpdateMerchantCategoryMappingRequest(
Integer partCategoryId
) {}

View File

@@ -0,0 +1,8 @@
package group.goforward.ballistic.web.dto.admin;
public record UpdatePartRoleMappingRequest(
String platform,
String partRole,
String categorySlug,
String notes
) {}

View File