FEATURE: Modify userdatas table with json file

This commit is contained in:
Nikurasu 2021-12-18 23:13:48 +01:00
parent 48ad677a12
commit 6bef75e520
9 changed files with 137 additions and 16 deletions

1
.gitignore vendored
View file

@ -17,3 +17,4 @@ yarn-error.log
socialtree.sqlite
database.db
node_modules
data.json

6
package-lock.json generated
View file

@ -1,6 +0,0 @@
{
"name": "socialtree",
"lockfileVersion": 2,
"requires": true,
"packages": {}
}

View file

@ -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;
}
}
}

View 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);
}
}

View file

@ -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'];

View file

@ -21,7 +21,7 @@ class links extends Model
* @var array
*/
protected $attributes = [
'plattform' => null,
'plattform' => 'null',
'link' => 'null'
];
}

View file

@ -8,4 +8,9 @@ use Illuminate\Database\Eloquent\Model;
class userdata extends Model
{
use HasFactory;
protected $table = 'userdatas';
protected $attributes = [
'username'=> 'null',
'theme'=> 'orange'
];
}

View 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,
]);
}
}

View 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']);
}
}
}
}