package fr.vincent.ramiere.mangerautourdesimplonback.service; import fr.vincent.ramiere.mangerautourdesimplonback.models.Restaurant; import fr.vincent.ramiere.mangerautourdesimplonback.repository.RestaurantRepository; import java.util.List; import java.util.Optional; import org.springframework.stereotype.Service; import lombok.AllArgsConstructor; /** * Service pour la gestion des restaurants. * Contient la logique métier liée aux opérations sur les restaurants. */ @Service @AllArgsConstructor public class RestaurantService { private final RestaurantRepository restaurantRepository; /** * Récupère tous les restaurants. * @return une liste de tous les restaurants. */ public List getAllRestaurants() { return restaurantRepository.findAll(); } /** * Récupère un restaurant par son identifiant. * @param id L'identifiant du restaurant. * @return un Optional contenant le restaurant s'il est trouvé, sinon un Optional vide. */ public Optional getRestaurantById(Integer id) { return restaurantRepository.findById(id); } /** * Enregistre un nouveau restaurant. * @param restaurant Le restaurant à enregistrer. * @return le restaurant enregistré */ public Restaurant saveRestaurant(Restaurant restaurant) { return restaurantRepository.save(restaurant); } /** * Met à jour un restaurant existant. * @param id L'identifiant du restaurant à mettre à jour. * @param restaurantDetails Les nouvelles informations du restaurant. * @return le restaurant mis à jour. */ public Optional updateRestaurant(Integer id, Restaurant restaurantDetails) { return restaurantRepository.findById(id) .map(existingRestaurant -> { existingRestaurant.setNom(restaurantDetails.getNom()); existingRestaurant.setAdresse(restaurantDetails.getAdresse()); existingRestaurant.setTelephone(restaurantDetails.getTelephone()); existingRestaurant.setAccesPMR(restaurantDetails.getAccesPMR()); existingRestaurant.setAEmporter(restaurantDetails.getAEmporter()); existingRestaurant.setSurPlace(restaurantDetails.getSurPlace()); existingRestaurant.setPrix(restaurantDetails.getPrix()); existingRestaurant.setLatitude(restaurantDetails.getLatitude()); existingRestaurant.setLongitude(restaurantDetails.getLongitude()); existingRestaurant.setWebsite(restaurantDetails.getWebsite()); return restaurantRepository.save(existingRestaurant); }); } /** * Supprime un restaurant par son identifiant. * @param id L'identifiant du restaurant à supprimer. */ public void deleteRestaurant(Integer id) { restaurantRepository.deleteById(id); } }