Skip to content
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Add Pets management (monicahq#722)
  • Loading branch information
djaiss authored Dec 27, 2017
commit 6d8dcefe77699b4039dbaabd0858fe12bd4ce29e
4 changes: 1 addition & 3 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
UNRELEASED CHANGES:

<<<<<<< HEAD
* Add pets management
* Activities made with contact now appears in the Journal
* Add ability to rate how a day went in the Journal
=======
* Add validation when changing email address
>>>>>>> master
* Add ability to change account's password in the settings
* Show a user's avatar when searching

Expand Down
10 changes: 10 additions & 0 deletions app/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ public function addresses()
return $this->hasMany('App\Address');
}

/**
* Get the Pets records associated with the contact.
*
* @return HasMany
*/
public function pets()
{
return $this->hasMany('App\Pet');
}

/**
* Get the contact records associated with the account.
*
Expand Down
109 changes: 109 additions & 0 deletions app/Http/Controllers/Contacts/PetsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace App\Http\Controllers\Contacts;

use App\Pet;
use App\Contact;
use App\PetCategory;
use App\Http\Controllers\Controller;
use App\Http\Requests\People\PetsRequest;

class PetsController extends Controller
{
/**
* Get all the pet categories.
*/
public function getPetCategories()
{
$petCategoriesData = collect([]);

$petCategories = PetCategory::all();

foreach ($petCategories as $petCategory) {
$data = [
'id' => $petCategory->id,
'name' => $petCategory->name,
'edit' => false,
];
$petCategoriesData->push($data);
}

return $petCategoriesData;
}

/**
* Get all the pets for this contact.
* @param Contact $contact
*/
public function get(Contact $contact)
{
$petsCollection = collect([]);
$pets = $contact->pets;

foreach ($pets as $pet) {
$data = [
'id' => $pet->id,
'name' => $pet->name,
'pet_category_id' => $pet->pet_category_id,
'category_name' => $pet->petCategory->name,
'edit' => false,
];
$petsCollection->push($data);
}

return $petsCollection;
}

/**
* Store the pet.
*/
public function store(PetsRequest $request, Contact $contact)
{
$pet = $contact->pets()->create(
$request->only([
'pet_category_id',
'name',
])
+ [
'account_id' => auth()->user()->account->id,
]
);

return $data = [
'id' => $pet->id,
'name' => $pet->name,
'pet_category_id' => $pet->pet_category_id,
'category_name' => $pet->petCategory->name,
'edit' => false,
];
}

/**
* Update the pet.
*/
public function update(PetsRequest $request, Contact $contact, Pet $pet)
{
$pet->update(
$request->only([
'pet_category_id',
'name',
])
+ [
'account_id' => auth()->user()->account->id,
]
);

return $data = [
'id' => $pet->id,
'name' => $pet->name,
'pet_category_id' => $pet->pet_category_id,
'category_name' => $pet->petCategory->name,
'edit' => false,
];
}

public function trash(Contact $contact, Pet $pet)
{
$pet->delete();
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/People/PetsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests\People;

use Illuminate\Foundation\Http\FormRequest;

class PetsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'max:255|nullable',
'pet_category_id' => 'integer',
];
}
}
1 change: 1 addition & 0 deletions app/Jobs/ExportAccountAsSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ExportAccountAsSQL
'oauth_personal_access_clients',
'oauth_refresh_tokens',
'password_resets',
'pet_categories',
'sessions',
'statistics',
'subscriptions',
Expand Down
57 changes: 57 additions & 0 deletions app/Pet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App;

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

class Pet extends Model
{
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = ['id'];

/**
* Get the account record associated with the gift.
*
* @return BelongsTo
*/
public function account()
{
return $this->belongsTo(Account::class);
}

/**
* Get the contact record associated with the gift.
*
* @return BelongsTo
*/
public function contact()
{
return $this->belongsTo(Contact::class);
}

/**
* Get the contact record associated with the gift.
*
* @return BelongsTo
*/
public function petCategory()
{
return $this->belongsTo(PetCategory::class);
}

/**
* Set the name to null if it's an empty string.
*
* @param string $value
* @return void
*/
public function setNameAttribute($value)
{
$this->attributes['name'] = $value ?: null;
}
}
39 changes: 39 additions & 0 deletions app/PetCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PetCategory extends Model
{
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = ['id'];

protected $table = 'pet_categories';

/**
* Scope a query to only include pet categories that are considered `common`.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCommon($query)
{
return $query->where('is_common', 1);
}

/**
* Get the name of the pet's category.
*
* @param string $value
* @return string
*/
public function getNameAttribute($value)
{
return $value;
}
}
7 changes: 7 additions & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Route;
use App\Day;
use App\Pet;
use App\Debt;
use App\Gift;
use App\Note;
Expand Down Expand Up @@ -124,6 +125,12 @@ public function boot()
->where('id', $value)
->firstOrFail();
});

Route::bind('pet', function ($value, $route) {
return Pet::where('account_id', auth()->user()->account_id)
->where('id', $value)
->firstOrFail();
});
}

/**
Expand Down
44 changes: 44 additions & 0 deletions database/migrations/2017_12_24_115641_create_pets_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

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

class CreatePetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('pets', function (Blueprint $table) {
$table->increments('id');
$table->integer('account_id');
$table->integer('contact_id');
$table->integer('pet_category_id');
$table->string('name')->nullable();
$table->timestamps();
});

Schema::create('pet_categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->boolean('is_common');
$table->timestamps();
});

DB::table('pet_categories')->insert(['name' => 'reptile', 'is_common' => false]);
DB::table('pet_categories')->insert(['name' => 'bird', 'is_common' => false]);
DB::table('pet_categories')->insert(['name' => 'cat', 'is_common' => true]);
DB::table('pet_categories')->insert(['name' => 'dog', 'is_common' => true]);
DB::table('pet_categories')->insert(['name' => 'fish', 'is_common' => true]);
DB::table('pet_categories')->insert(['name' => 'hamster', 'is_common' => false]);
DB::table('pet_categories')->insert(['name' => 'horse', 'is_common' => false]);
DB::table('pet_categories')->insert(['name' => 'rabbit', 'is_common' => false]);
DB::table('pet_categories')->insert(['name' => 'rat', 'is_common' => false]);
DB::table('pet_categories')->insert(['name' => 'small_animal', 'is_common' => false]);
DB::table('pet_categories')->insert(['name' => 'other', 'is_common' => false]);
}
}
18 changes: 18 additions & 0 deletions database/seeds/FakeContentTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function run()
$this->populateGifts();
$this->populateAddresses();
$this->populateContactFields();
$this->populatePets();
$this->changeUpdatedAt();

$progress->advance();
Expand Down Expand Up @@ -431,6 +432,23 @@ public function populateEntries()
}
}

public function populatePets()
{
if (rand(1, 3) == 1) {
for ($j = 0; $j < rand(1, 3); $j++) {
$date = $this->faker->dateTimeThisYear();

$petId = DB::table('pets')->insertGetId([
'account_id' => $this->account->id,
'contact_id' => $this->contact->id,
'pet_category_id' => rand(1, 11),
'name' => (rand(1, 3) == 1) ? $this->faker->firstName : null,
'created_at' => $date,
]);
}
}
}

public function populateDayRatings()
{
for ($j = 0; $j < rand(10, 100); $j++) {
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/app.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"/js/app.js": "/js/app.js?id=cbd6e9b0e83d9120517a",
"/js/app.js": "/js/app.js?id=add66bd4d66fd8e26f4c",
"/css/app.css": "/css/app.css?id=0a25d8cfce9c8cb33447",
"/js/app.js.map": "/js/app.js.map?id=2943336e0fe707dadc13",
"/js/app.js.map": "/js/app.js.map?id=d41602915a27ee1daf44",
"/css/app.css.map": "/css/app.css.map?id=516bf3da4d93de8e038d"
}
5 changes: 5 additions & 0 deletions resources/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Vue.component(
require('./components/people/Notes.vue')
);

Vue.component(
'pet',
require('./components/people/Pets.vue')
);

// Journal
Vue.component(
'journal-list',
Expand Down
Loading