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
Changes from 1 commit
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
Next Next commit
journal: add date support to entries
Refactor to use a common `CreateEntry` service across the web and api
teobaranga committed May 10, 2024
commit 6b4aa6c46574bbe34b3c835d1c4fab35da1c17d0
30 changes: 17 additions & 13 deletions app/Http/Controllers/Api/ApiJournalController.php
Original file line number Diff line number Diff line change
@@ -2,12 +2,14 @@

namespace App\Http\Controllers\Api;

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

class ApiJournalController extends ApiController
{
@@ -49,32 +51,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
64 changes: 30 additions & 34 deletions app/Http/Controllers/JournalController.php
Original file line number Diff line number Diff line change
@@ -3,13 +3,16 @@
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 App\Services\Journal\CreateEntry;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\Journal\DaysRequest;
use Illuminate\Validation\ValidationException;
use Throwable;

class JournalController extends Controller
{
@@ -33,20 +36,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
@@ -90,7 +93,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)
@@ -160,56 +163,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)
@@ -226,7 +222,7 @@ 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)
20 changes: 8 additions & 12 deletions app/Models/Journal/Entry.php
Original file line number Diff line number Diff line change
@@ -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);
}

/**
2 changes: 1 addition & 1 deletion app/Models/Journal/JournalEntry.php
Original file line number Diff line number Diff line change
@@ -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);
58 changes: 58 additions & 0 deletions app/Services/Journal/CreateEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Services\Journal;

use App\Models\Journal\Entry;
use App\Models\Journal\JournalEntry;
use App\Services\BaseService;

class CreateEntry extends BaseService
{
/**
* Get the validation rules that apply to the service.
*
* @return array
*/
public function rules(): array
{
return [
'title' => 'nullable|max:255',
'post' => 'required|max:1000000',
'date' => 'required|date|date_format:Y-m-d',
];
}

/**
* Create an entry.
*
* @param array $data
* @return Entry
*/
public function execute(array $data): Entry
{
$this->validate($data);

$entry = $this->create($data);

// Log a journal entry
JournalEntry::add($entry);

return $entry;
}

/**
* Create the entry.
*
* @param array $data
* @return Entry
*/
private function create(array $data): Entry
{
return Entry::create([
'account_id' => $data['account_id'],
'title' => $this->nullOrValue($data, 'title'),
'post' => $data['post'],
'date' => $data['date'],
]);
}
}
41 changes: 41 additions & 0 deletions database/migrations/2024_05_09_215722_add_date_to_entries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('entries', function (Blueprint $table) {
// Add the new date column
$table->date('date')->after('account_id');
});

// Copy the date from journal_entries table and populate the new column
DB::table('entries')
->join('journal_entries', function ($join) {
$join->on('entries.id', '=', 'journal_entries.journalable_id')
->where('journal_entries.journalable_type', '=', 'App\Models\Journal\Entry');
})
->update(['entries.date' => DB::raw('journal_entries.date')]);
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('entries', function (Blueprint $table) {
$table->dropColumn('date');
});
}
};