/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request, ApiService $apiService) { /* Do some basic validation beause the form is a perfect representation of the resource on the api side. TODO, might want to move this into the a WWW service layer if it gets out of hand? (can it be tested well here?) */ $validator = Validator::make($request->all(), ['name' => 'required', 'msw_country' => 'required'], ['msw_country.required' => 'Please select the location']); if ($validator->fails()) { return redirect(route('spot.create'))->withErrors($validator)->withInput(); } /** **/ $response = $apiService->post(route('api.v1.0.spot.store'), $request->only(['msw_spot_id', 'name', 'public']))->addHeader(['Authorization' => 'Bearer ' . $request->cookie('token')])->dispatch(); if ($response->getStatusCode() === 200) { //update the user's country as a convenience $payload = json_decode(base64_decode(explode('.', $request->cookie('token'))[1])); //this gets the JWT payload. Can be faked by the user, but the API server side will be validating the request $apiService->patch(route('api.v1.0.user.update', $payload->sub), $request->only('msw_country'))->addHeader(['Authorization' => 'Bearer ' . $request->cookie('token')])->dispatch(); return redirect(route($request->get('from_route') ?: 'dashboard')); } else { if ($response->getStatusCode() === 400) { $content = $response->getContent(); return redirect(route('spot.create'))->withErrors((array) $content->errors)->withInput(); } else { if ($response->getStatusCode() === 403) { return redirect(route('login'))->withErrors(['generic' => ['Your session has expired, please login again']]); } else { return redirect(route('spot.create'))->withErrors(['generic' => ['There was a problem with the system, please try again']])->withInput(); } } } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @param ApiService $apiService * @return \Illuminate\Http\Response */ public function store(Request $request, ApiService $apiService) { $response = $apiService->post(route('api.v1.0.surf.store'), $request->all())->addHeader(['Authorization' => 'Bearer ' . $request->cookie('token')])->dispatch(); if ($response->getStatusCode() === 200) { return redirect(route('dashboard'))->with('message_success', 'Your surf session has been logged'); } else { if ($response->getStatusCode() === 400) { return redirect(route('surf.create'))->withErrors((array) $response->getContent()->errors)->withInput(); } else { return redirect(route('surf.create'))->withErrors(['generic' => ['There was a problem with the system, please try again']])->withInput(); } } }
public function postChangeLocation(Request $request, ApiService $apiService) { $payload = json_decode(base64_decode(explode('.', $request->cookie('token'))[1])); //this gets the JWT payload. Can be faked by the user, but the API server side will be validating the request $response = $apiService->patch(route('api.v1.0.user.update', $payload->sub), $request->only('msw_country'))->addHeader(['Authorization' => 'Bearer ' . $request->cookie('token')])->dispatch(); if ($response->getStatusCode() === 200) { //yay, it worked return redirect(route($request->get('from_route') ?: 'dashboard')); } else { if ($response->getStatusCode() === 400) { //validation issue, show errors $content = $response->getContent(); return redirect(route('change-location'))->withErrors((array) $content->errors)->withInput(); } else { //no idea what happened, give a nice generic error message return redirect(route('change-location'))->withErrors(['generic' => ['There was a problem updating your location']])->withInput(); } } }