This commit is contained in:
Bozhidar Slaveykov 2024-04-05 15:29:35 +03:00
parent 63cb9bbd54
commit ed1c1df4f0
8 changed files with 185 additions and 23 deletions

View file

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\Request\CustomerCreateRequest;
use App\Http\Controllers\ApiController;
use App\Models\Customer;
use App\Models\HostingSubscription;
use Illuminate\Http\Request;
class CustomersController extends ApiController
@ -59,7 +60,6 @@ class CustomersController extends ApiController
$customer = new Customer();
$customer->name = $request->name;
$customer->email = $request->email;
$customer->phone = $request->phone;
$customer->save();
return response()->json([
@ -70,4 +70,31 @@ class CustomersController extends ApiController
]
]);
}
public function getHostingSubscriptionsByCustomerId($customerId)
{
$findCustomer = Customer::where('id', $customerId)->first();
if (!$findCustomer) {
return response()->json([
'status' => 'error',
'message' => 'Customer not found',
], 404);
}
$findHostingSubscriptions = HostingSubscription::where('customer_id', $customerId)->get();
if ($findHostingSubscriptions->isEmpty()) {
return response()->json([
'status' => 'error',
'message' => 'No hosting subscriptions found for this customer',
], 404);
}
return response()->json([
'status' => 'ok',
'message' => 'Hosting subscriptions found',
'data' => [
'hostingSubscriptions' => $findHostingSubscriptions,
]
]);
}
}

View file

@ -0,0 +1,74 @@
<?php
namespace app\Http\Controllers\Api;
use App\Http\Controllers\Api\Request\CustomerCreateRequest;
use App\Http\Controllers\ApiController;
use App\Models\Customer;
use App\Models\HostingPlan;
use Illuminate\Http\Request;
class HostingPlansController extends ApiController
{
/**
* @OA\Get(
* path="/api/hosting-plans",
* @OA\Response(
* response=200,
* description="Successful operation",
* ),
* @OA\PathItem (
* ),
* )
*/
public function index()
{
$findHostingPlans = HostingPlan::all();
return response()->json([
'status' => 'ok',
'message' => 'Hosting Plans found',
'data' => [
'hostingPlans' => $findHostingPlans->toArray()
]
]);
}
/**
* @OA\Post(
* path="/api/customers",
* @OA\Response(
* response=200,
* description="Successful operation",
* ),
* @OA\PathItem (
* ),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* required={"name","email","phone"},
* @OA\Property(property="name", type="string", example="John Doe", description="Name of the customer"),
* @OA\Property(property="email", type="string", example="jhon@gmail.com", description="Email of the customer"),
* @OA\Property(property="phone", type="string", example="1234567890", description="Phone of the customer")
* )
* )
* )
*/
public function store(CustomerCreateRequest $request)
{
$customer = new Customer();
$customer->name = $request->name;
$customer->email = $request->email;
$customer->phone = $request->phone;
$customer->save();
return response()->json([
'status' => 'ok',
'message' => 'Customer created',
'data' => [
'customer' => $customer,
]
]);
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\ApiController;
use App\Models\HostingSubscription;
use Carbon\Carbon;
use Illuminate\Http\Request;
class HostingSubscriptionsController extends ApiController
{
public function index()
{
$findHostingSubscription = HostingSubscription::all();
return response()->json([
'status' => 'ok',
'message' => 'Hosting subscriptions found',
'data' => [
'HostingSubscriptions' => $findHostingSubscription,
]
]);
}
public function store(Request $request)
{
$hostingSubscription = new HostingSubscription();
$hostingSubscription->customer_id = $request->customer_id;
$hostingSubscription->hosting_plan_id = $request->hosting_plan_id;
$hostingSubscription->domain = $request->domain;
// $hostingSubscription->username = $request->username;
// $hostingSubscription->password = $request->password;
// $hostingSubscription->description = $request->description;
$hostingSubscription->setup_date = Carbon::now();
$hostingSubscription->save();
return response()->json([
'status' => 'ok',
'message' => 'Hosting subscription created',
'data' => [
'hostingSubscription' => $hostingSubscription,
]
]);
}
public function destroy($id)
{
$findHostingSubscription = HostingSubscription::where('id', $id)->first();
if ($findHostingSubscription) {
$findHostingSubscription->delete();
return response()->json([
'status' => 'ok',
'message' => 'Hosting subscription deleted',
]);
}
return response()->json([
'status' => 'error',
'message' => 'Hosting subscription not found',
], 404);
}
}

View file

@ -11,6 +11,8 @@ class AuthorizedApiRequest extends ApiRequest
*/
public function authorize()
{
return true;
$user = auth()->user();
if ($user) {

View file

@ -12,8 +12,7 @@ class CustomerCreateRequest extends AuthorizedApiRequest
{
return [
'name' => 'required',
'email' => 'required|email|unique:customers,email',
'phone' => 'required',
'email' => 'required|email|unique:customers,email'
];
}
}

View file

@ -17,7 +17,8 @@ use Illuminate\Routing\Controller as BaseController;
*/
class ApiController extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
// use AuthorizesRequests, ValidatesRequests;
use ValidatesRequests;
}

View file

@ -1,11 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class HostingAccount extends Model
{
use HasFactory;
}

View file

@ -16,18 +16,25 @@ use Illuminate\Support\Facades\Route;
Route::get('health', [\App\Http\Controllers\Api\HealthController::class, 'index']);
Route::get('customers', [\App\Http\Controllers\Api\CustomersController::class, 'index']);
Route::post('customers', [\App\Http\Controllers\Api\CustomersController::class, 'store']);
Route::get('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'show']);
Route::put('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'update']);
Route::delete('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'destroy']);
Route::get('customers/{id}/hosting-subscriptions', [\App\Http\Controllers\Api\CustomersController::class, 'getHostingSubscriptionsByCustomerId']);
Route::get('hosting-subscriptions', [\App\Http\Controllers\Api\HostingSubscriptionsController::class, 'index']);
Route::post('hosting-subscriptions', [\App\Http\Controllers\Api\HostingSubscriptionsController::class, 'store']);
Route::delete('hosting-subscriptions/{id}', [\App\Http\Controllers\Api\HostingSubscriptionsController::class, 'destroy']);
Route::get('hosting-plans', [\App\Http\Controllers\Api\HostingPlansController::class, 'index']);
Route::middleware('auth:sanctum')->group(function () {
Route::get('/user', function (Request $request) {
return $request->user();
});
Route::get('customers', [\App\Http\Controllers\Api\CustomersController::class, 'index']);
Route::post('customers', [\App\Http\Controllers\Api\CustomersController::class, 'store']);
Route::get('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'show']);
Route::put('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'update']);
Route::delete('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'destroy']);
});