Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(journal): add date support to API #7280

Open
wants to merge 4 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 37 additions & 54 deletions app/Http/Controllers/Api/ApiJournalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\Journal\Entry;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Validator;
use App\Http\Resources\Journal\Entry as JournalResource;
use App\Models\Journal\Entry;
use App\Services\Journal\CreateEntry;
use App\Services\Journal\DestroyEntry;
use App\Services\Journal\UpdateEntry;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class ApiJournalController extends ApiController
{
Expand Down Expand Up @@ -49,32 +52,34 @@ public function show(Request $request, $entryId)
}

/**
* Store the call.
* Store the entry.
*
* @param Request $request
* @return JournalResource|\Illuminate\Http\JsonResponse
*/
public function store(Request $request)
{
$isvalid = $this->validateUpdate($request);
if ($isvalid !== true) {
return $isvalid;
}

try {
$entry = Entry::create(
$entry = app(CreateEntry::class)->execute(
$request->except(['account_id'])
+ ['account_id' => auth()->user()->account_id]
+
[
'account_id' => auth()->user()->account_id,
]
);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (ValidationException $e) {
return $this->respondValidatorFailed($e->validator);
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
return $this->respondInvalidQuery();
}

return new JournalResource($entry);
}

/**
* Update the note.
* Update the entry.
*
* @param Request $request
* @param int $entryId
Expand All @@ -83,48 +88,25 @@ public function store(Request $request)
public function update(Request $request, $entryId)
{
try {
$entry = Entry::where('account_id', auth()->user()->account_id)
->where('id', $entryId)
->firstOrFail();
$entry = app(UpdateEntry::class)->execute(
$request->except(['account_id', 'id'])
+
[
'account_id' => auth()->user()->account_id,
'id' => $entryId,
]
);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

$isvalid = $this->validateUpdate($request);
if ($isvalid !== true) {
return $isvalid;
}

try {
$entry->update($request->only(['title', 'post']));
} catch (ValidationException $e) {
return $this->respondValidatorFailed($e->validator);
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
return $this->respondInvalidQuery();
}

return new JournalResource($entry);
}

/**
* Validate the request for update.
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse|true
*/
private function validateUpdate(Request $request)
{
// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'title' => 'required|max:255',
'post' => 'required|max:1000000',
]);

if ($validator->fails()) {
return $this->respondValidatorFailed($validator);
}

return true;
}

/**
* Delete a journal entry.
*
Expand All @@ -134,15 +116,16 @@ private function validateUpdate(Request $request)
public function destroy(Request $request, $entryId)
{
try {
$entry = Entry::where('account_id', auth()->user()->account_id)
->where('id', $entryId)
->firstOrFail();
app(DestroyEntry::class)->execute([
'account_id' => auth()->user()->account_id,
'id' => $entryId,
]);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
} catch (ValidationException $e) {
return $this->respondValidatorFailed($e->validator);
}

$entry->delete();

return $this->respondObjectDeleted($entry->id);
return $this->respondObjectDeleted($entryId);
}
}
119 changes: 57 additions & 62 deletions app/Http/Controllers/JournalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
namespace App\Http\Controllers;

use App\Helpers\DateHelper;
use App\Helpers\JournalHelper;
use App\Http\Requests\Journal\DaysRequest;
use App\Models\Journal\Day;
use Illuminate\Http\Request;
use App\Models\Journal\Entry;
use App\Helpers\JournalHelper;
use App\Models\Journal\JournalEntry;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\Journal\DaysRequest;
use App\Services\Journal\CreateEntry;
use App\Services\Journal\DestroyEntry;
use App\Services\Journal\UpdateEntry;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Throwable;

class JournalController extends Controller
{
Expand All @@ -33,20 +37,20 @@ public function list(Request $request)

$startDate = $request->input('start_date');
$endDate = $request->input('end_date');
$sortBy = $request->input('sort_by', 'created_at');
$sortOrder = $request->input('sort_order', 'desc');
$sortBy = $request->input('sort_by', 'created_at');
$sortOrder = $request->input('sort_order', 'desc');
$perPage = $request->input('per_page', 30);

$entries = collect([]);

$journalEntriesQuery = auth()->user()->account->journalEntries();

if ($startDate && $endDate) {
$journalEntriesQuery->whereDate('date', '>=', $startDate)
->whereDate('date', '<=', $endDate);
}
$journalEntries = $journalEntriesQuery->orderBy($sortBy, $sortOrder)
->paginate($perPage);
->paginate($perPage);


// this is needed to determine if we need to display the calendar
Expand Down Expand Up @@ -90,7 +94,7 @@ public function list(Request $request)
/**
* Gets the details of a single Journal Entry.
*
* @param JournalEntry $journalEntry
* @param JournalEntry $journalEntry
* @return array
*/
public function get(JournalEntry $journalEntry)
Expand Down Expand Up @@ -160,56 +164,49 @@ public function create()
/**
* Saves the journal entry.
*
* @param Request $request
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function save(Request $request)
{
$validator = Validator::make($request->all(), [
'entry' => 'required|string',
'date' => 'required|date',
]);

if ($validator->fails()) {
try {
app(CreateEntry::class)->execute(
$request->except(['account_id'])
+
[
'account_id' => $request->user()->account_id,
'post' => $request->input('entry'),
]
);
} catch (ValidationException $e) {
return back()
->withInput()
->withErrors($validator);
}

$entry = new Entry;
$entry->account_id = $request->user()->account_id;
$entry->post = $request->input('entry');

if ($request->input('title') != '') {
$entry->title = $request->input('title');
->withErrors($e->validator);
} catch (Throwable $e) {
return back()
->withInput();
}

$entry->save();

$entry->date = $request->input('date');
// Log a journal entry
JournalEntry::add($entry);

return redirect()->route('journal.index');
}

/**
* Display the Edit journal entry screen.
*
* @param Entry $entry
* @param Entry $entry
* @return \Illuminate\View\View
*/
public function edit(Entry $entry)
{
return view('journal.edit')
->withEntry($entry);
}

/**
* Method updateDay
*
* @param Request $request
* @param Day $day
* @param Day $day
*
*/
public function updateDay(Request $request, Day $day)
Expand All @@ -226,48 +223,46 @@ public function updateDay(Request $request, Day $day)
/**
* Update a journal entry.
*
* @param Request $request
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, Entry $entry)
{
$validator = Validator::make($request->all(), [
'entry' => 'required|string',
'date' => 'required|date',
]);

if ($validator->fails()) {
try {
app(UpdateEntry::class)->execute(
$request->except(['account_id', 'id'])
+
[
'account_id' => $request->user()->account_id,
'id' => $entry->id,
'post' => $request->input('entry'),
]
);
} catch (ValidationException $e) {
return back()
->withInput()
->withErrors($validator);
}

$entry->post = $request->input('entry');

if ($request->input('title') != '') {
$entry->title = $request->input('title');
}

$entry->save();

// Update journal entry
$journalEntry = $entry->journalEntry;
if ($journalEntry) {
$entry->date = $request->input('date');
$journalEntry->edit($entry);
->withErrors($e->validator);
} catch (Throwable $e) {
return back()
->withInput();
}

return redirect()->route('journal.index');
}

/**
* Delete the reminder.
* Delete the journal entry.
*/
public function deleteEntry(Request $request, Entry $entry)
{
$entry->deleteJournalEntry();
$entry->delete();

return ['true'];
try {
app(DestroyEntry::class)->execute([
'account_id' => $request->user()->account_id,
'id' => $entry->id,
]);
return ['true'];
} catch (Throwable $e) {
return ['false'];
}
}
}
20 changes: 8 additions & 12 deletions app/Models/Journal/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,24 @@ class Entry extends Model implements IsJournalableInterface
'account_id',
'title',
'post',
'date',
];

/**
* Get the account record associated with the entry.
* The attributes that should be mutated to dates.
*
* @return BelongsTo
* @var array<string>
*/
public function account()
{
return $this->belongsTo(Account::class);
}
protected $dates = ['date'];

/**
* Get the Entry date.
* Get the account record associated with the entry.
*
* @param string $value
* @return \Carbon\Carbon
* @return BelongsTo
*/
public function getDateAttribute($value)
public function account()
{
// Default to created_at, but show journalEntry->date if the entry type is JournalEntry
return $this->journalEntry ? $this->journalEntry->date : $this->created_at;
return $this->belongsTo(Account::class);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Journal/JournalEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function add(IsJournalableInterface $resourceToLog): self
if ($resourceToLog instanceof \App\Models\Account\Activity) {
$journal->date = $resourceToLog->happened_at;
} elseif ($resourceToLog instanceof \App\Models\Journal\Entry) {
$journal->date = $resourceToLog->attributes['date'];
$journal->date = $resourceToLog->date;
}
$journal->save();
$resourceToLog->journalEntries()->save($journal);
Expand Down
Loading
Loading