Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
89bfd2b
composer update
djaiss Mar 4, 2018
2692ec5
wip
djaiss Mar 4, 2018
5aa5143
wip
djaiss Mar 4, 2018
c0a3215
wip
djaiss Mar 5, 2018
f2d6ac8
wip
djaiss Mar 14, 2018
e28d17b
wip
djaiss Mar 15, 2018
1d05bb2
wip
djaiss Mar 16, 2018
ea24eff
wip
djaiss Mar 16, 2018
8feb259
merge master
djaiss Mar 16, 2018
63b2c78
wip
djaiss Mar 17, 2018
a616fea
wip
djaiss Mar 18, 2018
e9658ef
wip
djaiss Mar 18, 2018
4a772a9
wip
djaiss Mar 19, 2018
3480bb3
wip
djaiss Mar 20, 2018
3cb1e73
wip
djaiss Mar 20, 2018
cc9b3fa
wip
djaiss Mar 21, 2018
a167d84
Merge branch 'replace-relationships' of github.com:monicahq/monica in…
djaiss Mar 22, 2018
e6d6a85
wip
djaiss Mar 22, 2018
1f03697
far from being over... depressing
djaiss Mar 22, 2018
7fb1f20
fix stupid mistake I made while freaking tired
djaiss Mar 22, 2018
4ade29d
wip
djaiss Mar 23, 2018
dd7622b
wip
djaiss Mar 23, 2018
99f8f3a
Add better design for displaying a relationship
djaiss Mar 24, 2018
4cf1c10
wip
djaiss Mar 25, 2018
15bc1dd
tests
djaiss Mar 25, 2018
91a6501
final test
djaiss Mar 25, 2018
941f5ee
api
djaiss Mar 26, 2018
1671a86
wip
djaiss Mar 26, 2018
f9d4595
merge master
djaiss Mar 26, 2018
55aae4b
styleci
djaiss Mar 26, 2018
a2edbeb
styleci
djaiss Mar 26, 2018
0b128cc
bump files
djaiss Mar 26, 2018
beb7ecc
tests
djaiss Mar 27, 2018
f855158
merge master
djaiss Mar 27, 2018
a405ea3
fix styleci
djaiss Mar 27, 2018
7f5b09b
wip api
djaiss Mar 28, 2018
eae3fb1
wip api
djaiss Mar 28, 2018
82da42a
wip
djaiss Mar 30, 2018
c558111
wip
djaiss Mar 30, 2018
5f8a665
wip
djaiss Mar 30, 2018
f5e899a
wip
djaiss Mar 30, 2018
2727011
wip
djaiss Mar 30, 2018
bf0dd02
fix tests
djaiss Mar 30, 2018
91ec824
styleci
djaiss Mar 30, 2018
1c63734
merge master
djaiss Mar 31, 2018
1ad5764
fix bug
djaiss Mar 31, 2018
d7ffd47
fix test
djaiss Mar 31, 2018
b9ee161
remove useless test
djaiss Apr 1, 2018
df2e51a
Merge branch 'master' into replace-relationships
djaiss Apr 1, 2018
d0d5441
optimization
djaiss Apr 1, 2018
66b1965
Merge branch 'master' into replace-relationships
djaiss Apr 1, 2018
e7a7087
fix stuff
djaiss Apr 1, 2018
35020f1
Merge branch 'replace-relationships' of github.com:monicahq/monica in…
djaiss Apr 1, 2018
afaae90
fix dashboard
djaiss Apr 2, 2018
4e49981
merge mastr
djaiss Apr 2, 2018
916107c
styleci
djaiss Apr 2, 2018
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
Next Next commit
api
  • Loading branch information
djaiss committed Mar 26, 2018
commit 941f5ee98f70d8d075cfbf12d1ce7cf1a25e6633
83 changes: 0 additions & 83 deletions app/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1001,89 +1001,6 @@ public function updateLastCalledInfo(Call $call)
$this->save();
}

/**
* Get the list of all potential contacts to add as either a significant
* other or a kid.
*
* @return Collection
*/
public function getPotentialContacts()
{
$partners = self::where('account_id', $this->account_id)
->where('is_partial', 0)
->where('id', '!=', $this->id)
->orderBy('first_name', 'asc')
->orderBy('last_name', 'asc')
->get();

// Filter out the contacts who already partner with the given contact
$counter = 0;
foreach ($partners as $partner) {
$relationship = Relationship::where('contact_id', $this->id)
->where('with_contact_id', $partner->id)
->count();

$offspring = Offspring::where('contact_id', $partner->id)
->where('is_the_child_of', $this->id)
->count();

$progenitor = Progenitor::where('contact_id', $partner->id)
->where('is_the_parent_of', $this->id)
->count();

if ($relationship != 0 || $offspring != 0 || $progenitor != 0) {
$partners->forget($counter);
}
$counter++;
}

return $partners;
}

/**
* Get the list of partners who are not "real" contacts.
*
* @return Collection
*/
public function getPartialPartners()
{
$relationships = Relationship::where('contact_id', $this->id)
->get();

$partners = collect();
foreach ($relationships as $relationship) {
$partner = self::findOrFail($relationship->with_contact_id);

if ($partner->is_partial) {
$partners->push($partner);
}
}

return $partners;
}

/**
* Get the list of kids who are not "real" contacts.
*
* @return Collection
*/
public function getPartialOffsprings()
{
$offsprings = Offspring::where('is_the_child_of', $this->id)
->get();

$kids = collect();
foreach ($offsprings as $offspring) {
$kid = self::findOrFail($offspring->contact_id);

if ($kid->is_partial) {
$kids->push($kid);
}
}

return $kids;
}

/**
* Set a relationship between two contacts.
*
Expand Down
197 changes: 197 additions & 0 deletions app/Http/Controllers/Api/ApiRelationshipTypeGroupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php

namespace App\Http\Controllers\Api;

use Validator;
use App\Contact;
use Illuminate\Http\Request;
use App\RelationshipTypeGroup;
use Illuminate\Database\QueryException;
use App\Http\Resources\Pet\Pet as PetResource;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class ApiRelationshipTypeGroupController extends ApiController
{
/**
* Account ID column name.
*/
const ACCOUNT_ID = 'account_id';

/**
* Contact ID column name.
*/
const CONTACT_ID = 'contact_id';

/**
* Get all relationship type groups in an instance.
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function index(Request $request, $id)
{
try {
$relationshipTypeGroups = RelationshipTypeGroup::where(static::ACCOUNT_ID, auth()->user()->account_id)
->get();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

return new RelationshipTypeGroupResource::collection($relationshipTypeGroups);
}

/**
* Get the detail of a given relationship type group.
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function show(Request $request, $id)
{
try {
$relationshipTypeGroup = RelationshipTypeGroup::where(static::ACCOUNT_ID, auth()->user()->account_id)
->where('id', $id)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

return new PetResource($pet);
}

/**
* Store the pet.
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'pet_category_id' => 'integer|required|exists:pet_categories,id',
static::CONTACT_ID => 'required|integer|exists:contacts,id',
]);

if ($validator->fails()) {
return $this->setErrorCode(32)
->respondWithError($validator->errors()->all());
}

try {
$pet = Pet::create(
$request->all()
+ [
static::ACCOUNT_ID => auth()->user()->account->id,
]
);
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
}

return new PetResource($pet);
}

/**
* Update the pet.
* @param Request $request
* @param int $petId
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $petId)
{
try {
$pet = Pet::where(static::ACCOUNT_ID, auth()->user()->account_id)
->where('id', $petId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'pet_category_id' => 'sometimes|integer|required|exists:pet_categories,id',
static::CONTACT_ID => 'sometimes|required|integer|exists:contacts,id',
]);

if ($validator->fails()) {
return $this->setErrorCode(32)
->respondWithError($validator->errors()->all());
}

try {
$pet->update($request->all());
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
}

return new PetResource($pet);
}

/**
* Delete a pet.
* @param Request $request
* @param int $petId
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $petId)
{
try {
$pet = Pet::where(static::ACCOUNT_ID, auth()->user()->account_id)
->where('id', $petId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

$pet->delete();

return $this->respondObjectDeleted($pet->id);
}

/**
* Get the list of pets for the given contact.
* @param Request $request
* @param int $contactId
* @return \Illuminate\Http\Response
*/
public function listContactPets(Request $request, $contactId)
{
try {
$contact = Contact::where(static::ACCOUNT_ID, auth()->user()->account_id)
->where('id', $contactId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}

$pets = $contact->pets()
->paginate($this->getLimitPerPage());

return PetResource::collection($pets);
}

/**
* Store the pet, associated to a specific contact.
* @param Request $request
* @param int $contactId
* @return \Illuminate\Http\Response
*/
public function storeContactPet(Request $request, $contactId)
{
$request->request->add([static::CONTACT_ID => $contactId]);

return $this->store($request);
}

/**
* Update the pet, associated to a specific contact.
* @param Request $request
* @param int $contactId
* @param int $petId
* @return \Illuminate\Http\Response
*/
public function moveContactPet(Request $request, $contactId, $petId)
{
$request->request->add([static::CONTACT_ID => $contactId]);

return $this->update($request, $petId);
}
}
32 changes: 32 additions & 0 deletions app/Http/Resources/RelationshipType/RelationshipType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Resources\RelationshipType;

use Illuminate\Http\Resources\Json\Resource;
use App\Http\Resources\Contact\ContactShort as ContactShortResource;
use App\Http\Resources\Settings\ContactFieldType\ContactFieldType as ContactFieldTypeResource;

class RelationshipType extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'object' => 'relationshiptype',
'data' => $this->data,
'contact_field_type' => new ContactFieldTypeResource($this->contactFieldType),
'account' => [
'id' => $this->account->id,
],
'contact' => new ContactShortResource($this->contact),
'created_at' => $this->created_at->format(config('api.timestamp_format')),
'updated_at' => (is_null($this->updated_at) ? null : $this->updated_at->format(config('api.timestamp_format'))),
];
}
}
31 changes: 31 additions & 0 deletions app/Http/Resources/RelationshipTypeGroup/RelationshipTypeGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Resources\RelationshipTypeGroup;

use Illuminate\Http\Resources\Json\Resource;
use App\Http\Resources\Contact\ContactShort as ContactShortResource;
use App\Http\Resources\Settings\ContactFieldType\ContactFieldType as ContactFieldTypeResource;

class RelationshipTypeGroup extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'object' => 'relationshiptypegroup',
'name' => $this->name,
'delible' => (bool) $this->delible,
'account' => [
'id' => $this->account->id,
],
'created_at' => $this->created_at->format(config('api.timestamp_format')),
'updated_at' => (is_null($this->updated_at) ? null : $this->updated_at->format(config('api.timestamp_format'))),
];
}
}
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
'create', 'edit', 'patch',
]]);

// Relationship Type Groups
Route::resource('relationshiptypegroups', 'Api\\ApiRelationshipTypeGroupController', ['except' => [
'create', 'edit', 'patch',
]]);

/*
* SETTINGS
*/
Expand Down