Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #57 from improv-ee/feature/order-form
Browse files Browse the repository at this point in the history
New functionality - GigAds for hiring performers
  • Loading branch information
anroots authored Oct 20, 2020
2 parents eea0c18 + c016d13 commit 01c4e3b
Show file tree
Hide file tree
Showing 43 changed files with 1,743 additions and 181 deletions.
31 changes: 31 additions & 0 deletions src/app/Http/Controllers/Api/V1/GigCategoryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Resources\V1\Gig\CategoryResource;
use App\Orm\GigCategory;
use Spatie\QueryBuilder\QueryBuilder;


/**
* @group Gigs
*
* Gig categories categorize gig ads
* Examples: Performance, workshop
*/
class GigCategoryController extends Controller
{
/**
* List all Gig categories
*/
public function index()
{

$categories = QueryBuilder::for(GigCategory::class)
->orderBy('order', 'asc')
->paginate(15);

return CategoryResource::collection($categories);
}
}
131 changes: 131 additions & 0 deletions src/app/Http/Controllers/Api/V1/GigadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Requests\Gigad\DeleteGigadRequest;
use App\Http\Requests\Gigad\StoreGigadRequest;
use App\Http\Requests\Gigad\UpdateGigadRequest;
use App\Http\Resources\V1\GigadResource;
use App\Http\Services\GigadStorageService;
use App\Orm\Gigad;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

/**
* Control Gig Ads
*
* A Gig Ad is an advertisement from an organization to accept types of Gigs.
* For example, an organization might offer companies 'shows for hire', where they perform
* at the time and place of the customers choosing
*
* @group Gigs
*/
class GigadController extends Controller
{
/**
* @var GigadStorageService
*/
private GigadStorageService $gigadStorageService;

/**
* @param GigadStorageService $gigadStorageService
*/
public function __construct(GigadStorageService $gigadStorageService)
{
$this->gigadStorageService = $gigadStorageService;
}


/**
* Show Gigad details
*
* @param Gigad $gigad
* @return JsonResource
*/
public function show(Gigad $gigad): JsonResource
{
return new GigadResource($gigad);
}

/**
* List all Gigads
*
* @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Resources\Json\AnonymousResourceCollection|\Illuminate\Http\Response
*/
public function index(Request $request)
{

$gigads = QueryBuilder::for(Gigad::class)
->allowedFilters([
AllowedFilter::exact('is_public'),
AllowedFilter::exact('gig_category_id'),
AllowedFilter::exact('organization.uid')
])
->orderBy('id', 'asc')
->onlyMine($request->input('onlyMine', false))
->paginate(15);

return GigadResource::collection($gigads);
}

/**
* Create a new Gigad
*
* @param StoreGigadRequest $request
* @return GigadResource
* @authenticated
* @bodyParam description string Long (markdown-enabled) description of the organization
* @bodyParam is_public boolean Whether or not to show this publicly
* @bodyParam link string Link to "read more"
* @bodyParam int gig_category_id Numeric category ID
* @bodyParam string organization_uid Organization who owns this
*/
public function store(StoreGigadRequest $request)
{

$gigad = new Gigad;
$gigad->setToken();

$this->gigadStorageService->update($gigad,$request);

return new GigadResource($gigad);
}

/**
* Update an ad
*
* @param Gigad $gigad
* @param UpdateGigadRequest $request
* @return GigadResource
* @bodyParam description string Long (markdown-enabled) description of the organization
* @bodyParam is_public boolean Whether or not to show this publicly
* @bodyParam link string Link to "read more"
* @bodyParam int gig_category_id Numeric category ID
* @bodyParam string organization_uid Organization who owns this
* @authenticated
*/
public function update(Gigad $gigad, UpdateGigadRequest $request)
{

$this->gigadStorageService->update($gigad,$request);
return new GigadResource($gigad);
}


/**
* Delete a Gigad
*
* @param Gigad $gigad
* @param DeleteGigadRequest $request
* @throws \Exception
* @authenticated
*/
public function destroy(Gigad $gigad, DeleteGigadRequest $request)
{
$gigad->delete();
}
}
38 changes: 38 additions & 0 deletions src/app/Http/Requests/Gigad/DeleteGigadRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Requests\Gigad;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

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

$gigadOrganizations = $this->gigad->organization()->get()->pluck('id')->toArray();
foreach (Auth::user()->organizations()->get() as $organization) {
if (in_array($organization->id, $gigadOrganizations)) {
return true;
}
}
return false;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}
16 changes: 16 additions & 0 deletions src/app/Http/Requests/Gigad/StoreGigadRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Requests\Gigad;

class StoreGigadRequest extends UpdateGigadRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}
28 changes: 28 additions & 0 deletions src/app/Http/Requests/Gigad/UpdateGigadRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Requests\Gigad;

use App\Rules\Base64HeaderImage;
use App\Rules\ContainsMyOrganization;

class UpdateGigadRequest extends DeleteGigadRequest
{


/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'link' => 'max:255|nullable|min:5|url',
'description' => 'max:500|nullable',
'organization_uid' => ['required', 'exists:organizations,uid', new ContainsMyOrganization],
'images.header.content' => ['nullable', new Base64HeaderImage],
'is_public' => 'required|bool',
'gig_category_id' => ['required', 'int', 'exists:gig_categories,id']
];
}
}
31 changes: 31 additions & 0 deletions src/app/Http/Resources/V1/Gig/CategoryResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Resources\V1\Gig;

use Illuminate\Http\Resources\Json\JsonResource;

/**
* @package App\Http\Resources\V1\Gig
*/
class CategoryResource extends JsonResource
{

/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{

return [
'id' => $this['id'],
'name' => $this['name'],
'description' => $this['description'],
'order'=> $this['order'],
'ads' => $this->gigads->count()
];
}

}
49 changes: 49 additions & 0 deletions src/app/Http/Resources/V1/GigadResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Http\Resources\V1;

use App\Http\Resources\V1\Image\HeaderImageResource;
use App\Orm\GigCategory;
use App\Orm\Organization;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @property Organization $organization
* @property GigCategory $category
* @package App\Http\Resources\V1
*/
class GigadResource extends JsonResource
{



/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{

return [
'uid' => $this['uid'],
'category' => [
'id' => $this->category->id,
'name' => $this->category->name
],
'organization' => [
'uid' => $this->organization->uid,
'name' => $this->organization->name
],
'link'=> $this['link'],
'description' => $this['description'],
'times' => [
'updated_at'=>$this['updated_at']->toIso8601String()
],
'is_public' => $this['is_public'],
'images' => new HeaderImageResource($this)
];
}

}
1 change: 1 addition & 0 deletions src/app/Http/Resources/V1/Image/HeaderImageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Cache;
use Spatie\MediaLibrary\HasMedia;

/**
* @property HasMedia $this
Expand Down
37 changes: 37 additions & 0 deletions src/app/Http/Services/GigadStorageService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Services;

use App\Http\Requests\Gigad\UpdateGigadRequest;
use App\Http\Services\Traits\SavesMediaTrait;
use App\Orm\Gigad;
use App\Orm\Organization;
use Illuminate\Support\Facades\DB;

class GigadStorageService
{
use SavesMediaTrait;

/**
* @param Gigad $gigad
* @param UpdateGigadRequest $request
* @return Gigad
*/
public function update(Gigad $gigad, UpdateGigadRequest $request): Gigad
{
$gigad->link = $request->input('link');
$gigad->description = $request->input('description');
$gigad->organization_id = Organization::where('uid', $request->input('organization_uid'))->first()->id;
$gigad->gig_category_id = $request->input('gig_category_id');
$gigad->is_public = $request->input('is_public', false);

DB::transaction(function () use ($gigad, $request) {
$gigad->save();

$this->syncMedia($request, $gigad);
});


return $gigad;
}
}
Loading

0 comments on commit 01c4e3b

Please sign in to comment.