FEATURE: Modify userdatas table with json file
This commit is contained in:
parent
48ad677a12
commit
6bef75e520
9 changed files with 137 additions and 16 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -17,3 +17,4 @@ yarn-error.log
|
|||
socialtree.sqlite
|
||||
database.db
|
||||
node_modules
|
||||
data.json
|
||||
|
|
6
package-lock.json
generated
6
package-lock.json
generated
|
@ -1,6 +0,0 @@
|
|||
{
|
||||
"name": "socialtree",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {}
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Http\Controllers\LoadDBController;
|
||||
use Illuminate\Console\Command;
|
||||
use League\Flysystem\Config;
|
||||
|
||||
|
@ -38,7 +39,14 @@ class loadAll extends Command
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
printf(env('JSON_FILE_PATH'));
|
||||
return Command::SUCCESS;
|
||||
if (file_exists(env('JSON_FILE_PATH'))) {
|
||||
$config = json_decode(file_get_contents(env('JSON_FILE_PATH')), true);
|
||||
$loadDBController = new LoadDBController();
|
||||
$loadDBController->loadUserdata($config['username'], $config['theme']);
|
||||
return Command::SUCCESS;
|
||||
} else {
|
||||
print('Error: JSON File does not exist at given location'."\n");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
22
src/app/Http/Controllers/LoadDBController.php
Normal file
22
src/app/Http/Controllers/LoadDBController.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\LoadDBService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class LoadDBController extends Controller
|
||||
{
|
||||
private $loadDBService;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->loadDBService = new LoadDBService();
|
||||
}
|
||||
|
||||
public function loadUserdata($username, $theme): void
|
||||
{
|
||||
$this->loadDBService->setusername($username);
|
||||
$this->loadDBService->settheme($theme);
|
||||
}
|
||||
}
|
|
@ -17,13 +17,6 @@ class SocialTreePageController extends Controller
|
|||
|
||||
public function index(): view
|
||||
{
|
||||
|
||||
$username = "Nikurasu";
|
||||
$links = [
|
||||
["id" => 1, "platform" => "YouTube", "link" => "https://youtube.com/test", "linktext" => "YouTube", "brandcolors" => true],
|
||||
["id" => 2, "platform" => "Twitter", "link" => "https://twitter.com/test", "linktext" => "Twitter", "brandcolors" => false],
|
||||
["id" => 3, "platform" => "LinkedIn", "link" => "https://linkedIn.com/test", "linktext" => "LinkedIn", "brandcolors" => true],
|
||||
];
|
||||
$links = $this->socialTreePageService->getLinks();
|
||||
$config = $this->socialTreePageService->getConfig()[0];
|
||||
$username = $config['username'];
|
||||
|
|
|
@ -21,7 +21,7 @@ class links extends Model
|
|||
* @var array
|
||||
*/
|
||||
protected $attributes = [
|
||||
'plattform' => null,
|
||||
'plattform' => 'null',
|
||||
'link' => 'null'
|
||||
];
|
||||
}
|
||||
|
|
|
@ -8,4 +8,9 @@ use Illuminate\Database\Eloquent\Model;
|
|||
class userdata extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $table = 'userdatas';
|
||||
protected $attributes = [
|
||||
'username'=> 'null',
|
||||
'theme'=> 'orange'
|
||||
];
|
||||
}
|
||||
|
|
46
src/app/Services/LoadDBDataMutator.php
Normal file
46
src/app/Services/LoadDBDataMutator.php
Normal file
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\userdata;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class LoadDBDataMutator
|
||||
{
|
||||
public function setusername($username): void
|
||||
{
|
||||
$ud = new userdata;
|
||||
$ud->username = $username;
|
||||
$ud->save();
|
||||
}
|
||||
|
||||
public function getAll(): array
|
||||
{
|
||||
return userdata::all()->toArray();
|
||||
}
|
||||
|
||||
public function deleteByID($id): void
|
||||
{
|
||||
userdata::where('id', $id)->delete();
|
||||
}
|
||||
|
||||
public function editUsernameByID($id, $username): void
|
||||
{
|
||||
userdata::where('id', $id)->update([
|
||||
'username' => $username,
|
||||
]);
|
||||
}
|
||||
public function settheme(string $theme): void
|
||||
{
|
||||
$ud = new userdata;
|
||||
$ud->theme = $theme;
|
||||
$ud->save();
|
||||
}
|
||||
public function editThemeByID(int $id, string $theme): void
|
||||
{
|
||||
userdata::where('id', $id)->update([
|
||||
'theme' => $theme,
|
||||
]);
|
||||
}
|
||||
}
|
52
src/app/Services/LoadDBService.php
Normal file
52
src/app/Services/LoadDBService.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
class LoadDBService
|
||||
{
|
||||
private $loadDBDataMutator;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->loadDBDataMutator = new LoadDBDataMutator();
|
||||
}
|
||||
|
||||
public function setusername($username): void
|
||||
{
|
||||
$this->reduceEntriesToOne($this->loadDBDataMutator->getAll());
|
||||
$userdata = $this->loadDBDataMutator->getAll();
|
||||
if (count($userdata) == 1)
|
||||
{
|
||||
$this->loadDBDataMutator->editUsernameByID($userdata[0]['id'], $username);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->loadDBDataMutator->setusername($username);
|
||||
}
|
||||
}
|
||||
public function settheme($theme): void
|
||||
{
|
||||
$this->reduceEntriesToOne($this->loadDBDataMutator->getAll());
|
||||
$userdata = $this->loadDBDataMutator->getAll();
|
||||
if (count($userdata) == 1)
|
||||
{
|
||||
$this->loadDBDataMutator->editThemeByID($userdata[0]['id'], $theme);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->loadDBDataMutator->settheme($theme);
|
||||
}
|
||||
}
|
||||
|
||||
private function reduceEntriesToOne($userdata): void
|
||||
{
|
||||
if (count($userdata)> 1)
|
||||
{
|
||||
for ($entries = 1; $entries < count($userdata); $entries++)
|
||||
{
|
||||
$this->loadDBDataMutator->deleteByID($userdata[$entries]['id']);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue