Fonctionalité CRUD pour restaurant et amélioration du model
This commit is contained in:
parent
9f8c9aec80
commit
1cec457011
4 changed files with 189 additions and 1 deletions
|
@ -0,0 +1,91 @@
|
||||||
|
package fr.vincent.ramiere.mangerautourdesimplonback.controller;
|
||||||
|
|
||||||
|
import fr.vincent.ramiere.mangerautourdesimplonback.models.Restaurant;
|
||||||
|
import fr.vincent.ramiere.mangerautourdesimplonback.service.RestaurantService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controleur REST pour la gestion des restaurants.
|
||||||
|
* Expose les endpoints de l'API pour les opérations CRUD.
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/restaurants")
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class RestaurantController {
|
||||||
|
private final RestaurantService restaurantService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint pour récupérer la liste de tous les restaurants.
|
||||||
|
* @return une ResponseEntity contenant la liste des restaurants et le statut HTTP OK.
|
||||||
|
*/
|
||||||
|
@GetMapping
|
||||||
|
public ResponseEntity<List<Restaurant>> getAllRestaurants() {
|
||||||
|
List<Restaurant> restaurants = restaurantService.getAllRestaurants();
|
||||||
|
return new ResponseEntity<>(restaurants, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint pour récupérer un restaurant par son identifiant.
|
||||||
|
* @param id L'identifiant du restaurant.
|
||||||
|
* @return une ResponseEntity contenant le restaurant s'il est trouvé (OK), sinon NOT_FOUND.
|
||||||
|
*/
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<Restaurant> getRestaurantById(@PathVariable Integer id) {
|
||||||
|
return restaurantService.getRestaurantById(id)
|
||||||
|
.map(restaurant -> new ResponseEntity<>(restaurant, HttpStatus.OK))
|
||||||
|
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint pour créer un nouveau restaurant.
|
||||||
|
* @param restaurant Le restaurant à créer.
|
||||||
|
* @return une ResponseEntity contenant le restaurant créé et le statut CREATED.
|
||||||
|
*/
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<Restaurant> createRestaurant(@RequestBody Restaurant restaurant) {
|
||||||
|
Restaurant savedRestaurant = restaurantService.saveRestaurant(restaurant);
|
||||||
|
return new ResponseEntity<>(savedRestaurant, HttpStatus.CREATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint pour mettre à jour un restaurant existant.
|
||||||
|
* @param id L'identifiant du restaurant à mettre à jour.
|
||||||
|
* @param restaurantDetails Les nouvelles données du restaurant.
|
||||||
|
* @return une ResponseEntity contenant le restaurant mis à jour (OK), sinon NOT_FOUND.
|
||||||
|
*/
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public ResponseEntity<Restaurant> updateRestaurant(@PathVariable Integer id, @RequestBody Restaurant restaurantDetails) {
|
||||||
|
return restaurantService.updateRestaurant(id, restaurantDetails)
|
||||||
|
.map(updateRestaurant -> new ResponseEntity<>(updateRestaurant, HttpStatus.OK))
|
||||||
|
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint pour supprimer un restaurant.
|
||||||
|
* @param id L'identifiant du restaurant à supprimer.
|
||||||
|
* @return une ResponseEntity avec le statut NO_CONTENT si la suppression réussit,
|
||||||
|
* ou NOT_FOUND si le restaurant n'existe pas.
|
||||||
|
*/
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> deleteRestaurant(@PathVariable Integer id) {
|
||||||
|
if (restaurantService.getRestaurantById(id).isPresent()) {
|
||||||
|
restaurantService.deleteRestaurant(id);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,7 +13,10 @@ import java.util.Collection;
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "restaurant")
|
@Table(name = "restaurant")
|
||||||
@Data
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@ToString(exclude = {"typerestaus", "preference"})
|
||||||
|
@EqualsAndHashCode(exclude = {"typerestaus", "preference"})
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Builder
|
@Builder
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package fr.vincent.ramiere.mangerautourdesimplonback.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import fr.vincent.ramiere.mangerautourdesimplonback.models.Restaurant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dépot pour l'entité Restaurant.
|
||||||
|
* Fournit les méthodes pour les opérations CRUD.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface RestaurantRepository extends JpaRepository<Restaurant, Integer>{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
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<Restaurant> 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<Restaurant> 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<Restaurant> 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue