rental_prices.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. header("Content-Type: application/json; charset=UTF-8");
  4. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  5. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  6. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  7. exit(0);
  8. }
  9. require_once '../config/database.php';
  10. require_once '../models/RentalPrice.php';
  11. $database = new Database();
  12. $db = $database->getConnection();
  13. $rentalPrice = new RentalPrice($db);
  14. $request_method = $_SERVER['REQUEST_METHOD'];
  15. switch($request_method) {
  16. case 'GET':
  17. if(isset($_GET['id'])) {
  18. $rentalPrice->id = $_GET['id'];
  19. $rentalPrice->readOne();
  20. if($rentalPrice->item_id != null) {
  21. $rental_arr = array(
  22. "id" => $rentalPrice->id,
  23. "item_id" => $rentalPrice->item_id,
  24. "client_id" => $rentalPrice->client_id,
  25. "start_date" => $rentalPrice->start_date,
  26. "end_date" => $rentalPrice->end_date,
  27. "daily_price" => $rentalPrice->daily_price,
  28. "created_at" => $rentalPrice->created_at,
  29. "updated_at" => $rentalPrice->updated_at
  30. );
  31. http_response_code(200);
  32. echo json_encode($rental_arr);
  33. } else {
  34. http_response_code(404);
  35. echo json_encode(array("message" => "Rental price not found."));
  36. }
  37. } elseif(isset($_GET['item_id'])) {
  38. $rentalPrice->item_id = $_GET['item_id'];
  39. $stmt = $rentalPrice->read();
  40. $num = $stmt->rowCount();
  41. if($num > 0) {
  42. $rentals_arr = array();
  43. $rentals_arr["records"] = array();
  44. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  45. extract($row);
  46. $rental_item = array(
  47. "id" => $id,
  48. "item_id" => $item_id,
  49. "client_id" => $client_id,
  50. "start_date" => $start_date,
  51. "end_date" => $end_date,
  52. "daily_price" => $daily_price,
  53. "created_at" => $created_at,
  54. "updated_at" => $updated_at
  55. );
  56. array_push($rentals_arr["records"], $rental_item);
  57. }
  58. http_response_code(200);
  59. echo json_encode($rentals_arr);
  60. } else {
  61. http_response_code(200);
  62. echo json_encode(array("records" => array()));
  63. }
  64. } else {
  65. http_response_code(400);
  66. echo json_encode(array("message" => "Missing item_id parameter."));
  67. }
  68. break;
  69. case 'POST':
  70. $data = json_decode(file_get_contents("php://input"));
  71. if(!empty($data->item_id) && !empty($data->start_date) && !empty($data->end_date) && !empty($data->daily_price)) {
  72. $rentalPrice->item_id = $data->item_id;
  73. $rentalPrice->client_id = $data->client_id ?? null;
  74. $rentalPrice->start_date = $data->start_date;
  75. $rentalPrice->end_date = $data->end_date;
  76. $rentalPrice->daily_price = $data->daily_price;
  77. if($rentalPrice->create()) {
  78. http_response_code(201);
  79. echo json_encode(array("message" => "Rental price was created."));
  80. } else {
  81. http_response_code(503);
  82. echo json_encode(array("message" => "Unable to create rental price."));
  83. }
  84. } else {
  85. http_response_code(400);
  86. echo json_encode(array("message" => "Unable to create rental price. Data is incomplete."));
  87. }
  88. break;
  89. case 'PUT':
  90. $data = json_decode(file_get_contents("php://input"));
  91. if(!empty($data->id) && !empty($data->item_id) && !empty($data->start_date) && !empty($data->end_date) && !empty($data->daily_price)) {
  92. $rentalPrice->id = $data->id;
  93. $rentalPrice->item_id = $data->item_id;
  94. $rentalPrice->client_id = $data->client_id ?? null;
  95. $rentalPrice->start_date = $data->start_date;
  96. $rentalPrice->end_date = $data->end_date;
  97. $rentalPrice->daily_price = $data->daily_price;
  98. if($rentalPrice->update()) {
  99. http_response_code(200);
  100. echo json_encode(array("message" => "Rental price was updated."));
  101. } else {
  102. http_response_code(503);
  103. echo json_encode(array("message" => "Unable to update rental price."));
  104. }
  105. } else {
  106. http_response_code(400);
  107. echo json_encode(array("message" => "Unable to update rental price. Data is incomplete."));
  108. }
  109. break;
  110. case 'DELETE':
  111. if(isset($_GET['id'])) {
  112. $rentalPrice->id = $_GET['id'];
  113. if($rentalPrice->delete()) {
  114. http_response_code(200);
  115. echo json_encode(array("message" => "Rental price was deleted."));
  116. } else {
  117. http_response_code(503);
  118. echo json_encode(array("message" => "Unable to delete rental price."));
  119. }
  120. } else {
  121. http_response_code(400);
  122. echo json_encode(array("message" => "Unable to delete rental price. ID is missing."));
  123. }
  124. break;
  125. default:
  126. http_response_code(405);
  127. echo json_encode(array("message" => "Method not allowed."));
  128. break;
  129. }
  130. ?>