Skip to content

Commit

Permalink
Merge pull request laravel#3 from ablancobarreda/3--UploadImages
Browse files Browse the repository at this point in the history
3  upload images
  • Loading branch information
ablancobarreda authored Oct 14, 2022
2 parents 29fd760 + 6a932ad commit a3f430c
Show file tree
Hide file tree
Showing 56 changed files with 116 additions and 89 deletions.
171 changes: 98 additions & 73 deletions app/Http/Controllers/v1/BusinessController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;


// return response()->json(
// [
// 'code' => 'ok',
// 'message' => 'Test',
// 'request' => $request->all()
// ]
// );

class BusinessController extends Controller
{

Expand All @@ -23,105 +33,111 @@ public function getBusinesses(){
return response()->json(
[
'code' => 'ok',
'message' => 'Users',
'message' => 'Businesses',
'businesses' => $businesses
]
);
}

//section Get_Business
public function getBusinessById(Request $request){
public function getBusinessBySlug(Request $request){

$shop = Shop::whereId($request->businessId)->first();
$business = Shop::whereSlug($request->businessSlug)->first();

return response()->json(
[
'code' => 'ok',
'message' => 'Business',
'shop' => $shop
'business' => $business
]
);
}

//section New_Business
public function newBusiness(Request $request){

return response()->json(
[
'code' => 'ok',
'message' => 'Test',
'request' => $request->all()
]
);
try{
DB::beginTransaction();

$business = new Shop();

if($request->userId){
$business->user_id = $request->userId;
}
else{

$user = new User();

$user->name = $request->userName;
$user->email = $request->userEmail;
$user->password = Hash::make($request->userPassword);

$user->save();

$business->user_id = $user->id;
}

$business->name = $request->businessName;
$business->slug = Str::slug($request->businessUrl);
$business->description = $request->businessDescription;
$business->address = $request->businessAddress;
$business->phone = $request->businessPhone;
$business->email = $request->businessEmail;
$business->url = $request->businessUrl;
$business->comission = $request->businessComission;
if ($request->hasFile('businessAvatar')) {
$business->avatar = self::uploadImage($request->businessAvatar, $request->businessName);
}
if ($request->hasFile('businessCover')) {
$business->cover = self::uploadImage($request->businessCover, $request->businessName);
}

$business->save();

// try{
// DB::beginTransaction();
// $business = new Shop();
//
// if($request->userId){
// $business->user_id = $request->userId;
// }
// else{
//
// $user = new User();
//
// $user->name = $request->userName;
// $user->email = $request->userEmail;
// $user->password = Hash::make($request->userPassword);
//
// $user->save();
//
// $business->user_id = $user->id;
// }
//
// $business->name = $request->businessName;
// $business->description = $request->businessDescription;
// $business->address = $request->businessAddress;
// $business->phone = $request->businessPhone;
// $business->email = $request->businessEmail;
// $business->url = $request->businessUrl;
// if ($request->hasFile('avatar')) {
// $business->avatar = self::uploadImage($request->businessAvatar, $request->businessName);
// }
// if ($request->hasFile('cover')) {
// $business->cover = self::uploadImage($request->businessCover, $request->businessName);
// }
//
// $business->save();
//
// DB::commit();
//
// return response()->json(
// [
// 'code' => 'ok',
// 'message' => 'Business created successfuly',
// 'business' => $business
// ]
// );
// }
// catch(\Throwable $th){
// return response()->json(
// ['code' => 'error', 'message' => $th->getMessage()]
// );
// }
DB::commit();

return response()->json(
[
'code' => 'ok',
'message' => 'Business created successfully',
'business' => $business
]
);
}
catch(\Throwable $th){
return response()->json(
['code' => 'error', 'message' => $th->getMessage()]
);
}
}

//section Update_Business
public function updateBusiness(NewBusinessRequest $request){
public function updateBusiness(Request $request){

// return response()->json(
// [
// 'code' => 'ok',
// 'message' => 'Test',
// 'request' => $request->all()
// ]
// );
try{
DB::beginTransaction();

$business = Shop::whereId($request->businessId)->first();

$business->name = $request->businessName;
$business->slug = Str::slug($request->businessUrl);
$business->description = $request->businessDescription;
$business->address = $request->businessAddress;
$business->phone = $request->businessPhone;
$business->email = $request->businessEmail;
$business->url = $request->businessUrl;
if ($request->hasFile('avatar')) {
$business->comission = $request->businessComission;
if ($request->hasFile('businessAvatar')) {
$business->avatar = self::uploadImage($request->businessAvatar, $request->businessName);
}
if ($request->hasFile('cover')) {
if ($request->hasFile('businessCover')) {
$business->cover = self::uploadImage($request->businessCover, $request->businessName);
}

Expand All @@ -132,7 +148,7 @@ public function updateBusiness(NewBusinessRequest $request){
return response()->json(
[
'code' => 'ok',
'message' => 'Business updated successfuly',
'message' => 'Business updated successfully',
'business' => $business
]
);
Expand All @@ -148,13 +164,24 @@ public function updateBusiness(NewBusinessRequest $request){
public function deleteBusiness(Request $request){
try {
DB::beginTransaction();
Shop::whereId($request->businessId)->delete();

$result = Shop::whereId($request->businessId)->delete();

DB::commit();

if($result){
return response()->json(
[
'code' => 'ok',
'message' => 'Business deleted successfully'
]
);
}

return response()->json(
[
'code' => 'ok',
'message' => 'Business deleted successfuly'
'code' => 'error',
'message' => 'Business not found'
]
);

Expand All @@ -167,14 +194,12 @@ public function deleteBusiness(Request $request){
}

//section Upload_image
public static function uploadImage($path, $name)
{
public static function uploadImage($path, $name){
$image = $path;

$avatarName = $name . substr(uniqid(rand(), true), 7, 7) . '.webp';

$avatarName = $name . substr(uniqid(rand(), true), 7, 7) . '.png';

$img = Image::make($image->getRealPath())->encode('webp', 50)->orientate();
$img = Image::make($image->getRealPath())->encode('png', 50)->orientate();

$img->resize(null, 300, function ($constraint) {
$constraint->aspectRatio();
Expand Down
10 changes: 5 additions & 5 deletions app/Http/Controllers/v1/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function getUsers(){
//section Get_User
public function getUserById(Request $request){

$users = User::whereId($request->userId)->first();
$user = User::whereId($request->userId)->first();

return response()->json(
[
'code' => 'ok',
'message' => 'User',
'users' => $users
'user' => $user
]
);
}
Expand All @@ -64,7 +64,7 @@ public function newUser(NewUserRequest $request){
return response()->json(
[
'code' => 'ok',
'message' => 'User created successfuly',
'message' => 'User created successfully',
'user' => $user
]
);
Expand Down Expand Up @@ -98,7 +98,7 @@ public function updateUser(NewUserRequest $request){
return response()->json(
[
'code' => 'ok',
'message' => 'User updated successfuly',
'message' => 'User updated successfully',
'user' => $user
]
);
Expand All @@ -120,7 +120,7 @@ public function deleteUser(Request $request){
return response()->json(
[
'code' => 'ok',
'message' => 'User deleted successfuly'
'message' => 'User deleted successfully'
]
);

Expand Down
2 changes: 2 additions & 0 deletions app/Models/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* @property integer $id
* @property integer $comission
* @property string $name
* @property string $description
* @property string $cover
Expand All @@ -22,6 +23,7 @@
* @property string $created_at
* @property string $updated_at
* @property string $user_id
* @property string $slug
* @property ShopProduct[] $shopProducts
* @property ShopCoupon[] $shopCoupons
* @property Order[] $orders
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"intervention/image": "^2.7",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"laravel/tinker": "^2.7"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
Expand Down
4 changes: 2 additions & 2 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'root' => public_path(''),
'url' => env('APP_URL').'/public',
'visibility' => 'public',
'throw' => false,
],
Expand Down
Binary file added public/businessImages/CluzStudio246349a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/businessImages/CluzStudio296349a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/businessImages/CluzStudio3576349.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added public/businessImages/CluzStudio6516349.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/businessImages/CluzStudio816349a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/businessImages/CluzStudio836349a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/businessImages/CluzStudio896349a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/businessImages/CluzStudio9992836349.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/businessImages/CluzStudio9993386349.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed public/businessImages/TonyShop006347d.png
Binary file not shown.
Binary file removed public/businessImages/TonyShop146347d.png
Binary file not shown.
Binary file removed public/businessImages/TonyShop166347d.png
Binary file not shown.
Binary file removed public/businessImages/TonyShop176347d.png
Binary file not shown.
Binary file removed public/businessImages/TonyShop2396347.png
Binary file not shown.
Binary file removed public/businessImages/TonyShop2816347.png
Binary file not shown.
Binary file removed public/businessImages/TonyShop3606347.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop386347d.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop4096347.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop4126347.png
Diff not rendered.
Binary file added public/businessImages/TonyShop416349a.png
Binary file removed public/businessImages/TonyShop466347d.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop4776347.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop566347d.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop586347d.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop606347d.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop6166347.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop6596347.png
Diff not rendered.
Binary file added public/businessImages/TonyShop666349a.png
Binary file removed public/businessImages/TonyShop8016347.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop836347d.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop8406347.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop8676347.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop9226347.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop986347d.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop9886347.png
Diff not rendered.
Binary file removed public/businessImages/TonyShop9966347.png
Diff not rendered.
Binary file added public/businessImages/tonyto286349c.png
Binary file added public/businessImages/tonyto386349c.png
Binary file added public/businessImages/tonyto516349c.png
Binary file added public/businessImages/tonyto996349c.png
16 changes: 8 additions & 8 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
});

// section Routes_User
Route::get('/v1/user/getUsers', [\App\Http\Controllers\v1\UserController::class, 'getUsers']);
Route::get('/v1/user/getUser/{userId}', [\App\Http\Controllers\v1\UserController::class, 'getUserById']);
Route::get('/v1/user/all', [\App\Http\Controllers\v1\UserController::class, 'getUsers']);
Route::get('/v1/user/view/{userId}', [\App\Http\Controllers\v1\UserController::class, 'getUserById']);
Route::post('/v1/user/new', [\App\Http\Controllers\v1\UserController::class, 'newUser']);
Route::put('/v1/user/update/{userId}', [\App\Http\Controllers\v1\UserController::class, 'updateUser']);
Route::delete('/v1/user/delete/{userId}', [\App\Http\Controllers\v1\UserController::class, 'deleteUser']);
Route::post('/v1/user/update/{userId}', [\App\Http\Controllers\v1\UserController::class, 'updateUser']);
Route::delete('/v1/user/delete', [\App\Http\Controllers\v1\UserController::class, 'deleteUser']);

// section Routes_Business
Route::get('/v1/business/getBusinesses', [\App\Http\Controllers\v1\BusinessController::class, 'getBusinesses']);
Route::get('/v1/business/getBusiness/{businessId}', [\App\Http\Controllers\v1\BusinessController::class, 'getBusinessById']);
Route::get('/v1/business/all', [\App\Http\Controllers\v1\BusinessController::class, 'getBusinesses']);
Route::get('/v1/business/view/{businessSlug}', [\App\Http\Controllers\v1\BusinessController::class, 'getBusinessBySlug']);
Route::post('/v1/business/new', [\App\Http\Controllers\v1\BusinessController::class, 'newBusiness']);
Route::put('/v1/business/update/{businessId}', [\App\Http\Controllers\v1\BusinessController::class, 'updateBusiness']);
Route::delete('/v1/business/delete/{businessId}', [\App\Http\Controllers\v1\BusinessController::class, 'deleteBusiness']);
Route::post('/v1/business/update', [\App\Http\Controllers\v1\BusinessController::class, 'updateBusiness']);
Route::delete('/v1/business/delete', [\App\Http\Controllers\v1\BusinessController::class, 'deleteBusiness']);

// section Routes_Product

Expand Down

0 comments on commit a3f430c

Please sign in to comment.