feat: add firearm name lookup for webhook YAML parsing
When firearmId is absent from the YAML block, resolveFirearmId falls back to firearmName lookup via FirearmRepository.findByName(). If both are present, firearmId takes precedence.
This commit is contained in:
@@ -71,6 +71,21 @@ public class ModificationManager {
|
|||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long resolveFirearmId(Long firearmId, String firearmName) {
|
||||||
|
if (firearmId != null) {
|
||||||
|
return firearmId;
|
||||||
|
}
|
||||||
|
if (firearmName == null || firearmName.isBlank()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
var matches = firearmRepository.findByName(firearmName);
|
||||||
|
if (matches.isEmpty()) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
|
||||||
|
"Firearm not found by name: " + firearmName);
|
||||||
|
}
|
||||||
|
return matches.getFirst().getId();
|
||||||
|
}
|
||||||
|
|
||||||
private Modification toEntity(ModificationRequest request, Firearm firearm) {
|
private Modification toEntity(ModificationRequest request, Firearm firearm) {
|
||||||
return Modification.builder()
|
return Modification.builder()
|
||||||
.firearm(firearm)
|
.firearm(firearm)
|
||||||
|
|||||||
@@ -7,9 +7,13 @@ import org.springframework.data.domain.Pageable;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface FirearmRepository extends JpaRepository<Firearm, Long> {
|
public interface FirearmRepository extends JpaRepository<Firearm, Long> {
|
||||||
|
|
||||||
Page<Firearm> findAllByType(FirearmType type, Pageable pageable);
|
Page<Firearm> findAllByType(FirearmType type, Pageable pageable);
|
||||||
|
|
||||||
|
List<Firearm> findByName(String name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -109,7 +109,9 @@ public class WebhookService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ModificationRequest mapToRequest(Map<String, Object> data) {
|
private ModificationRequest mapToRequest(Map<String, Object> data) {
|
||||||
Long firearmId = toLong(data.get("firearmId"));
|
Long firearmId = modificationManager.resolveFirearmId(
|
||||||
|
toLong(data.get("firearmId")),
|
||||||
|
(String) data.get("firearmName"));
|
||||||
String name = (String) data.get("name");
|
String name = (String) data.get("name");
|
||||||
String code = (String) data.get("code");
|
String code = (String) data.get("code");
|
||||||
List<String> tags = toStringList(data.get("tags"));
|
List<String> tags = toStringList(data.get("tags"));
|
||||||
|
|||||||
Reference in New Issue
Block a user