diff --git a/app/Http/Controllers/User/AccountController.php b/app/Http/Controllers/User/AccountController.php index 969c28c4..e4b0820c 100644 --- a/app/Http/Controllers/User/AccountController.php +++ b/app/Http/Controllers/User/AccountController.php @@ -49,56 +49,13 @@ class AccountController extends Controller * * @return InvoiceCollection */ - public function invoices() { + public function invoices() + { return new InvoiceCollection( Auth::user()->invoices() ); } - /** - * Update user profile - * - * @param Request $request - * @return ResponseFactory|\Illuminate\Http\Response - */ - public function update_profile(Request $request) - { - // Validate request - $validator = Validator::make($request->all(), [ - 'avatar' => 'file', - 'name' => 'string', - 'value' => 'string', - ]); - - // Return error - if ($validator->fails()) abort(400, 'Bad input'); - - // Get user - $user = Auth::user(); - - // Check if is demo - if (is_demo($user->id)) { - return Demo::response_204(); - } - - // Update data - if ($request->hasFile('avatar')) { - - // Update avatar - $avatar = store_avatar($request->file('avatar'), 'avatars'); - - // Update data - $user->update(['avatar' => $avatar]); - - } else { - - // Update text data - $user->update(make_single_input($request)); - } - - return response('Saved!', 204); - } - /** * Update user settings relationship * @@ -108,7 +65,9 @@ class AccountController extends Controller public function update_user_settings(Request $request) { // Validate request + // TODO: pridat validator do requestu $validator = Validator::make($request->all(), [ + 'avatar' => 'sometimes|file', 'name' => 'string', 'value' => 'string', ]); @@ -124,7 +83,17 @@ class AccountController extends Controller return Demo::response_204(); } - // Update text data + // Update avatar + if ($request->hasFile('avatar')) { + $user + ->settings() + ->update([ + 'avatar' => store_avatar($request->file('avatar')) + ]); + + return response('Saved!', 204); + } + $user ->settings() ->update( diff --git a/app/Http/Helpers/helpers.php b/app/Http/Helpers/helpers.php index dbc22b74..7472fbcd 100644 --- a/app/Http/Helpers/helpers.php +++ b/app/Http/Helpers/helpers.php @@ -227,16 +227,12 @@ function is_editor($shared) * Store user avatar to storage * * @param $image - * @param $path * @return string */ -function store_avatar($image, $path) +function store_avatar($image) { - // Get directory - $path = check_directory($path); - // Store avatar - $image_path = Str::random(8) . '-' . $image->getClientOriginalName(); + $image_path = Str::random(16) . '-' . $image->getClientOriginalName(); // Create intervention image $img = Image::make($image->getRealPath()); @@ -245,32 +241,28 @@ function store_avatar($image, $path) $img->fit('150', '150')->stream(); // Store thumbnail to disk - Storage::put($path . '/' . $image_path, $img); + Storage::put("avatars/$image_path", $img); // Return path to image - return $path . '/' . $image_path; + return "avatars/$image_path"; } /** * Store system image * * @param $image - * @param $path * @return string */ -function store_system_image($image, $path) +function store_system_image($image) { - // Get directory - $path = check_directory($path); - // Store avatar $image_path = Str::random(8) . '-' . str_replace(' ', '', $image->getClientOriginalName()); // Store image to disk - Storage::putFileAs($path, $image, $image_path); + Storage::putFileAs('system', $image, $image_path); // Return path to image - return $path . '/' . $image_path; + return `system/$image_path`; } /** diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index a6d6185b..6afdde17 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -27,7 +27,6 @@ class UserFactory extends Factory 'role' => $this->faker->randomElement( ['user', 'admin'] ), - 'name' => $this->faker->name(), 'email' => $this->faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => Hash::make('secret'), diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 15f0c600..6334ae67 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -16,8 +16,6 @@ class CreateUsersTable extends Migration Schema::create('users', function (Blueprint $table) { $table->uuid('id'); $table->enum('role', ['admin', 'user'])->default('user'); - $table->string('name'); - $table->string('avatar')->nullable(); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); diff --git a/database/migrations/2020_05_26_092649_create_user_settings_table.php b/database/migrations/2020_05_26_092649_create_user_settings_table.php index ab4cb864..8fa01907 100644 --- a/database/migrations/2020_05_26_092649_create_user_settings_table.php +++ b/database/migrations/2020_05_26_092649_create_user_settings_table.php @@ -16,6 +16,7 @@ class CreateUserSettingsTable extends Migration Schema::create('user_settings', function (Blueprint $table) { $table->uuid('user_id'); $table->integer('storage_capacity')->default(5); + $table->string('avatar')->nullable(); $table->text('name')->nullable(); $table->text('address')->nullable(); $table->text('state')->nullable(); diff --git a/routes/user.php b/routes/user.php index f90902ae..d9b12d73 100644 --- a/routes/user.php +++ b/routes/user.php @@ -14,7 +14,6 @@ Route::group(['middleware' => ['auth:sanctum']], function () { // Account Route::patch('/relationships/settings', [AccountController::class, 'update_user_settings']); Route::post('/password', [AccountController::class, 'change_password']); - Route::patch('/profile', [AccountController::class, 'update_profile']); Route::get('/subscription', [SubscriptionController::class, 'show']); Route::get('/invoices', [AccountController::class, 'invoices']); Route::get('/storage', [AccountController::class, 'storage']); diff --git a/tests/Feature/UserTest.php b/tests/Feature/AuthTest.php similarity index 77% rename from tests/Feature/UserTest.php rename to tests/Feature/AuthTest.php index 8fea63fd..e4cd53c8 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/AuthTest.php @@ -8,7 +8,7 @@ use Laravel\Sanctum\Sanctum; use Storage; use Tests\TestCase; -class UserTest extends TestCase +class AuthTest extends TestCase { use DatabaseMigrations; @@ -70,24 +70,4 @@ class UserTest extends TestCase 'password' => 'secret', ])->assertStatus(200); } - - /** - * @test - */ - public function it_update_user_settings() - { - $user = User::factory(User::class) - ->create(); - - Sanctum::actingAs($user); - - $this->patchJson('/api/user/relationships/settings', [ - 'name' => 'address', - 'value' => 'Jantar', - ])->assertStatus(204); - - $this->assertDatabaseHas('user_settings', [ - 'address' => 'Jantar', - ]); - } } diff --git a/tests/Feature/UserAccountTest.php b/tests/Feature/UserAccountTest.php index b669b270..245a4d5e 100644 --- a/tests/Feature/UserAccountTest.php +++ b/tests/Feature/UserAccountTest.php @@ -3,16 +3,25 @@ namespace Tests\Feature; use App\Models\User; +use App\Services\SetupService; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; +use Illuminate\Http\UploadedFile; use Laravel\Sanctum\Sanctum; +use Storage; use Tests\TestCase; class UserAccountTest extends TestCase { use DatabaseMigrations; + public function __construct() + { + parent::__construct(); + $this->setup = app()->make(SetupService::class); + } + /** * @test */ @@ -31,4 +40,49 @@ class UserAccountTest extends TestCase // TODO: login s novym heslom } + + /** + * @test + */ + public function it_update_user_settings() + { + $user = User::factory(User::class) + ->create(); + + Sanctum::actingAs($user); + + $this->patchJson('/api/user/relationships/settings', [ + 'name' => 'address', + 'value' => 'Jantar', + ])->assertStatus(204); + + $this->assertDatabaseHas('user_settings', [ + 'address' => 'Jantar', + ]); + } + + /** + * @test + */ + public function it_update_user_avatar() + { + Storage::fake('local'); + + $this->setup->create_directories(); + + $user = User::factory(User::class) + ->create(); + + Sanctum::actingAs($user); + + $avatar = UploadedFile::fake() + ->image('fake-image.jpg'); + + $this->patchJson('/api/user/relationships/settings', [ + 'avatar' => $avatar, + ])->assertStatus(204); + + Storage::disk('local') + ->assertExists($user->settings->avatar); + } }