暫無描述
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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 = 'Email atau Password yang anda masukkan salah';
  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. ]);
  116. if(!$validator->fails()){
  117. $result = Profile::create([
  118. 'user_id' => $request->user_id,
  119. 'fullname' => $request->fullname,
  120. 'phone_number' => $request->phone_number,
  121. 'gender' => $request->gender,
  122. 'is_have_organization' => $request->is_have_organization,
  123. 'organization_id' => !empty($request->organization_id)?$request->organization_id:NULL,
  124. ]);
  125. $res_status = true;
  126. $msg = 'Berhasil membuat user';
  127. $status_msg = $msg;
  128. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  129. }else {
  130. $res_status = false;
  131. $msg = 'Gagal Membuat user';
  132. $status_msg = $validator->errors();
  133. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  134. }
  135. }
  136. public function postUpdateProfile(Request $request)
  137. {
  138. $validator = Validator::make($request->all(), [
  139. 'user_id' => ['required'],
  140. 'fullname' => ['required'],
  141. 'phone_number' => ['required'],
  142. 'gender' => ['required'],
  143. 'is_have_organization' => ['required'],
  144. 'organization_id' => [],
  145. ]);
  146. if(!$validator->fails()){
  147. $profile = Profile::where('user_id', $request->user_id)->firstOrFail();
  148. $profile->user_id = $request->user_id;
  149. $profile->fullname = $request->fullname;
  150. $profile->phone_number = $request->phone_number;
  151. $profile->gender = $request->gender;
  152. $profile->is_have_organization = $request->is_have_organization;
  153. $profile->organization_id = !empty($request->organization_id)?$request->organization_id:'';
  154. $res_status = true;
  155. $msg = 'Berhasil membuat user';
  156. $status_msg = $msg;
  157. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  158. }else {
  159. $res_status = false;
  160. $msg = 'Gagal Membuat user';
  161. $status_msg = $validator->errors();
  162. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  163. }
  164. }
  165. public function postForgotPassword() {
  166. $credentials = request()->validate(['email' => 'required|email']);
  167. Password::sendResetLink($credentials);
  168. return response()->json(["msg" => 'Reset password link sent on your email id.']);
  169. }
  170. public function postResetPassword() {
  171. $credentials = request()->validate([
  172. 'email' => 'required|email',
  173. 'token' => 'required|string',
  174. 'password' => 'required|string|confirmed'
  175. ]);
  176. $reset_password_status = Password::reset($credentials, function ($user, $password) {
  177. $user->password = $password;
  178. $user->save();
  179. });
  180. if ($reset_password_status == Password::INVALID_TOKEN) {
  181. return response()->json(["msg" => "Invalid token provided"], 400);
  182. }
  183. return response()->json(["msg" => "Password has been successfully changed"]);
  184. }
  185. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  186. {
  187. $response['response'] = array(
  188. 'status' => $status,
  189. 'message' => $msg,
  190. 'status_msg' => $status_msg,
  191. );
  192. $response['param'] = !empty($param) ? $param : '';
  193. $response['results'] = $result;
  194. return response()->json($response, 200);
  195. }
  196. }