Advertisement
Haotik

Untitled

Nov 15th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use app\models\Users;
  6. use yii\db\Exception;
  7. use yii\rest\Controller;
  8.  
  9. class UsersController extends ApiBaseController
  10. {
  11. public $modelClass = 'app\models\Users';
  12.  
  13. public function actionView() {
  14. try{
  15. return Users::find()->where(['userId'=>\Yii::$app->request->get('id')])->one();
  16. } catch (\Exception $x) {
  17. return "Пользователь с таким ID не найден";
  18. }
  19. }
  20.  
  21.  
  22. public function actionIndex(){
  23. $rights = $this->checkToken();
  24. if ($rights == "Expired token") return "$rights";
  25. if ($this->checkToken()) {
  26. return Users::find()->orderBy('userId')->all();
  27. } else {
  28. return "У вас нет прав к данному контенту";
  29. }
  30. }
  31.  
  32. public function actionUpdate(){
  33. $k = 0;
  34.  
  35. try {
  36. $user = Users::find()->where(['userId'=>\Yii::$app->request->get('id')])->one();
  37.  
  38. if (\Yii::$app->request->get('name') !== NULL) {
  39. $user->firstName = \Yii::$app->request->get('name');
  40. $k++;
  41. }
  42. if (\Yii::$app->request->get('lastname') !== NULL) {
  43. $user->last_name = \Yii::$app->request->get('lastname');
  44. $k++;
  45. }
  46. if (\Yii::$app->request->get('birthDate') !== NULL) {
  47. $user->birthDate = \Yii::$app->request->get('birthDate');
  48. $k++;
  49. }
  50. if (\Yii::$app->request->get('points') !== NULL) {
  51. $user->points = \Yii::$app->request->get('points');
  52. $k++;
  53. }
  54. if ($k == 0) {
  55. return "Не указаны данные для изменения";
  56. }
  57.  
  58. $user->save();
  59. return "Ok";
  60. } catch (Exception $x) {
  61. return "Что то пошло не так $x";
  62. }
  63. }
  64.  
  65. public function actionCreate() {
  66. return "Воспользуйтесь модулем аутенфикации";
  67. }
  68.  
  69. public function actionDelete() {
  70. return "Воспользуйтесь модулем аутенфикации";
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement