-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add life events feature (#1765)
- Loading branch information
Showing
116 changed files
with
4,212 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
app/Http/Controllers/Api/Contact/ApiLifeEventController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\Contact; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Models\Contact\LifeEvent; | ||
use App\Http\Controllers\Api\ApiController; | ||
use App\Exceptions\MissingParameterException; | ||
use App\Services\Contact\LifeEvent\CreateLifeEvent; | ||
use App\Services\Contact\LifeEvent\UpdateLifeEvent; | ||
use App\Services\Contact\LifeEvent\DestroyLifeEvent; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use App\Http\Resources\LifeEvent\LifeEvent as LifeEventResource; | ||
|
||
class ApiLifeEventController extends ApiController | ||
{ | ||
/** | ||
* Get the list of life events. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index(Request $request) | ||
{ | ||
$lifeEvents = auth()->user()->account->lifeEvents() | ||
->orderBy($this->sort, $this->sortDirection) | ||
->paginate($this->getLimitPerPage()); | ||
|
||
return LifeEventResource::collection($lifeEvents); | ||
} | ||
|
||
/** | ||
* Get the detail of a given life event. | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function show(Request $request, int $lifeEventId) | ||
{ | ||
try { | ||
$lifeEvent = LifeEvent::where('account_id', auth()->user()->account_id) | ||
->findOrFail($lifeEventId); | ||
} catch (ModelNotFoundException $e) { | ||
return $this->respondNotFound(); | ||
} | ||
|
||
return new LifeEventResource($lifeEvent); | ||
} | ||
|
||
/** | ||
* Store the life event. | ||
* | ||
* @param Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
try { | ||
$lifeEvent = (new CreateLifeEvent)->execute( | ||
$request->all() | ||
+ | ||
[ | ||
'account_id' => auth()->user()->account->id, | ||
] | ||
); | ||
} catch (ModelNotFoundException $e) { | ||
return $this->respondNotFound(); | ||
} catch (MissingParameterException $e) { | ||
return $this->setHTTPStatusCode(500) | ||
->setErrorCode(41) | ||
->respondWithError(config('api.error_codes.41')); | ||
} | ||
|
||
return new LifeEventResource($lifeEvent); | ||
} | ||
|
||
/** | ||
* Update the life event. | ||
* | ||
* @param Request $request | ||
* @param int $lifeEventId | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function update(Request $request, $lifeEventId) | ||
{ | ||
try { | ||
$lifeEvent = (new UpdateLifeEvent)->execute( | ||
$request->all() | ||
+ | ||
[ | ||
'account_id' => auth()->user()->account->id, | ||
'life_event_id' => $lifeEventId, | ||
] | ||
); | ||
} catch (ModelNotFoundException $e) { | ||
return $this->respondNotFound(); | ||
} catch (MissingParameterException $e) { | ||
return $this->setHTTPStatusCode(500) | ||
->setErrorCode(41) | ||
->respondWithError(config('api.error_codes.41')); | ||
} | ||
|
||
return new LifeEventResource($lifeEvent); | ||
} | ||
|
||
/** | ||
* Destroy the life event. | ||
* | ||
* @param Request $request | ||
* @param int $lifeEventId | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function destroy(Request $request, $lifeEventId) | ||
{ | ||
try { | ||
(new DestroyLifeEvent)->execute([ | ||
'account_id' => auth()->user()->account->id, | ||
'life_event_id' => $lifeEventId, | ||
]); | ||
} catch (ModelNotFoundException $e) { | ||
return $this->respondNotFound(); | ||
} | ||
|
||
return $this->respondObjectDeleted((int) $lifeEventId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Contacts; | ||
|
||
use App\Helpers\DateHelper; | ||
use Illuminate\Http\Request; | ||
use App\Models\Contact\Contact; | ||
use Illuminate\Support\Collection; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Contact\LifeEventCategory; | ||
use App\Services\Contact\LifeEvent\CreateLifeEvent; | ||
use App\Services\Contact\LifeEvent\DestroyLifeEvent; | ||
use App\Http\Resources\LifeEvent\LifeEventType as LifeEventTypeResource; | ||
use App\Http\Resources\LifeEvent\LifeEventCategory as LifeEventCategoryResource; | ||
|
||
class LifeEventsController extends Controller | ||
{ | ||
/** | ||
* Get the list of life event categories. | ||
* | ||
* @param Request $request | ||
* @return Collection | ||
*/ | ||
public function categories(Request $request) | ||
{ | ||
$lifeEventCategories = auth()->user()->account->lifeEventCategories; | ||
|
||
return LifeEventCategoryResource::collection($lifeEventCategories); | ||
} | ||
|
||
/** | ||
* Get the list of life event types for a given life event category. | ||
* @param Request $request | ||
* @param int $LifeEventCategoryId | ||
* @return Collection | ||
*/ | ||
public function types(Request $request, int $lifeEventCategoryId) | ||
{ | ||
$lifeEventCategory = LifeEventCategory::findOrFail($lifeEventCategoryId); | ||
$lifeEventTypes = $lifeEventCategory->lifeEventTypes; | ||
|
||
return LifeEventTypeResource::collection($lifeEventTypes); | ||
} | ||
|
||
/** | ||
* Display the list of life events. | ||
* | ||
* @param Contact $contact | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index(Request $request, Contact $contact) | ||
{ | ||
$lifeEventsCollection = collect([]); | ||
$lifeEvents = $contact->lifeEvents()->get(); | ||
|
||
foreach ($lifeEvents as $lifeEvent) { | ||
$data = [ | ||
'id' => $lifeEvent->id, | ||
'life_event_type' => $lifeEvent->lifeEventType->name, | ||
'default_life_event_type_key' => $lifeEvent->lifeEventType->default_life_event_type_key, | ||
'name' => $lifeEvent->name, | ||
'note' => $lifeEvent->note, | ||
'happened_at' => DateHelper::getShortDate($lifeEvent->happened_at), | ||
]; | ||
$lifeEventsCollection->push($data); | ||
} | ||
|
||
return $lifeEventsCollection; | ||
} | ||
|
||
/** | ||
* Store the life event. | ||
* | ||
* @param Request $request | ||
* @param Contact $contact | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function store(Request $request, Contact $contact) | ||
{ | ||
$data = [ | ||
'account_id' => auth()->user()->account->id, | ||
'contact_id' => $contact->id, | ||
'life_event_type_id' => $request->get('life_event_type_id'), | ||
'happened_at' => $request->get('happened_at'), | ||
'name' => $request->get('name'), | ||
'note' => $request->get('note'), | ||
]; | ||
|
||
// create the conversation | ||
try { | ||
$lifeEvent = (new CreateLifeEvent)->execute($data); | ||
} catch (\Exception $e) { | ||
return back() | ||
->withInput() | ||
->withErrors(trans('app.error_save')); | ||
} | ||
|
||
return $lifeEvent; | ||
} | ||
|
||
/** | ||
* Destroy the life event. | ||
* @param Request $request | ||
* @param Contact $contat | ||
* @param LifeEvent $lifeEvent | ||
* @return bool | ||
*/ | ||
public function destroy(Request $request, Contact $contat, $lifeEventId) | ||
{ | ||
$data = [ | ||
'account_id' => auth()->user()->account->id, | ||
'life_event_id' => $lifeEventId, | ||
]; | ||
|
||
try { | ||
(new DestroyLifeEvent)->execute($data); | ||
} catch (\Exception $e) { | ||
return back() | ||
->withInput() | ||
->withErrors(trans('app.error_save')); | ||
} | ||
|
||
return 'done'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.