TaskCommentService.php
1 <?php 2 3 namespace App\Http\Services; 4 5 use App\Models\TaskComment; 6 use Exception; 7 use Illuminate\Support\Facades\DB; 8 9 class TaskCommentService 10 { 11 /** 12 * Display a listing of the resource. 13 * 14 * @return \Illuminate\Http\Response 15 */ 16 public static function find(int $taskCommentId) 17 { 18 return TaskComment::find($taskCommentId); 19 } 20 21 /** 22 * Create a resource 23 * 24 * @return \Illuminate\Http\Response 25 */ 26 public static function create(string $comment, array $relation): bool|Exception 27 { 28 try { 29 DB::beginTransaction(); 30 31 TaskComment::create([ 32 'comment' => $comment, 33 'task_id' => $relation['task_id'], 34 'user_id' => $relation['user_id'], 35 ]); 36 37 DB::commit(); 38 39 return true; 40 } catch (Exception $e) { 41 DB::rollBack(); 42 throw new Exception($e->getMessage()); 43 } 44 } 45 46 /** 47 * Update a resource 48 * 49 * @return \Illuminate\Http\Response 50 */ 51 public static function update(string $comment, int $taskCommentId) 52 { 53 try { 54 DB::beginTransaction(); 55 56 $taskComment = TaskComment::findOrFail($taskCommentId); 57 $taskComment->comment = $comment; 58 $taskComment->save(); 59 60 DB::commit(); 61 62 return true; 63 } catch (Exception $e) { 64 DB::rollBack(); 65 //throw new Exception($e->getMessage()); 66 throw new Exception(__('tasks.notifications.update.error.message')); 67 } 68 } 69 70 /** 71 * Destroy a resource 72 * 73 * @return \Illuminate\Http\Response 74 */ 75 public static function remove(int $taskCommentId) 76 { 77 try { 78 DB::beginTransaction(); 79 TaskComment::destroy($taskCommentId); 80 DB::commit(); 81 82 return true; 83 } catch (Exception $e) { 84 DB::rollBack(); 85 throw new Exception(__('tasks.notifications.remove.error.message')); 86 } 87 } 88 }