Skip to content

Commit

Permalink
Merge pull request #8 from malekch-15/back2dto
Browse files Browse the repository at this point in the history
Back2dto
  • Loading branch information
malekch-15 authored Nov 15, 2024
2 parents edcbc59 + 5c28d02 commit 6b87565
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 76 deletions.
46 changes: 38 additions & 8 deletions backend/src/main/java/reacp/controller/RestaurantController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package reacp.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import reacp.model.RestaurantModel;
import reacp.model.RestaurantModelDto;
import reacp.services.RestaurantService;

import java.util.List;
Expand All @@ -15,7 +15,8 @@ public class RestaurantController {

private final RestaurantService restaurantService;

@GetMapping

@GetMapping()
public List<RestaurantModel> getAllRestaurants(){
return restaurantService.getAllRestaurants();
}
Expand All @@ -27,25 +28,54 @@ public RestaurantModel getRestaurantById(@PathVariable String id){

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public RestaurantModel postRestaurant(@RequestBody RestaurantModel restaurantModel){
return restaurantService.addRestaurant(restaurantModel);
public RestaurantModel postRestaurant(@RequestBody RestaurantModelDto restaurantModelDto){
return restaurantService.addRestaurant(
new RestaurantModel(
null,
restaurantModelDto.name(),
restaurantModelDto.city(),
restaurantModelDto.category(),
restaurantModelDto.description(),
restaurantModelDto.status()
));
}


@PostMapping("/update-all")
public List<RestaurantModel> updateAll(@RequestBody List<RestaurantModel> restaurantModels){
return restaurantService.updateAll(restaurantModels);
}



@PutMapping("/{id}")
public RestaurantModel putRestaurant(@PathVariable String id, @RequestBody RestaurantModel restaurantModel){
return restaurantService.updateRestaurantWithPut(id, restaurantModel);
public RestaurantModel putRestaurant(@PathVariable String id, @RequestBody RestaurantModelDto restaurantModeldto){
return restaurantService.updateRestaurantWithPut(id,
new RestaurantModel(
id,
restaurantModeldto.name(),
restaurantModeldto.city(),
restaurantModeldto.category(),
restaurantModeldto.description(),
restaurantModeldto.status()
));
}


@PatchMapping("/{id}")
public RestaurantModel patchRestaurant(@PathVariable String id, @RequestBody RestaurantModel restaurantModel){
return restaurantService.updateRestaurantWithPatch(id, restaurantModel);
public RestaurantModel patchRestaurant(@PathVariable String id, @RequestBody RestaurantModelDto restaurantModeldto){
return restaurantService.updateRestaurantWithPatch(id,
new RestaurantModel(
id,
restaurantModeldto.name(),
restaurantModeldto.city(),
restaurantModeldto.category(),
restaurantModeldto.description(),
restaurantModeldto.status()
));
}


@DeleteMapping("/{id}")
public void deleteRestaurant(@PathVariable String id){
restaurantService.deleteRestaurant(id);
Expand Down
10 changes: 10 additions & 0 deletions backend/src/main/java/reacp/model/RestaurantModelDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package reacp.model;

public record RestaurantModelDto(
String name,
String city,
String category,
String description,
WishlistStatus status
) {
}
9 changes: 2 additions & 7 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import './App.css'
import RestaurantCard from "./components/RestaurantCard.tsx";
import Details from "./components/Details.tsx";
import {BrowserRouter as Router, Route, Routes} from "react-router-dom";
import {BrowserRouter as Router} from "react-router-dom";

export default function App() {
return (
<>
<Router>
<Routes>
<Route path="/" element={<RestaurantCard />} />
<Route path="/restaurant/:id" element={<Details />} />
</Routes>
<h2>App</h2>
</Router>
</>
);
Expand Down
10 changes: 0 additions & 10 deletions frontend/src/components/Details.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@

export default function Details(){

return (
<>
<h2>Details</h2>
<p>Details of the restaurant</p>
</>
);
}
Empty file.
Empty file.
Empty file.
Empty file.
51 changes: 0 additions & 51 deletions frontend/src/components/RestaurantCard.tsx
Original file line number Diff line number Diff line change
@@ -1,51 +0,0 @@
import {Restaurant} from "./Restaurant.ts";
import axios from "axios";
import {useEffect, useState} from "react";
import {WishlistStatus} from "./WishlistStatus.ts";

type Props = {
restaurant: Restaurant;
onAddToWishlist: (id: string) => void;
onRemoveFromWishlist: (id: string) => void;
wishlistStatus: WishlistStatus;
onRestaurantUpdate: (restaurant: Restaurant) => void;
}

export default function RestaurantCard() {

const [restaurants, setRestaurants] = useState<Restaurant[]>([]);

function fetchRestaurants() {
axios.get("api/restaurant")
.then((response) => {
setRestaurants(response.data);
})
.catch((error) => console.error("Error fetching restaurants:", error));
}

useEffect(fetchRestaurants, []);

if(!restaurants) {
return "Loading...";
}


return (
<>
<div className={"page"}>
<h1>Restaurants</h1>
<div className={"restaurant-list"}>
{restaurants.map((restaurant) => (
<div key={restaurant.id} className={"restaurant-card"}>
<h2>{restaurant.name}</h2>
<p>{restaurant.city}</p>
<p>{restaurant.category}</p>
<p>{restaurant.description}</p>
</div>
))}
</div>

</div>
</>
);
}
Empty file.
File renamed without changes.
File renamed without changes.

0 comments on commit 6b87565

Please sign in to comment.