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

Commit 10827d3

Browse files
committed
Add backend API CRUD for a new domain object - "Gigad"
Gigad (an ad for a gig) is a public posting from Organizations; that they're willing to pick up offers for gigs of a certain type. This allows companies to hire performers. Ref #21
1 parent eea0c18 commit 10827d3

29 files changed

+950
-17
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\V1;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Gigad\DeleteGigadRequest;
7+
use App\Http\Requests\Gigad\StoreGigadRequest;
8+
use App\Http\Requests\Gigad\UpdateGigadRequest;
9+
use App\Http\Resources\V1\GigadResource;
10+
use App\Orm\Gigad;
11+
use App\Orm\Organization;
12+
use Illuminate\Http\Request;
13+
use Illuminate\Http\Resources\Json\JsonResource;
14+
use Spatie\QueryBuilder\AllowedFilter;
15+
use Spatie\QueryBuilder\QueryBuilder;
16+
17+
/**
18+
* @group Gigads
19+
*/
20+
class GigadController extends Controller
21+
{
22+
23+
24+
/**
25+
* Show Gigad details
26+
*
27+
* @param Gigad $gigad
28+
* @return JsonResource
29+
*/
30+
public function show(Gigad $gigad): JsonResource
31+
{
32+
return new GigadResource($gigad);
33+
}
34+
35+
/**
36+
* List all Gigads
37+
*
38+
* @param Request $request
39+
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Resources\Json\AnonymousResourceCollection|\Illuminate\Http\Response
40+
*/
41+
public function index(Request $request)
42+
{
43+
44+
$gigads = QueryBuilder::for(Gigad::class)
45+
->allowedFilters(AllowedFilter::exact('is_public'))
46+
->orderBy('id', 'asc')
47+
->onlyMine($request->input('onlyMine', false))
48+
->paginate(15);
49+
50+
return GigadResource::collection($gigads);
51+
}
52+
53+
/**
54+
* Create a new Gigad
55+
*
56+
* @param StoreGigadRequest $request
57+
* @return GigadResource
58+
* @authenticated
59+
* @bodyParam description string Long (markdown-enabled) description of the organization
60+
* @bodyParam is_public boolean Whether or not to show this publicly
61+
* @bodyParam link string Link to "read more"
62+
* @bodyParam int gig_category_id Numeric category ID
63+
* @bodyParam string organization_uid Organization who owns this
64+
*/
65+
public function store(StoreGigadRequest $request)
66+
{
67+
68+
$gigad = new Gigad;
69+
$gigad->link = $request->input('link');
70+
$gigad->description = $request->input('description');
71+
$gigad->organization_id = Organization::where('uid', $request->input('organization_uid'))->first()->id;
72+
$gigad->gig_category_id = $request->input('gig_category_id');
73+
$gigad->setToken();
74+
$gigad->save();
75+
76+
return new GigadResource($gigad);
77+
}
78+
79+
/**
80+
* Update an ad
81+
*
82+
* @param Gigad $gigad
83+
* @param UpdateGigadRequest $request
84+
* @return GigadResource
85+
* @bodyParam description string Long (markdown-enabled) description of the organization
86+
* @bodyParam is_public boolean Whether or not to show this publicly
87+
* @bodyParam link string Link to "read more"
88+
* @bodyParam int gig_category_id Numeric category ID
89+
* @bodyParam string organization_uid Organization who owns this
90+
* @authenticated
91+
*/
92+
public function update(Gigad $gigad, UpdateGigadRequest $request)
93+
{
94+
$gigad->link = $request->input('link');
95+
$gigad->description = $request->input('description');
96+
$gigad->organization_id = Organization::where('uid', $request->input('organization_uid'))->first()->id;
97+
$gigad->gig_category_id = $request->input('gig_category_id');
98+
$gigad->save();
99+
100+
return new GigadResource($gigad);
101+
}
102+
103+
104+
/**
105+
* Delete a Gigad
106+
*
107+
* @param Gigad $gigad
108+
* @param DeleteGigadRequest $request
109+
* @throws \Exception
110+
* @authenticated
111+
*/
112+
public function destroy(Gigad $gigad, DeleteGigadRequest $request)
113+
{
114+
$gigad->delete();
115+
}
116+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Gigad;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Support\Facades\Auth;
7+
8+
class DeleteGigadRequest extends FormRequest
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*
13+
* @return bool
14+
*/
15+
public function authorize()
16+
{
17+
18+
$gigadOrganizations = $this->gigad->organization()->get()->pluck('id')->toArray();
19+
foreach (Auth::user()->organizations()->get() as $organization) {
20+
if (in_array($organization->id, $gigadOrganizations)) {
21+
return true;
22+
}
23+
}
24+
return false;
25+
}
26+
27+
/**
28+
* Get the validation rules that apply to the request.
29+
*
30+
* @return array
31+
*/
32+
public function rules()
33+
{
34+
return [
35+
//
36+
];
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Gigad;
4+
5+
class StoreGigadRequest extends UpdateGigadRequest
6+
{
7+
/**
8+
* Determine if the user is authorized to make this request.
9+
*
10+
* @return bool
11+
*/
12+
public function authorize()
13+
{
14+
return true;
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Gigad;
4+
5+
use App\Rules\Base64HeaderImage;
6+
use App\Rules\ContainsMyOrganization;
7+
8+
class UpdateGigadRequest extends DeleteGigadRequest
9+
{
10+
11+
12+
/**
13+
* Get the validation rules that apply to the request.
14+
*
15+
* @return array
16+
*/
17+
public function rules()
18+
{
19+
return [
20+
'link' => 'max:255|required|min:5|url',
21+
'description' => 'max:5000|required',
22+
'organization_uid' => ['required', 'exists:organizations,uid', new ContainsMyOrganization],
23+
'images.header.content' => ['nullable', new Base64HeaderImage],
24+
'is_public' => 'required|bool',
25+
'gig_category_id' => ['required', 'int', 'exists:gig_categories,id']
26+
];
27+
}
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Resources\V1;
4+
5+
use App\Orm\GigCategory;
6+
use App\Orm\Organization;
7+
use Illuminate\Http\Resources\Json\JsonResource;
8+
9+
/**
10+
* @property Organization $organization
11+
* @property GigCategory $category
12+
* @package App\Http\Resources\V1
13+
*/
14+
class GigadResource extends JsonResource
15+
{
16+
17+
18+
19+
/**
20+
* Transform the resource into an array.
21+
*
22+
* @param \Illuminate\Http\Request $request
23+
* @return array
24+
*/
25+
public function toArray($request)
26+
{
27+
28+
return [
29+
'uid' => $this['uid'],
30+
'category' => [
31+
'id' => $this->category->id,
32+
'name' => $this->category->name
33+
],
34+
'organization' => [
35+
'uid' => $this->organization->uid,
36+
'name' => $this->organization->name
37+
],
38+
'link'=> $this['link'],
39+
'description' => $this['description']
40+
];
41+
}
42+
43+
}

src/app/Orm/GigCategory.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Orm;
4+
5+
use App\Orm\Traits\HasCoverImage;
6+
use Dirape\Token\DirapeToken;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\SoftDeletes;
10+
use OwenIt\Auditing\Contracts\Auditable;
11+
use Spatie\MediaLibrary\HasMedia;
12+
use Spatie\MediaLibrary\InteractsWithMedia;
13+
14+
/**
15+
* @property string $name
16+
* @property int $id
17+
* @property string $description
18+
* @package App\Orm
19+
*/
20+
class GigCategory extends Model
21+
{
22+
use HasFactory;
23+
24+
use \Astrotomic\Translatable\Translatable, SoftDeletes, HasFactory;
25+
26+
public $translatedAttributes = ['name', 'description'];
27+
28+
protected $dates = [
29+
'created_at',
30+
'updated_at',
31+
'deleted_at'
32+
];
33+
34+
protected $fillable = ['description', 'name','locale'];
35+
/**
36+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
37+
*/
38+
public function gigads()
39+
{
40+
return $this->hasMany('App\Orm\Gigad', 'gig_category_id');
41+
}
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Orm;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* @package App\Orm
9+
* @property string name
10+
* @property string locale
11+
* @property string description
12+
*/
13+
class GigCategoryTranslation extends Model
14+
{
15+
public $timestamps = false;
16+
protected $fillable = ['name', 'description'];
17+
18+
/**
19+
* The attributes that should be cast to native types.
20+
*
21+
* @var array
22+
*/
23+
protected $casts = [
24+
'auto_translated' => 'boolean',
25+
];
26+
27+
public function setNameAttribute($value)
28+
{
29+
$this->attributes['name'] = trim($value);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)