-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from WebII-2020-2/develop
Develop
- Loading branch information
Showing
201 changed files
with
11,977 additions
and
12,459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Order; | ||
use Exception; | ||
use Illuminate\Http\Request; | ||
use JWTAuth; | ||
|
||
class AddressController extends Controller | ||
{ | ||
public function store(Request $request) | ||
{ | ||
$data = $request->all(); | ||
$user_authenticaded = JWTAuth::toUser(); | ||
|
||
try { | ||
$result_address = $user_authenticaded->address()->create([ | ||
'public_place' => $data['public_place'], | ||
'district' => $data['district'], | ||
'number' => $data['number'], | ||
'complement' => $data['complement'] ?? " ", | ||
'zip_code' => $data['zip_code'], | ||
'city' => $data['city'], | ||
'state' => $data['state'], | ||
'reference_point' => $data['reference_point'] ?? " " | ||
]); | ||
} catch (\Exception $exception) { | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel salvar o endereço.']; | ||
} | ||
|
||
if (isset($result_address) && !isset($error)) { | ||
return response()->json(['success' => true, 'data' => null, 'error' => $error ?? null]); | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null]); | ||
} | ||
|
||
public function show() | ||
{ | ||
$user_authenticaded = JWTAuth::toUser(); | ||
|
||
try{ | ||
$addresses = $user_authenticaded->address; | ||
|
||
$mounted_addresses = []; | ||
foreach($addresses as $address){ | ||
array_push($mounted_addresses, array( | ||
'id' => $address->id, | ||
'public_place' => $address->public_place, | ||
'district' => $address->district, | ||
'number' => $address->number, | ||
'complement' => $address->complement, | ||
'zip_code' => $address->zip_code, | ||
'city' => $address->city, | ||
'state' => $address->state, | ||
'reference_point' => $address->reference_point | ||
)); | ||
} | ||
}catch(\Exception $exception){ | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel listar os endereços.']; | ||
} | ||
|
||
if (isset($mounted_addresses) && !isset($error)) { | ||
return response()->json(['success' => true, 'data' => $mounted_addresses, 'error' => $error ?? null]); | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null]); | ||
} | ||
|
||
public function get($id) | ||
{ | ||
$user_authenticaded = JWTAuth::toUser(); | ||
|
||
try{ | ||
$address = $user_authenticaded->address()->where('id', $id)->first(); | ||
|
||
$mounted_address = array( | ||
'id' => $address->id, | ||
'public_place' => $address->public_place, | ||
'district' => $address->district, | ||
'number' => $address->number, | ||
'complement' => $address->complement, | ||
'zip_code' => $address->zip_code, | ||
'city' => $address->city, | ||
'state' => $address->state, | ||
'reference_point' => $address->reference_point | ||
); | ||
}catch(\Exception $exception){ | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel listar o endereço.']; | ||
} | ||
|
||
if (isset($mounted_address) && !isset($error)) { | ||
return response()->json(['success' => true, 'data' => $mounted_address, 'error' => $error ?? null]); | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null]); | ||
} | ||
|
||
public function update($id, Request $request) | ||
{ | ||
$data = $request->all(); | ||
$user_authenticaded = JWTAuth::toUser(); | ||
|
||
try { | ||
if(isset($data['public_place'])){ | ||
$address['public_place'] = $data['public_place']; | ||
} | ||
if(isset($data['district'])){ | ||
$address['district'] = $data['district']; | ||
} | ||
if(isset($data['number'])){ | ||
$address['number'] = $data['number']; | ||
} | ||
if(isset($data['complement'])){ | ||
$address['complement'] = $data['complement']; | ||
} | ||
if(isset($data['zip_code'])){ | ||
$address['zip_code'] = $data['zip_code']; | ||
} | ||
if(isset($data['city'])){ | ||
$address['city'] = $data['city']; | ||
} | ||
if(isset($data['state'])){ | ||
$address['state'] = $data['state']; | ||
} | ||
if(isset($data['reference_point'])){ | ||
$address['reference_point'] = $data['reference_point']; | ||
} | ||
|
||
$result_address = $user_authenticaded->address()->where('id', $id)->update($address); | ||
} catch (\Exception $exception) { | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel atualizar o endereço.']; | ||
} | ||
|
||
if (isset($result_address) && !isset($error) && $result_address) { | ||
return response()->json(['success' => true, 'data' => null, 'error' => $error ?? null]); | ||
}else{ | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel atualizar o endereço.']; | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null]); | ||
} | ||
|
||
public function delete($id) | ||
{ | ||
$user_authenticaded = JWTAuth::toUser(); | ||
|
||
|
||
try{ | ||
$order = Order::where('address_id', $id)->first(); | ||
if(!is_null($order)){ | ||
throw new Exception(); | ||
} | ||
$result = $user_authenticaded->address()->where('id', $id)->delete(); | ||
}catch(\Exception $exception){ | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel deletar o endereço.']; | ||
} | ||
|
||
if (isset($result) && !isset($error) && $result) { | ||
return response()->json(['success' => true, 'data' => null, 'error' => $error ?? null]); | ||
}else{ | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel deletar o endereço.']; | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Banner; | ||
use App\Models\Product; | ||
use App\Models\BannerProduct; | ||
use App\Models\ProductPromotion; | ||
use Illuminate\Http\Request; | ||
|
||
|
||
class BannerController extends Controller | ||
{ | ||
public function store(Request $request){ | ||
|
||
$data = $request->all(); | ||
|
||
$data_image = preg_split("/^data:(.*);base64,/",$data['image'], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); | ||
|
||
try{ | ||
$result_banner = Banner::create([ | ||
'title' => $data['title'], | ||
'description' => $data['description'], | ||
'active' => $data['active'], | ||
'mime_type' => $data_image[0], | ||
'image' => base64_decode($data_image[1]), | ||
]); | ||
|
||
foreach ($request->input('products', []) as $product) { | ||
|
||
$banner_product = $result_banner->bannerProduct()->create(array( | ||
'product_id' => $product | ||
)); | ||
} | ||
}catch(\Exception $exception){ | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel salvar o Banner.']; | ||
} | ||
|
||
if(isset($result_banner) && !isset($error)){ | ||
return response()->json(['success' => true, 'data' => null, 'error' => $error ?? null], 200); | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null], 400); | ||
} | ||
|
||
public function show(){ | ||
try{ | ||
$banners = Banner::with('bannerProduct.product')->get(); | ||
|
||
$mounted_banners = []; | ||
foreach ($banners as $banner) { | ||
$bannerProducts = $banner->bannerProduct; | ||
|
||
$mounted_products = []; | ||
foreach ($bannerProducts as $product) { | ||
array_push($mounted_products, ['id' => $product->product_id]); | ||
} | ||
|
||
array_push($mounted_banners, array( | ||
'id' => $banner->id, | ||
'title' => $banner->title, | ||
'description' => $banner->description, | ||
'image' => 'data:'.$banner->mime_type.';base64,'.base64_encode($banner->image), | ||
'active' => $banner->active, | ||
'products' => $mounted_products | ||
)); | ||
} | ||
}catch(\Exception $exception){ | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel listar os banners.']; | ||
} | ||
|
||
if(isset($mounted_banners) && !isset($error)){ | ||
return response()->json(['success' => true, 'data' => $mounted_banners, 'error' => $error ?? null], 200); | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null], 400); | ||
} | ||
|
||
public function get($id){ | ||
try{ | ||
$banner = Banner::with('bannerProduct.product.productPromotion.promotion')->where('id', $id)->first(); | ||
|
||
$banner_products = $banner->bannerProduct; | ||
|
||
$products = []; | ||
foreach($banner_products as $banner_product){ | ||
unset($product_with_promotion); | ||
$product_promotion = ProductPromotion::where('product_id', $banner_product->product_id) | ||
->join('promotions as p', 'p.id', '=', 'product_promotions.promotion_id')->first(); | ||
|
||
if (!is_null($product_promotion)) { | ||
$product_with_promotion = $product_promotion->type == 1 ? | ||
($banner_product->product->unitary_value - $product_promotion->value) : | ||
$banner_product->product->unitary_value - ($banner_product->product->unitary_value * ($product_promotion->value / 100)); | ||
} | ||
|
||
$mounted_products = array( | ||
'id' => $banner_product->product->id, | ||
'name' => $banner_product->product->name, | ||
'quantity' => $banner_product->product->quantity, | ||
'unitary_value' => $banner_product->product->unitary_value, | ||
'value_promotion' => $product_with_promotion ?? null, | ||
'image' => 'data:' . $banner_product->product->mime_type . ';base64,' . base64_encode($banner_product->product->image), | ||
); | ||
array_push($products, $mounted_products); | ||
} | ||
|
||
$mounted_banner = array( | ||
"id" => $banner->id, | ||
'title' => $banner->title, | ||
'description' => $banner->description, | ||
'image' => 'data:' . $banner->mime_type . ';base64,' . base64_encode($banner->image), | ||
'active' => $banner->active, | ||
'products' => $products | ||
); | ||
}catch(\Exception $exception){ | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel listar o banner.', $exception]; | ||
} | ||
|
||
if(isset($mounted_banner) && !isset($error)){ | ||
return response()->json(['success' => true, 'data' => $mounted_banner, 'error' => $error ?? null], 200); | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null], 400); | ||
} | ||
|
||
public function update($id, Request $request){ | ||
$data = $request->only(['title', 'description', 'active', 'image', 'products']); | ||
|
||
if(isset($data['image'])){ | ||
$data_image = preg_split("/^data:(.*);base64,/",$data['image'], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); | ||
|
||
$data['image'] = base64_decode($data_image[1]); | ||
$data['mime_type'] = $data_image[0]; | ||
} | ||
|
||
try { | ||
$banner = Banner::find($id); | ||
|
||
$result_banner = $banner->update($data); | ||
|
||
$result_banner_product = $banner->bannerProduct()->whereNotIn('product_id', $data['products'] ?? [])->delete(); | ||
|
||
foreach ($data['products'] ?? [] as $product) { | ||
$banner_product = BannerProduct::updateOrCreate( | ||
['banner_id' => $id, 'product_id' => $product] | ||
); | ||
} | ||
} catch (\Exception $exception) { | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel atualizar o banner.', $exception]; | ||
} | ||
|
||
if(isset($result_banner) && !isset($error) && $result_banner){ | ||
return response()->json(['success' => true, 'data' => null, 'error' => $error ?? null], 200); | ||
}else{ | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel atualizar o produto.']; | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null], 400); | ||
} | ||
|
||
public function delete($id, Request $request){ | ||
try { | ||
$banner = Banner::where('id', $id)->delete(); | ||
} catch (\Exception $exception) { | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel deletar o banner.']; | ||
} | ||
|
||
if (isset($banner) && !isset($error) && $banner) { | ||
return response()->json(['success' => true, 'data' => null, 'error' => $error ?? null], 200); | ||
} else { | ||
$error = ['code' => 2, 'error_message' => 'Não foi possivel deletar o banner.']; | ||
} | ||
|
||
return response()->json(['success' => false, 'data' => null, 'error' => $error ?? null], 400); | ||
} | ||
} |
Oops, something went wrong.