Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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 <cr@nodes.dk>
- * @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 <cr@nodes.dk>
- * @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 <cr@nodes.dk>
- * @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 <cr@nodes.dk>
- * @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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement