Develop #1
@@ -71,6 +71,21 @@ public class ModificationManager {
|
||||
.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) {
|
||||
return Modification.builder()
|
||||
.firearm(firearm)
|
||||
|
||||
@@ -7,9 +7,13 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface FirearmRepository extends JpaRepository<Firearm, Long> {
|
||||
|
||||
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) {
|
||||
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 code = (String) data.get("code");
|
||||
List<String> tags = toStringList(data.get("tags"));
|
||||
|
||||
Reference in New Issue
Block a user