CommentLike.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\DTO\SearchData;
  5. class CommentLike extends Model
  6. {
  7. protected $table = 'tb_comment_like';
  8. protected $primaryKey = 'id';
  9. public $keyType = 'int';
  10. public $incrementing = true;
  11. public $timestamps = true;
  12. const CREATED_AT = 'created_at';
  13. const UPDATED_AT = null;
  14. protected $guarded = [];
  15. public function board()
  16. {
  17. return $this->belongsTo(Board::class);
  18. }
  19. public function post()
  20. {
  21. return $this->belongsTo(Post::class);
  22. }
  23. public function user()
  24. {
  25. return $this->belongsTo(User::class);
  26. }
  27. public function comment()
  28. {
  29. return $this->belongsTo(Comment::class);
  30. }
  31. /**
  32. * 댓글 추천/비추 삭제
  33. */
  34. public function data(SearchData $params): object
  35. {
  36. $query = $this->query();
  37. $query->select(
  38. 'tb_board.name AS boardName',
  39. 'tb_board.code',
  40. 'tb_post.subject',
  41. 'tb_comment.content',
  42. 'tb_comment_like.*',
  43. 'user_1.sid AS sid_1',
  44. 'user_1.name AS name_1',
  45. 'user_2.sid AS sid_2',
  46. 'user_2.name AS name_2'
  47. );
  48. if($params->keyword) {
  49. switch ($params->field) {
  50. case 'tb_comment_like.ip_address' :
  51. case 'user_1.name' :
  52. case 'user_1.email' :
  53. case 'user_2.name' :
  54. case 'user_2.email' :
  55. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  56. break;
  57. case 'tb_comment_like.id' :
  58. case 'tb_comment_like.post_id' :
  59. case 'user_1.id' :
  60. case 'user_1.sid' :
  61. case 'user_2.id' :
  62. case 'user_2.sid' :
  63. $query->where($params->field, '=', $params->keyword);
  64. break;
  65. }
  66. }
  67. if($params->startDate || $params->endDate) {
  68. if($params->startDate) {
  69. $query->where('created_at', '>=', $params->startDate . ' 00:00:00');
  70. }
  71. if($params->endDate) {
  72. $query->where('created_at', '<=', $params->endDate . ' 23:59:59');
  73. }
  74. }
  75. if($params->boardID) {
  76. $query->where('tb_comment_like.board_id', '=', $params->boardID);
  77. }
  78. $query->join('tb_board', 'tb_board.id', '=', 'tb_comment_like.board_id');
  79. $query->join('tb_post', 'tb_post.id', '=', 'tb_comment_like.post_id');
  80. $query->join('tb_comment', 'tb_comment.id', '=', 'tb_comment_like.comment_id');
  81. $query->leftJoin('users AS user_1', 'user_1.id', '=', 'tb_comment_like.user_id'); // 추천인
  82. $query->leftJoin('users AS user_2', 'user_2.id', '=', 'tb_comment.user_id'); // 작성자
  83. $query->orderByDesc('tb_comment_like.id');
  84. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  85. $total = $this->count();
  86. $rows = $list->count();
  87. return (object)[
  88. 'total' => $total,
  89. 'rows' => $rows,
  90. 'list' => $list
  91. ];
  92. }
  93. /**
  94. * 댓글 좋아요/싫어요 정보
  95. */
  96. public function info(Comment $comment, int $userID): CommentLike
  97. {
  98. return $this->where([
  99. ['post_id', $comment->post_id],
  100. ['comment_id', $comment->id],
  101. ['user_id', $userID]
  102. ])->firstOrNew();
  103. }
  104. /**
  105. * 댓글 좋아요/싫어요 등록
  106. */
  107. public function register(array $params): int
  108. {
  109. return $this->insertGetId($params);
  110. }
  111. /**
  112. * 댓글 좋아요/싫어요 삭제
  113. */
  114. public function remove(Comment $comment, int $userID): int
  115. {
  116. return $this->where([
  117. ['post_id', $comment->post_id],
  118. ['comment_id', $comment->id],
  119. ['user_id', $userID]
  120. ])->delete();
  121. }
  122. /**
  123. * 회원이 좋아요/싫어요 둘중 어느것 선택했는지 확인
  124. */
  125. public function isAlready(Comment $comment, int $userID, int $type): bool
  126. {
  127. return $this->where([
  128. ['post_id', $comment->post_id],
  129. ['comment_id', $comment->id],
  130. ['user_id', $userID],
  131. ['type', $type]
  132. ])->exists();
  133. }
  134. }