Skip to content

Commit

Permalink
Added Promo code update and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ojsholly committed Nov 13, 2020
1 parent 4e0dca7 commit 7650b14
Show file tree
Hide file tree
Showing 8 changed files with 365 additions and 7 deletions.
199 changes: 195 additions & 4 deletions app/Http/Controllers/API/v1/PromoCode/PromoCodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace App\Http\Controllers\API\v1\PromoCode;

use DateTime;
use Carbon\Carbon;
use Polyline;
use App\Models\PromoCode;
use Illuminate\Http\Request;
use App\Http\Traits\DistanceTrait;
use App\Http\Controllers\Controller;
use App\Http\Resources\PromoCodeResource;
use App\Http\Requests\CreatePromoCodeRequest;
use App\Http\Requests\UpdatePromoCodeRequest;
use App\Http\Requests\ValidatePromoCodeRequest;
use App\Http\Resources\PromoCodeResourceCollection;

class PromoCodeController extends Controller
{
Expand All @@ -23,6 +26,50 @@ class PromoCodeController extends Controller
public function index()
{
//
return new PromoCodeResourceCollection(PromoCode::withTrashed()->get());
}

public function active()
{
return new PromoCodeResourceCollection(PromoCode::withoutExpired()->get());
}

public function expired()
{
return new PromoCodeResourceCollection(PromoCode::onlyExpired()->get());
}

public function deactivated()
{
return new PromoCodeResourceCollection(PromoCode::onlyTrashed()->get());
}

public function deactivate($id)
{
$promo_code = PromoCode::findByUuid($id);

if ($promo_code == null) {
# code...
abort(404, 'Promo Code not found');
}


if ($promo_code->delete()) {
# code...
$response = [
'status' => 'success',
'message' => 'Promo Code Successfully deactivated'
];

return response()->json($response, 200);
}

$response = [
'status' => 'error',
'message' => 'Error Deactivating Promo Code'
];

return response()->json($response, 400);
}

/**
Expand Down Expand Up @@ -80,15 +127,92 @@ public function store(CreatePromoCodeRequest $request)
return response()->json($response, 201);
}


public function verify($id, array $origin, array $destination)
{
$promo_code = PromoCode::findByUuid($id);

$venue = $promo_code->venue;

$venue_coordinates = $this->get_latitude_and_longitude($venue);

$origin_distance = $this->get_distance($venue_coordinates, $origin);

$destination_distance = $this->get_distance($venue_coordinates, $destination);

$radius = $promo_code->radius;

if ($origin_distance > $radius && $destination_distance > $radius) {
# code...

return false;
}

return true;
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
public function show(ValidatePromoCodeRequest $request, $code)
{
//
$promo_code = PromoCode::where('code', $code)->first();

if ($promo_code == null) {
# code...

abort(404, 'Promo Code not found');
}

if ($promo_code->expired()) {
# code...

$response = [
'status' => 'error',
'message' => 'Sorry. This promo code has already expired.'
];

return response()->json($response, 400);
}

$origin_coordinates = $this->get_latitude_and_longitude($request->origin);

$destination_coordinates = $this->get_latitude_and_longitude($request->destination);

if ($origin_coordinates == null || $destination_coordinates == null) {
# code...

$response = [
'status' => 'error',
'message' => 'Sorry. Locations not found.'
];

return response()->json($response, 400);
}

$valid_code = $this->verify($promo_code->uuid, $origin_coordinates, $destination_coordinates);

if ($valid_code == false) {
# code...

$response = [
'status' => 'error',
'message' => 'Current location not valid for promo code.'
];

return response()->json($response, 400);
}


$points = [$destination_coordinates, $origin_coordinates];

$promo_code->polyline = Polyline::encode($points);

return new PromoCodeResource($promo_code);
}

/**
Expand All @@ -98,9 +222,53 @@ public function show($id)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
public function update(UpdatePromoCodeRequest $request, $id)
{
//
$promo_code = PromoCode::findByUuid($id);

if ($promo_code == null) {
# code...
abort(404, 'Promo Code Not Found');
}

$venue_coordinates = $this->get_latitude_and_longitude($request->venue);

if ($venue_coordinates == null) {
# code...

$response = [
'status' => 'error',
'message' => 'Sorry. Location not found.'
];

return response()->json($response, 400);
}

$update_promo_code = PromoCode::where('uuid', $id)->update([
'code' => $request->code,
'venue' => $request->venue,
'value' => $request->value,
'radius' => $request->radius,
'expires_at' => $request->expiry_date
]);

if ($update_promo_code == null) {
# code...
$response = [
'status' => 'error',
'message' => 'Error updating promo code.'
];

return response()->json($response, 400);
}

$response = [
'status' => 'success',
'message' => 'Promo Code successfully updated.'
];

return response()->json($response, 400);
}

/**
Expand All @@ -112,5 +280,28 @@ public function update(Request $request, $id)
public function destroy($id)
{
//
$promo_code = PromoCode::findByUuid($id);

if ($promo_code == null) {
# code...
abort(404, 'Promo Code not found');
}

if ($promo_code->forceDelete()) {
# code...
$response = [
'status' => 'success',
'message' => 'Promo Code Successfully deleted'
];

return response()->json($response, 200);
}

$response = [
'status' => 'error',
'message' => 'Error Deleting Promo Code'
];

return response()->json($response, 400);
}
}
40 changes: 40 additions & 0 deletions app/Http/Requests/UpdatePromoCodeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class UpdatePromoCodeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'id' => 'required|uuid',
'code' => [
'required',
'string',
Rule::unique('promo_codes')->ignore($this->code, 'code'),
],
'value' => 'required|numeric|gt:0',
'venue' => 'required|string',
'radius' => 'required|numeric|gt:0',
'expiry_date' => 'required|date|after_or_equal:' . date('Y-m-d')
];
}
}
33 changes: 33 additions & 0 deletions app/Http/Requests/ValidatePromoCodeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ValidatePromoCodeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
'code' => 'required|string|exists:promo_codes,code',
'origin' => 'required|string',
'destination' => 'required|string'
];
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/PromoCodeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class PromoCodeResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
22 changes: 22 additions & 0 deletions app/Http/Resources/PromoCodeResourceCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class PromoCodeResourceCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'count' => $this->count(),
'data' => $this->collection
];
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"require": {
"php": "^7.3|^8.0",
"binarycabin/laravel-uuid": "^1.0",
"emcconville/google-map-polyline-encoding-tool": "^1.3",
"fideloper/proxy": "^4.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
Expand Down
Loading

0 comments on commit 7650b14

Please sign in to comment.