Skip to content

Commit

Permalink
Merge pull request #146 from nazmulpcc/master
Browse files Browse the repository at this point in the history
Remove extra query to determine if record exists
  • Loading branch information
nahid authored Mar 25, 2021
2 parents ef1f98b + dd0ed08 commit b5e20aa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
}
],
"require": {
"php": "^5.6 || ^7.0 || ^7.1 || ^7.2",
"php": ">=5.6",
"illuminate/contracts": ">=5.1.0",
"illuminate/support": ">=5.1.0",
"sebastian-berc/repositories": "^1.0",
"nesbot/carbon": "^2.22",
"pusher/pusher-php-server": "^3.0",
"pusher/pusher-php-server": "^5.0",
"predis/predis": "~1.1.1",
"mpratt/embera": "^1.9"
},
Expand Down
58 changes: 58 additions & 0 deletions src/BaseRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php


namespace Nahid\Talk;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

abstract class BaseRepository
{
/**
* @var Model
*/
protected $model = null;

public function __construct()
{
$this->model = $this->makeModel();
}

public function __call($method, $arguments)
{
return call_user_func_array([$this->model, $method], $arguments);
}

protected function makeModel()
{
$model = $this->takeModel();
if (is_null($this->model)) {
$this->model = new $model();
}

return $this->model;
}

abstract public function takeModel();

public function update($id, $data)
{
$model = $this->model->find($id);

return $model->update($data);
}


public function create($data)
{
return $this->model->create($data);
}

public function delete($id)
{
$model = $this->model->find($id);

return $model->delete();
}
}
9 changes: 5 additions & 4 deletions src/Conversations/ConversationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Nahid\Talk\Conversations;

use Nahid\Talk\BaseRepository;
use SebastianBerc\Repositories\Repository;

class ConversationRepository extends Repository
class ConversationRepository extends BaseRepository
{
/*
* this method is default method for repository package
Expand Down Expand Up @@ -56,10 +57,10 @@ function ($q) use ($user1, $user2) {
}
);
}
);
)->first();

if ($conversation->exists()) {
return $conversation->first()->id;
if ($conversation) {
return $conversation->id;
}

return false;
Expand Down
5 changes: 3 additions & 2 deletions src/Messages/MessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Nahid\Talk\Messages;

use Nahid\Talk\BaseRepository;
use SebastianBerc\Repositories\Repository;

class MessageRepository extends Repository
class MessageRepository extends BaseRepository
{
public function takeModel()
{
Expand Down Expand Up @@ -34,6 +35,6 @@ public function softDeleteMessage($messageId, $authUserId)
}

return (boolean) $this->update($message);

}
}

0 comments on commit b5e20aa

Please sign in to comment.