PHP Classes

File: api/product.php

Recommend this page to a friend!
  Classes of Okanlawon Anuoluwapo   PHP MongoDB CRUD Example of API   api/product.php   Download  
File: api/product.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP MongoDB CRUD Example of API
API that provides CRUD operations with MongoDB
Author: By
Last change:
Date: 8 months ago
Size: 1,614 bytes
 

Contents

Class file image Download
<?php
require __DIR__ . '/config.php';

$product = new Product();

if (
$_SERVER['REQUEST_METHOD'] === "GET" ) :
    try {
        if ( isset(
$_GET['name']) ) {
           
$result = $product->findProduct( trim($_GET['name']) );
        } else {
           
$result = $product->findAllProduct();
        }
       
HttpResponse::OK($result);
    } catch (\
Throwable $th) {
       
HttpResponse::badRequest($th->getMessage());
    }
endif;


if (
$_SERVER['REQUEST_METHOD'] === "POST" ) :
   
$data = json_decode(file_get_contents('php://input'),true);
    try {
       
$result = $product->createProduct(array(
           
'name' => $data['name'],
           
'image' => $data['image'],
        ));
       
// $result = $product->loadProducts($data['products']);
       
HttpResponse::Created($result);
    } catch (\
Throwable $th) {
       
HttpResponse::badRequest($th->getMessage());
    }
endif;

if (
$_SERVER['REQUEST_METHOD'] === "PATCH" ) :
  
$data = json_decode(file_get_contents('php://input'),true);
   try {
       
$result = $product->updateProduct(array(
           
'name' => $data['name'],
           
'image' => $data['image'],
        ));

       
HttpResponse::Ok($result);
    } catch (\
Throwable $th) {
       
HttpResponse::badRequest($th->getMessage());
    }
endif;


if (
$_SERVER['REQUEST_METHOD'] === "DELETE" ) :
   
$data = json_decode(file_get_contents('php://input'),true);
    try {
       
$result = $product->deleteProduct($data['name']);

       
HttpResponse::Ok($result);
    } catch (\
Throwable $th) {
       
HttpResponse::badRequest($th->getMessage());
    }
endif;