GroupController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Group;
  4. use App\TwoFAccount;
  5. use App\Services\GroupService;
  6. use App\Http\Requests\GroupStoreRequest;
  7. use App\Http\Requests\GroupAssignRequest;
  8. use App\Http\Resources\GroupResource;
  9. use App\Http\Resources\TwoFAccountCollection;
  10. use Illuminate\Http\Request;
  11. class GroupController extends Controller
  12. {
  13. /**
  14. * The TwoFAccount Service instance.
  15. */
  16. protected $groupService;
  17. /**
  18. * Create a new controller instance.
  19. *
  20. * @param GroupService $groupService
  21. * @return void
  22. */
  23. public function __construct(GroupService $groupService)
  24. {
  25. $this->groupService = $groupService;
  26. }
  27. /**
  28. * Display a listing of the resource.
  29. *
  30. * @return \Illuminate\Http\Response
  31. */
  32. public function index()
  33. {
  34. $groups = $this->groupService->getAll();
  35. return GroupResource::collection($groups);
  36. }
  37. /**
  38. * Store a newly created resource in storage.
  39. *
  40. * @param \App\Http\Requests\GroupRequest $request
  41. * @return \App\Http\Resources\GroupResource
  42. */
  43. public function store(GroupStoreRequest $request)
  44. {
  45. $validated = $request->validated();
  46. $group = $this->groupService->Create($validated);
  47. return (new GroupResource($group))
  48. ->response()
  49. ->setStatusCode(201);
  50. }
  51. /**
  52. * Display the specified resource.
  53. *
  54. * @param \App\Group $group
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function show(Group $group)
  58. {
  59. return new GroupResource($group);
  60. }
  61. /**
  62. * Update the specified resource in storage.
  63. *
  64. * @param \App\Http\Requests\GroupRequest $request
  65. * @param \App\Group $group
  66. * @return \App\Http\Resources\GroupResource
  67. */
  68. public function update(GroupStoreRequest $request, Group $group)
  69. {
  70. $validated = $request->validated();
  71. $this->groupService->update($group, $validated);
  72. return new GroupResource($group);
  73. }
  74. /**
  75. * Associate the specified accounts with the group
  76. *
  77. * @param \App\Http\Requests\GroupAssignRequest $request
  78. * @param \App\Group $group
  79. * @return \Illuminate\Http\Response
  80. */
  81. public function assignAccounts(GroupAssignRequest $request, Group $group)
  82. {
  83. $validated = $request->validated();
  84. $this->groupService->assign($validated['ids'], $group);
  85. return response()->json($group, 200);
  86. }
  87. /**
  88. * Get accounts assign to the group
  89. *
  90. * @param \App\Group $group
  91. * @return \App\Http\Resources\TwoFAccountCollection
  92. */
  93. public function accounts(Group $group)
  94. {
  95. $groups = $this->groupService->getAccounts($group);
  96. return new TwoFAccountCollection($groups);
  97. }
  98. /**
  99. * Remove the specified resource from storage.
  100. *
  101. * @param \App\Group $group
  102. * @return \Illuminate\Http\Response
  103. */
  104. public function destroy(Group $group)
  105. {
  106. $this->groupService->delete($group->id);
  107. return response()->json(null, 204);
  108. }
  109. }