GroupsApiController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Http\Controllers\Groups;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Services\GroupService;
  6. use App\Models\Group;
  7. use App\Models\GroupCategory;
  8. use App\Models\GroupMember;
  9. use App\Services\Groups\GroupAccountService;
  10. class GroupsApiController extends Controller
  11. {
  12. public function __construct()
  13. {
  14. $this->middleware('auth');
  15. }
  16. protected function toJson($group, $pid = false)
  17. {
  18. return GroupService::get($group->id, $pid);
  19. }
  20. public function getConfig(Request $request)
  21. {
  22. return GroupService::config();
  23. }
  24. public function getGroupAccount(Request $request, $gid, $pid)
  25. {
  26. $res = GroupAccountService::get($gid, $pid);
  27. return response()->json($res);
  28. }
  29. public function getGroupCategories(Request $request)
  30. {
  31. $res = GroupService::categories();
  32. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  33. }
  34. public function getGroupsByCategory(Request $request)
  35. {
  36. $name = $request->input('name');
  37. $category = GroupCategory::whereName($name)->firstOrFail();
  38. $groups = Group::whereCategoryId($category->id)
  39. ->simplePaginate(6)
  40. ->map(function($group) {
  41. return GroupService::get($group->id);
  42. })
  43. ->filter(function($group) {
  44. return $group;
  45. })
  46. ->values();
  47. return $groups;
  48. }
  49. public function getRecommendedGroups(Request $request)
  50. {
  51. return [];
  52. }
  53. public function getSelfGroups(Request $request)
  54. {
  55. $selfOnly = $request->input('self') == true;
  56. $memberOnly = $request->input('member') == true;
  57. $pid = $request->user()->profile_id;
  58. $res = GroupMember::whereProfileId($request->user()->profile_id)
  59. ->when($selfOnly, function($q, $selfOnly) {
  60. return $q->whereRole('founder');
  61. })
  62. ->when($memberOnly, function($q, $memberOnly) {
  63. return $q->whereRole('member');
  64. })
  65. ->simplePaginate(4)
  66. ->map(function($member) use($pid) {
  67. $group = $member->group;
  68. return $this->toJson($group, $pid);
  69. });
  70. return response()->json($res);
  71. }
  72. }