Skip to content

Commit 7e5a5a3

Browse files
TeddyBear06asbiin
authored andcommitted
feat: add journal entry edit option (monicahq#2802)
1 parent 9a050c0 commit 7e5a5a3

File tree

25 files changed

+403
-66
lines changed

25 files changed

+403
-66
lines changed

.azure/azure.sig

0 Bytes
Binary file not shown.

.azure/unit-tests-step.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ steps:
99
TESTSUITES: "Api|Feature|Unit-Models|Unit-Services"
1010

1111
- bash: |
12+
source scripts/ci/fixsecrets.sh
1213
php artisan route:cache
1314
ulimit -S unlimited
1415
phpdbg -dmemory_limit=4G -qrr vendor/bin/phpunit -c phpunit.xml --log-junit ./results/junit/unit/results${SYSTEM_JOBPOSITIONINPHASE}.xml --coverage-clover ./results/coverage${SYSTEM_JOBPOSITIONINPHASE}.xml --testsuite $TESTSUITE

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### New features:
44

5+
* Add ability to edit a Journal entry
56
* Add vcard photo/avatar import
67
* Add ability to change the avatar of your contacts
78
* Add the ablity to set a 'me' contact (only API for now)

app/Http/Controllers/ActivitiesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function store(ActivitiesRequest $request, Contact $contact)
7979
}
8080

8181
// Log a journal entry
82-
(new JournalEntry)->add($activity);
82+
JournalEntry::add($activity);
8383

8484
return redirect()->route('people.show', $contact)
8585
->with('success', trans('people.activities_add_success'));

app/Http/Controllers/Api/ApiActivityController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function store(Request $request)
8282
}
8383

8484
// Log a journal entry
85-
(new JournalEntry)->add($activity);
85+
JournalEntry::add($activity);
8686

8787
// Now we associate the activity with each one of the attendees
8888
$attendeesID = $request->get('contacts');
@@ -109,6 +109,7 @@ public function store(Request $request)
109109
public function update(Request $request, $activityId)
110110
{
111111
try {
112+
/** @var Activity */
112113
$activity = Activity::where('account_id', auth()->user()->account_id)
113114
->findOrFail($activityId);
114115
} catch (ModelNotFoundException $e) {
@@ -136,7 +137,7 @@ public function update(Request $request, $activityId)
136137

137138
// Log a journal entry but need to delete the previous one first
138139
$activity->deleteJournalEntry();
139-
(new JournalEntry)->add($activity);
140+
JournalEntry::add($activity);
140141

141142
// Get the attendees
142143
$attendeesID = $request->get('contacts');

app/Http/Controllers/JournalController.php

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function storeDay(DaysRequest $request)
9292
]);
9393

9494
// Log a journal entry
95-
$journalEntry = (new JournalEntry)->add($day);
95+
$journalEntry = JournalEntry::add($day);
9696

9797
return [
9898
'id' => $journalEntry->id,
@@ -169,20 +169,68 @@ public function save(Request $request)
169169

170170
$entry->date = $request->input('date');
171171
// Log a journal entry
172-
(new JournalEntry)->add($entry);
172+
JournalEntry::add($entry);
173173

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

177177
/**
178-
* Delete the reminder.
178+
* Display the Edit journal entry screen.
179+
*
180+
* @param Entry $entry
181+
* @return \Illuminate\View\View
182+
*/
183+
public function edit(Entry $entry)
184+
{
185+
return view('journal.edit')
186+
->withEntry($entry);
187+
}
188+
189+
/**
190+
* Update a journal entry.
191+
*
192+
* @param Request $request
193+
* @return \Illuminate\Http\RedirectResponse
179194
*/
180-
public function deleteEntry(Request $request, $entryId)
195+
public function update(Request $request, Entry $entry)
181196
{
182-
$entry = Entry::where('account_id', $request->user()->account_id)
183-
->findOrFail($entryId);
197+
$validator = Validator::make($request->all(), [
198+
'entry' => 'required|string',
199+
'date' => 'required|date',
200+
]);
201+
202+
if ($validator->fails()) {
203+
return back()
204+
->withInput()
205+
->withErrors($validator);
206+
}
207+
208+
$entry->post = $request->input('entry');
209+
210+
if ($request->input('title') != '') {
211+
$entry->title = $request->input('title');
212+
}
184213

214+
$entry->save();
215+
216+
// Update journal entry
217+
$journalEntry = $entry->journalEntry;
218+
if ($journalEntry) {
219+
$entry->date = $request->input('date');
220+
$journalEntry->edit($entry);
221+
}
222+
223+
return redirect()->route('journal.index');
224+
}
225+
226+
/**
227+
* Delete the reminder.
228+
*/
229+
public function deleteEntry(Request $request, Entry $entry)
230+
{
185231
$entry->deleteJournalEntry();
186232
$entry->delete();
233+
234+
return ['true'];
187235
}
188236
}

app/Interfaces/IsJournalableInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,36 @@
22

33
namespace App\Interfaces;
44

5+
use Illuminate\Database\Eloquent\Relations\MorphOne;
6+
use Illuminate\Database\Eloquent\Relations\MorphMany;
7+
58
interface IsJournalableInterface
69
{
10+
/**
11+
* Get all journal entries.
12+
*
13+
* @return MorphMany
14+
*/
15+
public function journalEntries();
16+
17+
/**
18+
* Get the journal record associated.
19+
*
20+
* @return MorphOne
21+
*/
22+
public function journalEntry();
23+
24+
/**
25+
* Get all the information of the Entry for the journal.
26+
*
27+
* @return array
28+
*/
729
public function getInfoForJournalEntry();
830

31+
/**
32+
* Delete the Journal Entry associated with the given object.
33+
*
34+
* @return bool
35+
*/
936
public function deleteJournalEntry();
1037
}

app/Models/Account/Activity.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use App\Helpers\DateHelper;
77
use App\Traits\Journalable;
88
use App\Models\Contact\Contact;
9-
use App\Models\Journal\JournalEntry;
109
use App\Interfaces\IsJournalableInterface;
1110
use App\Models\ModelBindingHasher as Model;
1211
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -80,14 +79,6 @@ public function type()
8079
return $this->belongsTo(ActivityType::class, 'activity_type_id');
8180
}
8281

83-
/**
84-
* Get all of the activities journal entries.
85-
*/
86-
public function journalEntries()
87-
{
88-
return $this->morphMany(JournalEntry::class, 'journalable');
89-
}
90-
9182
/**
9283
* Return the markdown parsed body.
9384
*

app/Models/Journal/Entry.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use App\Helpers\DateHelper;
77
use App\Traits\Journalable;
88
use App\Models\Account\Account;
9-
use Illuminate\Database\Eloquent\Model;
9+
use App\Models\ModelBinding as Model;
1010
use App\Interfaces\IsJournalableInterface;
11+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1112

1213
class Entry extends Model implements IsJournalableInterface
1314
{
@@ -35,12 +36,26 @@ class Entry extends Model implements IsJournalableInterface
3536

3637
/**
3738
* Get the account record associated with the entry.
39+
*
40+
* @return BelongsTo
3841
*/
3942
public function account()
4043
{
4144
return $this->belongsTo(Account::class);
4245
}
4346

47+
/**
48+
* Get the Entry date.
49+
*
50+
* @param string $value
51+
* @return \Carbon\Carbon
52+
*/
53+
public function getDateAttribute($value)
54+
{
55+
// Default to created_at, but show journalEntry->date if the entry type is JournalEntry
56+
return $this->journalEntry ? $this->journalEntry->date : $this->created_at;
57+
}
58+
4459
/**
4560
* Get the Entry post.
4661
*
@@ -58,19 +73,17 @@ public function getPostAttribute($value)
5873
*/
5974
public function getInfoForJournalEntry()
6075
{
61-
// Default to created_at, but show journalEntry->date if the entry type is JournalEntry
62-
$entryDate = $this->journalEntry ? $this->journalEntry->date : $this->created_at;
63-
6476
return [
6577
'type' => 'entry',
6678
'id' => $this->id,
6779
'title' => $this->title,
6880
'post' => $this->post,
69-
'day' => $entryDate->day,
70-
'day_name' => mb_convert_case(DateHelper::getShortDay($entryDate), MB_CASE_TITLE, 'UTF-8'),
71-
'month' => $entryDate->month,
72-
'month_name' => mb_convert_case(DateHelper::getShortMonth($entryDate), MB_CASE_UPPER, 'UTF-8'),
73-
'year' => $entryDate->year,
81+
'day' => $this->date->day,
82+
'day_name' => mb_convert_case(DateHelper::getShortDay($this->date), MB_CASE_TITLE, 'UTF-8'),
83+
'month' => $this->date->month,
84+
'month_name' => mb_convert_case(DateHelper::getShortMonth($this->date), MB_CASE_UPPER, 'UTF-8'),
85+
'year' => $this->date->year,
86+
'date' => $this->date,
7487
];
7588
}
7689
}

app/Models/Journal/JournalEntry.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use App\Models\Contact\Entry;
66
use App\Models\Account\Account;
77
use App\Models\ModelBinding as Model;
8+
use App\Interfaces\IsJournalableInterface;
9+
use Illuminate\Database\Eloquent\Relations\MorphTo;
810
use Illuminate\Database\Eloquent\Relations\BelongsTo;
911

1012
/**
@@ -28,14 +30,16 @@ class JournalEntry extends Model
2830

2931
/**
3032
* Get all of the owning "journal-able" models.
33+
*
34+
* @return MorphTo
3135
*/
3236
public function journalable()
3337
{
3438
return $this->morphTo();
3539
}
3640

3741
/**
38-
* Get the account record associated with the invitation.
42+
* Get the account record associated with the journal entry.
3943
*
4044
* @return BelongsTo
4145
*/
@@ -46,35 +50,52 @@ public function account()
4650

4751
/**
4852
* Adds a new entry in the journal.
49-
* @param mixed $resourceToLog
53+
*
54+
* @param \App\Interfaces\IsJournalableInterface $resourceToLog
55+
* @return self
5056
*/
51-
public function add($resourceToLog)
57+
public static function add(IsJournalableInterface $resourceToLog) : self
5258
{
53-
$this->account_id = $resourceToLog->account_id;
54-
$this->date = now();
59+
$journal = new self;
60+
$journal->account_id = $resourceToLog->account_id;
61+
$journal->date = now();
5562
if ($resourceToLog instanceof \App\Models\Account\Activity) {
56-
$this->date = $resourceToLog->date_it_happened;
63+
$journal->date = $resourceToLog->date_it_happened;
64+
} elseif ($resourceToLog instanceof \App\Models\Journal\Entry) {
65+
$journal->date = $resourceToLog->attributes['date'];
5766
}
67+
$journal->save();
68+
$resourceToLog->journalEntries()->save($journal);
69+
70+
return $journal;
71+
}
72+
73+
/**
74+
* Update an entry in the journal.
75+
*
76+
* @param \App\Interfaces\IsJournalableInterface $resourceToLog
77+
* @return self
78+
*/
79+
public function edit(IsJournalableInterface $resourceToLog) : self
80+
{
5881
if ($resourceToLog instanceof \App\Models\Journal\Entry) {
59-
$this->date = $resourceToLog->date;
82+
$this->date = $resourceToLog->attributes['date'];
6083
}
61-
$this->journalable_id = $resourceToLog->id;
62-
$this->journalable_type = get_class($resourceToLog);
6384
$this->save();
6485

6586
return $this;
6687
}
6788

6889
/**
6990
* Get the information about the object represented by the Journal Entry.
91+
*
7092
* @return array
7193
*/
7294
public function getObjectData()
7395
{
74-
$type = $this->journalable_type;
75-
7696
// Instantiating the object
77-
$correspondingObject = (new $type)->findOrFail($this->journalable_id);
97+
/** @var IsJournalableInterface */
98+
$correspondingObject = $this->journalable;
7899

79100
return $correspondingObject->getInfoForJournalEntry();
80101
}

0 commit comments

Comments
 (0)