暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserController.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use App\User;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Http\Request;
  8. use Validator;
  9. use Hash;
  10. use Helper;
  11. use Illuminate\Support\Facades\Password;
  12. use App\Model\Profile;
  13. class UserController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. DB::enableQueryLog();
  18. }
  19. public function getAll()
  20. {
  21. $query = User::select('*', 'users.id as user_id', 'profiles.id as profile_id')->leftJoin('profiles', 'profiles.user_id', 'users.id')->get();
  22. if($query){
  23. $result = $query;
  24. $res_status = true;
  25. $msg = 'Mendapatkan Data';
  26. $status_msg = $msg;
  27. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  28. }else {
  29. $res_status = false;
  30. $msg = 'Data tidak ditemukan';
  31. $status_msg = $msg;
  32. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  33. }
  34. }
  35. public function getSingle($data_id)
  36. {
  37. $query = User::select('users.*', 'profiles.*', 'users.id as user_id', 'profiles.id as profile_id', DB::raw('SUM(point_logs.point) as total_point'))->leftJoin('profiles', 'profiles.user_id', 'users.id')->leftJoin('point_logs', 'point_logs.user_id', 'users.id')->where('users.id', Helper::hash($data_id, 'decode'))->get();
  38. if($query){
  39. $result = $query;
  40. $res_status = true;
  41. $msg = 'Mendapatkan Data';
  42. $status_msg = $msg;
  43. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  44. }else {
  45. $res_status = false;
  46. $msg = 'Data tidak ditemukan';
  47. $status_msg = $msg;
  48. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  49. }
  50. }
  51. public function postLogin(Request $request)
  52. {
  53. $auth = Auth::attempt($request->only('email', 'password'));
  54. if($auth){
  55. $result['user'] = Auth::user();
  56. $res_status = true;
  57. $msg = 'Mendapatkan Data';
  58. $status_msg = $msg;
  59. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  60. }else {
  61. $res_status = false;
  62. $msg = 'Data tidak ditemukan';
  63. $status_msg = $msg;
  64. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  65. }
  66. }
  67. public function postRegister(Request $request)
  68. {
  69. $validator = Validator::make($request->all(), [
  70. 'name' => ['required', 'string', 'max:255'],
  71. 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
  72. 'password' => ['required', 'string', 'min:8', 'confirmed'],
  73. ]);
  74. if(!$validator->fails()){
  75. $result = User::create([
  76. 'name' => $request->name,
  77. 'email' => $request->email,
  78. 'password' => Hash::make($request->password),
  79. 'gambar' => !empty($request->gambar)?Helper::base64_to_image($request->gambar, 'transportation'):'',
  80. ]);
  81. $data_role['role_id'] = 2; //driver role
  82. $result->assignRole($data_role);
  83. // $file = $request->file('photo');
  84. // if($file){
  85. // $user = User::find($result->id);
  86. // // $nama_file = time()."_".$file->getClientOriginalName();
  87. // // $tujuan_upload = 'photo';
  88. // // //$file->move($tujuan_upload,$nama_file);
  89. // // Storage::putFileAs('public/photo/user/', $file, $nama_file );
  90. // // //File::delete('image/'.$user->photo);
  91. // // Storage::delete('public/photo/user/'.$user->photo);
  92. // // $user->photo = $nama_file;
  93. // $user->save();
  94. // }
  95. $res_status = true;
  96. $msg = 'Berhasil membuat user';
  97. $status_msg = $msg;
  98. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  99. }else {
  100. $res_status = false;
  101. $msg = 'Gagal Membuat user';
  102. $status_msg = $validator->errors();
  103. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  104. }
  105. }
  106. public function postCreateProfile(Request $request)
  107. {
  108. $validator = Validator::make($request->all(), [
  109. 'user_id' => ['required'],
  110. 'fullname' => ['required'],
  111. 'phone_number' => ['required'],
  112. 'gender' => ['required'],
  113. 'is_have_organization' => ['required'],
  114. 'organization_id' => [],
  115. 'id_member' => [],
  116. ]);
  117. if(!$validator->fails()){
  118. $result = Profile::create([
  119. 'user_id' => $request->user_id,
  120. 'fullname' => $request->fullname,
  121. 'phone_number' => $request->phone_number,
  122. 'gender' => $request->gender,
  123. 'is_have_organization' => $request->is_have_organization,
  124. 'organization_id' => !empty($request->organization_id)?$request->organization_id:NULL,
  125. 'id_member' => !empty($request->id_member)?$request->id_member:'',
  126. ]);
  127. $res_status = true;
  128. $msg = 'Berhasil membuat user';
  129. $status_msg = $msg;
  130. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  131. }else {
  132. $res_status = false;
  133. $msg = 'Gagal Membuat user';
  134. $status_msg = $validator->errors();
  135. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  136. }
  137. }
  138. public function postUpdateProfile(Request $request)
  139. {
  140. $validator = Validator::make($request->all(), [
  141. 'user_id' => ['required'],
  142. 'fullname' => ['required'],
  143. 'phone_number' => ['required'],
  144. 'gender' => ['required'],
  145. 'is_have_organization' => ['required'],
  146. 'organization_id' => [],
  147. 'id_member' => [],
  148. ]);
  149. if(!$validator->fails()){
  150. $profile = Profile::where('user_id', $request->user_id)->firstOrFail();
  151. $profile->user_id = $request->user_id;
  152. $profile->fullname = $request->fullname;
  153. $profile->phone_number = $request->phone_number;
  154. $profile->gender = $request->gender;
  155. $profile->is_have_organization = $request->is_have_organization;
  156. $profile->organization_id = !empty($request->organization_id)?$request->organization_id:'';
  157. $profile->id_member = !empty($request->id_member)?$request->id_member:'';
  158. $res_status = true;
  159. $msg = 'Berhasil membuat user';
  160. $status_msg = $msg;
  161. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  162. }else {
  163. $res_status = false;
  164. $msg = 'Gagal Membuat user';
  165. $status_msg = $validator->errors();
  166. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  167. }
  168. }
  169. public function postForgotPassword() {
  170. $credentials = request()->validate(['email' => 'required|email']);
  171. Password::sendResetLink($credentials);
  172. return response()->json(["msg" => 'Reset password link sent on your email id.']);
  173. }
  174. public function postResetPassword() {
  175. $credentials = request()->validate([
  176. 'email' => 'required|email',
  177. 'token' => 'required|string',
  178. 'password' => 'required|string|confirmed'
  179. ]);
  180. $reset_password_status = Password::reset($credentials, function ($user, $password) {
  181. $user->password = $password;
  182. $user->save();
  183. });
  184. if ($reset_password_status == Password::INVALID_TOKEN) {
  185. return response()->json(["msg" => "Invalid token provided"], 400);
  186. }
  187. return response()->json(["msg" => "Password has been successfully changed"]);
  188. }
  189. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  190. {
  191. $response['response'] = array(
  192. 'status' => $status,
  193. 'message' => $msg,
  194. 'status_msg' => $status_msg,
  195. );
  196. $response['param'] = !empty($param) ? $param : '';
  197. $response['results'] = $result;
  198. return response()->json($response, 200);
  199. }
  200. }