Мутим бэк и фронт по тихой
This commit is contained in:
		
							parent
							
								
									77ca442f2e
								
							
						
					
					
						commit
						b02f61fac5
					
				|  | @ -0,0 +1,45 @@ | ||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | namespace App\Http\Controllers; | ||||||
|  | 
 | ||||||
|  | use Illuminate\Http\Request; | ||||||
|  | use App\Models\Wish; | ||||||
|  | 
 | ||||||
|  | class WishesController extends Controller | ||||||
|  | { | ||||||
|  |     public function getUserWishes(Request $request, string $user_id) | ||||||
|  |     { | ||||||
|  |         return Wish::where('user_id', '=', $user_id)->get(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public function create(Request $request) | ||||||
|  |     { | ||||||
|  |         $request->validate([ | ||||||
|  |             'user_id' => 'required|exists:users,id', | ||||||
|  |             'name' => 'required|string|max:256', | ||||||
|  |             'price' => 'nullable|numeric', | ||||||
|  |             'url' => 'nullable|url', | ||||||
|  |         ]); | ||||||
|  | 
 | ||||||
|  |         $wish = Wish::create($request->all()); | ||||||
|  |         return response()->json($wish, 201); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public function update(Request $request, Wish $wish) | ||||||
|  |     { | ||||||
|  |         $request->validate([ | ||||||
|  |             'name' => 'required|string|max:256', | ||||||
|  |             'price' => 'nullable|numeric', | ||||||
|  |             'url' => 'nullable|url', | ||||||
|  |         ]); | ||||||
|  | 
 | ||||||
|  |         $wish->update($request->all()); | ||||||
|  |         return response()->json($wish); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public function destroy(Wish $wish) | ||||||
|  |     { | ||||||
|  |         $wish->delete(); | ||||||
|  |         return response()->json(null, 204); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -0,0 +1,16 @@ | ||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | namespace App\Models; | ||||||
|  | 
 | ||||||
|  | use Illuminate\Database\Eloquent\Factories\HasFactory; | ||||||
|  | use Illuminate\Database\Eloquent\Model; | ||||||
|  | 
 | ||||||
|  | class Wish extends Model | ||||||
|  | { | ||||||
|  |     use HasFactory; | ||||||
|  |     protected $fillable = ['user_id', 'name', 'price', 'url']; | ||||||
|  | 
 | ||||||
|  |     public function user(){ | ||||||
|  |         return  $this->belongsTo(User::class); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -0,0 +1,9 @@ | ||||||
|  | .link-no-decor { | ||||||
|  |     color: white; | ||||||
|  |     text-decoration: none; | ||||||
|  |     cursor: pointer; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | .link-no-decor:hover { | ||||||
|  |     color: #093160; | ||||||
|  | } | ||||||
|  | @ -6,8 +6,6 @@ export const useUserStore = defineStore('user', { | ||||||
|         user: null, |         user: null, | ||||||
|         token: localStorage.getItem('auth_token') || null, |         token: localStorage.getItem('auth_token') || null, | ||||||
|     }), |     }), | ||||||
|     // could also be defined as
 |  | ||||||
|     // state: () => ({ count: 0 })
 |  | ||||||
|     actions: { |     actions: { | ||||||
|         setUser(user) { |         setUser(user) { | ||||||
|             this.user = user; |             this.user = user; | ||||||
|  |  | ||||||
|  | @ -0,0 +1,47 @@ | ||||||
|  | import {defineStore} from 'pinia'; | ||||||
|  | import axios from "axios"; | ||||||
|  | 
 | ||||||
|  | export const useWishStore = defineStore('wish', { | ||||||
|  |     state: () => ({ | ||||||
|  |         wishesList: [] | ||||||
|  |     }), | ||||||
|  |     actions: { | ||||||
|  |         pushWish(wish){ | ||||||
|  |             this.wishesList.push(wish); | ||||||
|  |         }, | ||||||
|  |         async getUserWishes(user_id, token){ | ||||||
|  |             let result = null; | ||||||
|  |             await axios.get(`/api/wish/user_wishes/${user_id.toString()}`, | ||||||
|  |                 { | ||||||
|  |                     headers: { | ||||||
|  |                         Authorization: `Bearer ${token}`, | ||||||
|  |                         token: token | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             ).then((response)=>{ | ||||||
|  |                 result = response.data; | ||||||
|  |             }); | ||||||
|  |             return result; | ||||||
|  |         }, | ||||||
|  |         async create(user_id, name, price, url, token){ | ||||||
|  |             let newWish = null; | ||||||
|  |             await axios.post(`/api/wish/create`, | ||||||
|  |                 { | ||||||
|  |                     user_id: user_id, | ||||||
|  |                     name: name, | ||||||
|  |                     price: price, | ||||||
|  |                     url: url | ||||||
|  |                 }, | ||||||
|  |                 { | ||||||
|  |                     headers: { | ||||||
|  |                         Authorization: `Bearer ${token}`, | ||||||
|  |                         token: token | ||||||
|  |                     }, | ||||||
|  |                 } | ||||||
|  |             ).then((response)=>{ | ||||||
|  |                 newWish = {status: response.status, statusText: response.statusText, data: response.data}; | ||||||
|  |             }); | ||||||
|  |             return newWish; | ||||||
|  |         } | ||||||
|  |     }, | ||||||
|  | }) | ||||||
|  | @ -68,14 +68,4 @@ export default { | ||||||
|     color: white; |     color: white; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| .link-no-decor { |  | ||||||
|     color: white; |  | ||||||
|     text-decoration: none; |  | ||||||
|     cursor: pointer; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| .link-no-decor:hover { |  | ||||||
|     color: #093160; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| </style> | </style> | ||||||
|  |  | ||||||
|  | @ -1,11 +1,36 @@ | ||||||
| <script> | <script> | ||||||
|  | import {useUserStore} from "../../store/user.js"; | ||||||
|  | import {useWishStore} from "../../store/wish.js"; | ||||||
|  | import axios from "axios"; | ||||||
| export default { | export default { | ||||||
|     name: "Wishlist" |     name: "Wishlist", | ||||||
|  |     data: () => ({ | ||||||
|  |         userStore: useUserStore(), | ||||||
|  |         wishStore: useWishStore() | ||||||
|  |     }), | ||||||
|  |     methods: { | ||||||
|  |         tryWishes(){ | ||||||
|  |             let token = this.userStore.token; | ||||||
|  |             let user_id = this.userStore.user['id']; | ||||||
|  |             // get wish list | ||||||
|  |             this.wishStore.getUserWishes(user_id, token).then((result)=>{ | ||||||
|  |                 console.log(result); | ||||||
|  |                 // push new wish | ||||||
|  |                 this.wishStore.create(user_id, 'Google', 42000, 'http://google.com/', token).then((response)=>{ | ||||||
|  |                     console.log(response); | ||||||
|  |                     // get wish list | ||||||
|  |                     this.wishStore.getUserWishes(user_id, token).then((resultOfResponse)=>{ | ||||||
|  |                         console.log(resultOfResponse); | ||||||
|  |                     }); | ||||||
|  |                 }); | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
| </script> | </script> | ||||||
| 
 | 
 | ||||||
| <template> | <template> | ||||||
|     <v-label>Hello world!</v-label> |     <v-label class="link-no-decor" @click="this.tryWishes">Hello world!</v-label> | ||||||
| </template> | </template> | ||||||
| 
 | 
 | ||||||
| <style scoped> | <style scoped> | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ | ||||||
| use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||||
| use Illuminate\Support\Facades\Route; | use Illuminate\Support\Facades\Route; | ||||||
| use App\Http\Controllers\AuthController; | use App\Http\Controllers\AuthController; | ||||||
|  | use \App\Http\Controllers\WishesController; | ||||||
| 
 | 
 | ||||||
| /* | /* | ||||||
| |-------------------------------------------------------------------------- | |-------------------------------------------------------------------------- | ||||||
|  | @ -28,3 +29,12 @@ | ||||||
|         Route::get('user', [AuthController::class, 'user']); |         Route::get('user', [AuthController::class, 'user']); | ||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
|  | 
 | ||||||
|  | Route::group(['prefix' => 'wish'], function () { | ||||||
|  |     Route::get('user_wishes/{user_id}', [WishesController::class, 'getUserWishes']); | ||||||
|  |     Route::group(['middleware' => 'auth:sanctum'], function() { | ||||||
|  |         Route::post('create', [WishesController::class, 'create']); | ||||||
|  |         Route::post('update', [WishesController::class, 'update']); | ||||||
|  |         Route::post('destroy', [WishesController::class, 'destroy']); | ||||||
|  |     }); | ||||||
|  | }); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue