Skip to content

Commit

Permalink
Merge branch 'delete-dish/restaurant'
Browse files Browse the repository at this point in the history
  • Loading branch information
milenaaluisa committed Jun 14, 2022
1 parent 3fc5199 commit 15eb533
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 18 deletions.
23 changes: 23 additions & 0 deletions src/api/api_delete_dish.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types = 1);

session_start();

require_once('../database/connection.db.php');
require_once('../database/user.class.php');
require_once('../database/dish.class.php');

if (!isset($_SESSION['idUser'])) {
die();
}

$db = getDatabaseConnection();

$dish = Dish::getDish($db, intval($_GET['id']));

if ($dish && User::canEditDish($db, intval($_GET['id']), intval($_SESSION['idUser']))) {
$dish->deleteDish($db);
}

?>
8 changes: 4 additions & 4 deletions src/css/dishes_list.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
transform: scale(1.08);
}

#dishes > article > .close, #dishes > article > .composition, #dishes > article > form {
#dishes > article > .close, #dishes > article > .composition, #dishes > article > form {
display: none;
}/*----------------------------------*/

Expand All @@ -37,7 +37,7 @@
#dishes > article {
display: grid;
grid-template-columns: 1fr auto;
grid-template-rows: repeat(3, auto);
grid-template-rows: repeat(4, auto);
}

#dishes > article > a:nth-child(2) {
Expand All @@ -53,8 +53,8 @@
}

#dishes > article > .edit_options {
grid-column: 2; grid-row: 3;
padding: 0.5em;
grid-column: 1/3; grid-row: 4;
margin-top: 0.5em;
}

#dishes > article > .price {
Expand Down
18 changes: 11 additions & 7 deletions src/css/edit_restaurant.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@
border-radius: var(--default-border-radius);
}

#dishes > article > .edit_options {
display: block;
padding: 0.5em;
#dishes > article > .edit_options {
display: grid;
column-gap: 1em;
grid-template-columns: repeat(auto-fit, 7em);
justify-content: right;
text-align: center;
}

#dishes > article > .edit_options > * {
background-color: var(--dark-bg-color);
border-radius: var(--default-border-radius);
padding: 0.5em;
}

/*dishes*/
Expand All @@ -52,10 +59,7 @@


/*---------------LAYOUT----------------*/
#retaurants > article > #dishes > article > .edit_options {
display: block;
grid-column: 2; grid-row: 3;
}


#restaurants > article {
display: grid;
Expand Down
14 changes: 7 additions & 7 deletions src/database/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ CREATE TABLE RateDish
(
rate INT NOT NULL CHECK(rate >= 1 and rate <= 5),
idFoodOrder INT NOT NULL REFERENCES FoodOrder(idFoodOrder),
idDish INT NOT NULL REFERENCES Dish(idDish),
idDish INT NOT NULL REFERENCES Dish(idDish) ON DELETE CASCADE,
PRIMARY KEY (idFoodOrder, idDish)
);

Expand All @@ -129,13 +129,13 @@ CREATE TABLE Photo
idRestaurant INTEGER REFERENCES Restaurant(idRestaurant),
idReview INT REFERENCES Review(idReview),
idUser INT REFERENCES User(idUser),
idDish INT REFERENCES Dish(idDish)
idDish INT REFERENCES Dish(idDish) ON DELETE CASCADE
);

CREATE TABLE MenuDish
(
idMenu INT NOT NULL REFERENCES Menu(idMenu),
idDish INT NOT NULL REFERENCES Dish(idDish),
idDish INT NOT NULL REFERENCES Dish(idDish) ON DELETE CASCADE,
PRIMARY KEY (idMenu, idDish)
);

Expand All @@ -147,7 +147,7 @@ CREATE TABLE Allergen

CREATE TABLE DishAllergen
(
idDish INT NOT NULL REFERENCES Dish(idDish),
idDish INT NOT NULL REFERENCES Dish(idDish) ON DELETE CASCADE,
idAllergen INT NOT NULL REFERENCES Allergen(idAllergen),
PRIMARY KEY(idDish, idAllergen)
);
Expand All @@ -157,7 +157,7 @@ CREATE TABLE Selection
quantity INT NOT NULL,
extras VARCHAR,
idFoodOrder INT NOT NULL REFERENCES FoodOrder(idFoodOrder),
idDish INT NOT NULL REFERENCES Dish(idDish),
idDish INT NOT NULL REFERENCES Dish(idDish) ON DELETE CASCADE,
PRIMARY KEY (idFoodOrder, idDish)
);

Expand All @@ -177,13 +177,13 @@ CREATE TABLE FavRestaurant
);

CREATE TABLE FavDish(
idDish INT NOT NULL REFERENCES Dish(idDish),
idDish INT NOT NULL REFERENCES Dish(idDish) ON DELETE CASCADE,
idUser INT NOT NULL REFERENCES User(idUser),
PRIMARY KEY (idDish, idUser)
);

CREATE TABLE DishCategory(
idDish INT NOT NULL REFERENCES Dish(idDish),
idDish INT NOT NULL REFERENCES Dish(idDish) ON DELETE CASCADE,
idCategory INT NOT NULL REFERENCES Category(idCategory),
PRIMARY KEY(idDish, idCategory)
);
Expand Down
5 changes: 5 additions & 0 deletions src/database/dish.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ function updateDishInfo(PDO $db) {
WHERE idDish = ?');
$stmt->execute(array($this->name, $this->ingredients, $this->price, $this->idDish));
}

function deleteDish(PDO $db) {
$stmt = $db->prepare('DELETE FROM Dish WHERE idDish = ?');
$stmt->execute(array($this->idDish));
}
}

?>
24 changes: 24 additions & 0 deletions src/javascript/delete_dish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function deleteDish() {
const buttons = document.querySelectorAll('#dishes > article > .edit_options > button');

buttons.forEach(function(button, i){
console.log(button)
button.addEventListener('click', function(f){
idDish = button.getAttribute('data-id');

let request = new XMLHttpRequest()
var url = "../api/api_delete_dish.php?id=" + idDish

request.open("GET", url, true)
request.send()

request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
location.reload()
}
}
})
})
}

deleteDish();
1 change: 1 addition & 0 deletions src/templates/common.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function output_header(array $css_files = null) { ?>
<script src="../javascript/order_state.js" defer></script>
<script src="../javascript/favorite_restaurant.js" defer></script>
<script src="../javascript/favorite_dish.js" defer></script>
<script src="../javascript/delete_dish.js" defer></script>
<link href="../css/style.css" rel="stylesheet">
<link href="../css/layout.css" rel="stylesheet">
<link href="../css/forms.css" rel="stylesheet">
Expand Down
2 changes: 2 additions & 0 deletions src/templates/dish.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ function output_dish(Dish $dish, array $allergens = null) { ?>

<div class = "edit_options">
<a href="edit_dish_info.php?id=<?=$dish->idDish?>">Edit Info <i class="fa-regular fa-pen-to-square"></i></a>
<button data-id="<?= $dish->idDish ?>">Delete dish</button>

</div>

<?php if((isset($dish->ingredients) && !empty(trim($dish->ingredients))) || isset($allergens) && sizeof($allergens)>0) { ?>
Expand Down

0 comments on commit 15eb533

Please sign in to comment.