Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
839 B
2
Indexable
Never
 @PostMapping("/add-address")
    public AddressEntity createAddress(
            @RequestParam("street") String street,
            @RequestParam("city") String cityName,
            @RequestParam("country") String countryName
    ) {
        CityEntity city = cityRepository.findCityEntityByName(cityName)
                .orElseGet(() -> {
                    CountryEntity country = countryRepository.findCountryEntityByName(countryName)
                            .orElseGet(() -> countryRepository.save(new CountryEntity(countryName)));
                    return cityRepository.save(new CityEntity(cityName, country));
                });

        AddressEntity address = new AddressEntity();
        address.setStreet(street);
        address.setCity(city);

        return addressRepository.save(address);
    }
Leave a Comment