Skip to content

Instantly share code, notes, and snippets.

@Casperhr
Created December 3, 2015 21:11
Show Gist options
  • Select an option

  • Save Casperhr/969689fc65a86a52cf39 to your computer and use it in GitHub Desktop.

Select an option

Save Casperhr/969689fc65a86a52cf39 to your computer and use it in GitHub Desktop.
<?php
namespace Budget\Models\Project\Item;
use Budget\Models\Project\Item\Allocation\Allocation;
use Budget\Models\Project\Project;
use Nodes\Database\Repository as NodesRepository;
use Nodes\Exception\Exception;
/**
* Class ItemRepository
*
* @author Casper Rasmussen <[email protected]>
* @package Budget\Models\Project\Item
*/
class ItemRepository extends NodesRepository
{
/**
* ItemRepository constructor.
*
* @param \Budget\Models\Project\Item\Item $item
*/
public function __construct(Item $item)
{
$this->setupRepository($item);
}
/**
* Create an Item with the inputted data from backend forms.
* Remember to validate all data before
*
* @author Casper Rasmussen <[email protected]>
* @param array $data
* @throws \Exception
* @return \Budget\Models\Project\Item\Item
*/
public function createFromRaw(array $data)
{
try {
// Begin transaction
$this->getBuilder()->getConnection()->beginTransaction();
// Create instance
$item = $this->newInstance($data);
// Save, throw exception if fails
if (!$item->save()) {
throw new Exception('Failed to save item');
}
// Delete existing relations
$item->allocations()->delete();
// Create allocations
foreach ($data['months'] as $id => $amount) {
// Skip empty
if (empty($amount)) {
continue;
}
// Save allocation
$saved = $item->allocations()->save(new Allocation([
'month_id' => $id,
'amount' => $amount
]));
// Throw exception if 1 allocation is not saved
if (!$saved) {
throw new Exception('Failed to save allocation');
}
}
// Commit
$this->getBuilder()->getConnection()->commit();
} catch (\Exception $e) {
// Roll back
$this->getBuilder()->getConnection()->rollBack();
// Throw exception again
throw $e;
}
return $item;
}
/**
* Update an Item with the inputted data from backend forms.
* Remember to validate all data before
*
* @author Casper Rasmussen <[email protected]>
* @param \Budget\Models\Project\Item\Item $item
* @param array $data
* @return \Budget\Models\Project\Item\Item
* @throws \Exception
*/
public function updateItem(Item $item, array $data)
{
try {
// Begin transaction
$this->getBuilder()->getConnection()->beginTransaction();
// Fill and save, throw exception if fails
$item->fill($data);
if (!$item->save()) {
throw new Exception('Failed to save item');
}
// Delete existing relations
$item->allocations()->delete();
// Create allocations
foreach ($data['months'] as $id => $amount) {
// Skip empty
if (empty($amount)) {
continue;
}
// Save allocation
$saved = $item->allocations()->save(new Allocation([
'month_id' => $id,
'amount' => $amount
]));
// Throw exception if 1 allocation is not saved
if (!$saved) {
throw new Exception('Failed to save allocation');
}
}
// Commit
$this->getBuilder()->getConnection()->commit();
} catch (\Exception $e) {
// Roll back
$this->getBuilder()->getConnection()->rollBack();
// Throw exception again
throw $e;
}
return $item;
}
/**
* Retrieve a list of Items belonging to project
*
* @author Casper Rasmussen <[email protected]>
* @param \Budget\Models\Project\Project $project
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function getPaginatedForProject(Project $project)
{
// Build query
$query = $this->where('project_id', $project->id)
->orderBy('created_at', 'DESC');
// Paginate
return $query->paginate(20);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment