Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. <?php
  2. namespace Budget\Models\Project\Item;
  3.  
  4. use Budget\Models\Project\Item\Allocation\Allocation;
  5. use Budget\Models\Project\Project;
  6. use Nodes\Database\Repository as NodesRepository;
  7. use Nodes\Exception\Exception;
  8.  
  9. /**
  10. * Class ItemRepository
  11. *
  12. * @author Casper Rasmussen <cr@nodes.dk>
  13. * @package Budget\Models\Project\Item
  14. */
  15. class ItemRepository extends NodesRepository
  16. {
  17. /**
  18. * ItemRepository constructor.
  19. *
  20. * @param \Budget\Models\Project\Item\Item $item
  21. */
  22. public function __construct(Item $item)
  23. {
  24. $this->setupRepository($item);
  25. }
  26.  
  27. /**
  28. * Create an Item with the inputted data from backend forms.
  29. * Remember to validate all data before
  30. *
  31. * @author Casper Rasmussen <cr@nodes.dk>
  32. * @param array $data
  33. * @throws \Exception
  34. * @return \Budget\Models\Project\Item\Item
  35. */
  36. public function createFromRaw(array $data)
  37. {
  38. try {
  39. // Begin transaction
  40. $this->getBuilder()->getConnection()->beginTransaction();
  41.  
  42. // Create instance
  43. $item = $this->newInstance($data);
  44.  
  45. // Save, throw exception if fails
  46. if (!$item->save()) {
  47. throw new Exception('Failed to save item');
  48. }
  49.  
  50. // Delete existing relations
  51. $item->allocations()->delete();
  52.  
  53. // Create allocations
  54. foreach ($data['months'] as $id => $amount) {
  55. // Skip empty
  56. if (empty($amount)) {
  57. continue;
  58. }
  59.  
  60. // Save allocation
  61. $saved = $item->allocations()->save(new Allocation([
  62. 'month_id' => $id,
  63. 'amount' => $amount
  64. ]));
  65.  
  66. // Throw exception if 1 allocation is not saved
  67. if (!$saved) {
  68. throw new Exception('Failed to save allocation');
  69. }
  70. }
  71.  
  72. // Commit
  73. $this->getBuilder()->getConnection()->commit();
  74. } catch (\Exception $e) {
  75. // Roll back
  76. $this->getBuilder()->getConnection()->rollBack();
  77.  
  78. // Throw exception again
  79. throw $e;
  80. }
  81.  
  82. return $item;
  83. }
  84.  
  85. /**
  86. * Update an Item with the inputted data from backend forms.
  87. * Remember to validate all data before
  88. *
  89. * @author Casper Rasmussen <cr@nodes.dk>
  90. * @param \Budget\Models\Project\Item\Item $item
  91. * @param array $data
  92. * @return \Budget\Models\Project\Item\Item
  93. * @throws \Exception
  94. */
  95. public function updateItem(Item $item, array $data)
  96. {
  97. try {
  98. // Begin transaction
  99. $this->getBuilder()->getConnection()->beginTransaction();
  100.  
  101. // Fill and save, throw exception if fails
  102. $item->fill($data);
  103. if (!$item->save()) {
  104. throw new Exception('Failed to save item');
  105. }
  106.  
  107. // Delete existing relations
  108. $item->allocations()->delete();
  109.  
  110. // Create allocations
  111. foreach ($data['months'] as $id => $amount) {
  112. // Skip empty
  113. if (empty($amount)) {
  114. continue;
  115. }
  116.  
  117. // Save allocation
  118. $saved = $item->allocations()->save(new Allocation([
  119. 'month_id' => $id,
  120. 'amount' => $amount
  121. ]));
  122.  
  123. // Throw exception if 1 allocation is not saved
  124. if (!$saved) {
  125. throw new Exception('Failed to save allocation');
  126. }
  127. }
  128.  
  129. // Commit
  130. $this->getBuilder()->getConnection()->commit();
  131. } catch (\Exception $e) {
  132. // Roll back
  133. $this->getBuilder()->getConnection()->rollBack();
  134.  
  135. // Throw exception again
  136. throw $e;
  137. }
  138.  
  139. return $item;
  140. }
  141.  
  142. /**
  143. * Retrieve a list of Items belonging to project
  144. *
  145. * @author Casper Rasmussen <cr@nodes.dk>
  146. * @param \Budget\Models\Project\Project $project
  147. * @return \Illuminate\Pagination\LengthAwarePaginator
  148. */
  149. public function getPaginatedForProject(Project $project)
  150. {
  151. // Build query
  152. $query = $this->where('project_id', $project->id)
  153. ->orderBy('created_at', 'DESC');
  154.  
  155. // Paginate
  156. return $query->paginate(20);
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement