Skip to content

Commit

Permalink
feat: emotion on notes (monicahq/chandler#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaiss authored Feb 5, 2022
1 parent 8896a66 commit faedfb2
Show file tree
Hide file tree
Showing 18 changed files with 329 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function store(Request $request, int $vaultId, int $contactId)
'contact_id' => $contactId,
'title' => $request->input('title'),
'body' => $request->input('body'),
'emotion_id' => $request->input('emotion'),
];

$note = (new CreateNote)->execute($data);
Expand All @@ -43,6 +44,7 @@ public function update(Request $request, int $vaultId, int $contactId, int $note
'note_id' => $noteId,
'title' => $request->input('title'),
'body' => $request->input('body'),
'emotion_id' => $request->input('emotion'),
];

$note = (new UpdateNote)->execute($data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ public static function data(Contact $contact): array
$notesCollection = $notes->map(function ($note) use ($contact) {
return self::dto($contact, $note);
});
$emotions = $contact->vault->account->emotions()->get();
$emotionsCollection = $emotions->map(function ($emotion) {
return [
'id' => $emotion->id,
'name' => $emotion->name,
'type' => $emotion->type,
];
});

return [
'notes' => $notesCollection,
'emotions' => $emotionsCollection,
'url' => [
'store' => route('contact.note.store', [
'vault' => $contact->vault_id,
Expand All @@ -39,6 +48,10 @@ public static function dto(Contact $contact, Note $note): array
'body_excerpt' => Str::length($note->body) >= 200 ? Str::limit($note->body, 200) : null,
'show_full_content' => false,
'title' => $note->title,
'emotion' => $note->emotion ? [
'id' => $note->emotion->id,
'name' => $note->emotion->name,
] : null,
'author' => $note->author ? $note->author->name : $note->author_name,
'written_at' => DateHelper::formatDate($note->created_at),
'url' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ public static function data(Contact $contact): array
$notesCollection = $notes->map(function ($note) use ($contact) {
return self::dto($contact, $note);
});
$emotions = $contact->account->emotions()->get();
$emotionsCollection = $emotions->map(function ($emotion) {
return [
'id' => $emotion->id,
'name' => $emotion->name,
'type' => $emotion->type,
];
});

return [
'notes' => $notesCollection,
'emotions' => $emotionsCollection,
'url' => [
'store' => route('contact.note.store', [
'vault' => $contact->vault_id,
Expand All @@ -44,6 +53,7 @@ public static function dto(Contact $contact, Note $note): array
'show_full_content' => false,
'title' => $note->title,
'author' => $note->author ? $note->author->name : $note->author_name,
'emotion' => $note->emotion ? $note->emotion->name : null,
'written_at' => DateHelper::formatDate($note->created_at),
'url' => [
'update' => route('contact.note.update', [
Expand Down
23 changes: 23 additions & 0 deletions app/Jobs/SetupAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\User;
use App\Models\Module;
use App\Models\Emotion;
use App\Models\Template;
use App\Models\Information;
use App\Models\TemplatePage;
Expand Down Expand Up @@ -198,6 +199,7 @@ private function addFirstInformation(): void
$this->addAddressTypes();
$this->addContactInformation();
$this->addPetCategories();
$this->addEmotions();
}

/**
Expand Down Expand Up @@ -519,4 +521,25 @@ private function addPetCategories(): void
]);
}
}

private function addEmotions(): void
{
DB::table('emotions')->insert([
[
'account_id' => $this->user->account_id,
'name' => trans('app.emotion_negative'),
'type' => Emotion::TYPE_NEGATIVE,
],
[
'account_id' => $this->user->account_id,
'name' => trans('app.emotion_neutral'),
'type' => Emotion::TYPE_NEUTRAL,
],
[
'account_id' => $this->user->account_id,
'name' => trans('app.emotion_positive'),
'type' => Emotion::TYPE_POSITIVE,
],
]);
}
}
10 changes: 10 additions & 0 deletions app/Models/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,14 @@ public function petCategories()
{
return $this->hasMany(PetCategory::class);
}

/**
* Get the emotions associated with the account.
*
* @return HasMany
*/
public function emotions()
{
return $this->hasMany(Emotion::class);
}
}
42 changes: 42 additions & 0 deletions app/Models/Emotion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Emotion extends Model
{
use HasFactory;

protected $table = 'emotions';

/**
* Possible category.
*/
const TYPE_POSITIVE = 'positive';
const TYPE_NEUTRAL = 'neutral';
const TYPE_NEGATIVE = 'negative';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'account_id',
'name',
'type',
];

/**
* Get the account associated with the emotion.
*
* @return BelongsTo
*/
public function account()
{
return $this->belongsTo(Account::class);
}
}
11 changes: 11 additions & 0 deletions app/Models/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Note extends Model
protected $fillable = [
'contact_id',
'author_id',
'emotion_id',
'author_name',
'title',
'body',
Expand Down Expand Up @@ -62,6 +63,16 @@ public function author()
return $this->belongsTo(User::class);
}

/**
* Get the emotion associated with the note.
*
* @return BelongsTo
*/
public function emotion()
{
return $this->belongsTo(Emotion::class);
}

/**
* Get the note's feed item.
*/
Expand Down
9 changes: 9 additions & 0 deletions app/Services/Contact/ManageNote/CreateNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use App\Models\Note;
use App\Models\Emotion;
use App\Jobs\CreateAuditLog;
use App\Services\BaseService;
use App\Jobs\CreateContactLog;
Expand All @@ -26,6 +27,7 @@ public function rules(): array
'vault_id' => 'required|integer|exists:vaults,id',
'author_id' => 'required|integer|exists:users,id',
'contact_id' => 'required|integer|exists:contacts,id',
'emotion_id' => 'nullable|integer|exists:emotions,id',
'title' => 'nullable|string|max:255',
'body' => 'required|string|max:65535',
];
Expand Down Expand Up @@ -56,12 +58,19 @@ public function execute(array $data): Note
{
$this->validateRules($data);

if ($this->valueOrNull($data, 'emotion_id')) {
Emotion::where('account_id', $data['account_id'])
->where('id', $data['emotion_id'])
->firstOrFail();
}

$this->note = Note::create([
'contact_id' => $this->contact->id,
'author_id' => $this->author->id,
'author_name' => $this->author->name,
'title' => $this->valueOrNull($data, 'title'),
'body' => $data['body'],
'emotion_id' => $this->valueOrNull($data, 'emotion_id'),
]);

$this->contact->last_updated_at = Carbon::now();
Expand Down
9 changes: 9 additions & 0 deletions app/Services/Contact/ManageNote/UpdateNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use App\Models\Note;
use App\Models\Emotion;
use App\Jobs\CreateAuditLog;
use App\Services\BaseService;
use App\Jobs\CreateContactLog;
Expand All @@ -26,6 +27,7 @@ public function rules(): array
'author_id' => 'required|integer|exists:users,id',
'contact_id' => 'required|integer|exists:contacts,id',
'note_id' => 'required|integer|exists:notes,id',
'emotion_id' => 'nullable|integer|exists:emotions,id',
'title' => 'nullable|string|max:255',
'body' => 'required|string|max:65535',
];
Expand Down Expand Up @@ -59,8 +61,15 @@ public function execute(array $data): Note
$this->note = Note::where('contact_id', $data['contact_id'])
->findOrFail($data['note_id']);

if ($this->valueOrNull($data, 'emotion_id')) {
Emotion::where('account_id', $data['account_id'])
->where('id', $data['emotion_id'])
->firstOrFail();
}

$this->note->body = $data['body'];
$this->note->title = $this->valueOrNull($data, 'title');
$this->note->emotion_id = $this->valueOrNull($data, 'emotion_id');
$this->note->save();

$this->contact->last_updated_at = Carbon::now();
Expand Down
31 changes: 31 additions & 0 deletions database/factories/EmotionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Database\Factories;

use App\Models\Account;
use App\Models\Emotion;
use Illuminate\Database\Eloquent\Factories\Factory;

class EmotionFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Emotion::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'account_id' => Account::factory(),
'name' => $this->faker->firstName,
'type' => $this->faker->firstName,
];
}
}
35 changes: 35 additions & 0 deletions database/migrations/2021_10_20_163535_create_emotions_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

class CreateEmotionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('emotions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('account_id');
$table->string('name');
$table->string('type');
$table->timestamps();
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('emotions');
}
}
4 changes: 3 additions & 1 deletion database/migrations/2021_10_21_013005_create_notes_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ public function up()
$table->id();
$table->unsignedBigInteger('contact_id');
$table->unsignedBigInteger('author_id')->nullable();
$table->unsignedBigInteger('emotion_id')->nullable();
$table->string('author_name');
$table->string('title')->nullable();
$table->text('body');
$table->timestamps();
$table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');
$table->foreign('author_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('author_id')->references('id')->on('users')->onDelete('set null');
$table->foreign('emotion_id')->references('id')->on('emotions')->onDelete('set null');
});
}

Expand Down
Loading

0 comments on commit faedfb2

Please sign in to comment.