Skip to content

Commit

Permalink
Added the new fields to command handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervriends committed Nov 25, 2021
1 parent 5118903 commit c584971
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
8 changes: 6 additions & 2 deletions src/Badge/Badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Badge extends AbstractModel

protected $dates = ['created_at'];

public static function build($icon, $name, $image, $order = 0, $description = null, $isVisible = 0)
public static function build($icon, $name, $image, $order = 0, $description = null, $isVisible = 0, $backgroundColor = null, $iconColor = null, $labelColor = null)
{
$badge = new static();
$badge->icon = $icon;
Expand All @@ -21,6 +21,9 @@ public static function build($icon, $name, $image, $order = 0, $description = nu
$badge->image = $image;
$badge->description = $description;
$badge->is_visible = $isVisible;
$badge->background_color = $backgroundColor;
$badge->icon_color = $iconColor;
$badge->label_color = $labelColor;

return $badge;
}
Expand All @@ -44,7 +47,8 @@ public function users()
/**
* User count
*/
public function earnedCount() {
public function earnedCount()
{
return $this->hasMany(UserBadge::class, 'badge_id')->count();
}
}
11 changes: 7 additions & 4 deletions src/Badge/Command/CreateBadgeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CreateBadgeHandler
* @param SettingsRepositoryInterface $settings
*/
public function __construct(
TranslatorInterface $translator,
TranslatorInterface $translator,
SettingsRepositoryInterface $settings,
BadgeValidator $validator
) {
Expand All @@ -53,15 +53,18 @@ public function handle(CreateBadge $command)
Arr::get($command->data, "attributes.image", null),
Arr::get($command->data, "attributes.order", 0),
Arr::get($command->data, "attributes.description", null),
Arr::get($command->data, "attributes.isVisible", true)
Arr::get($command->data, "attributes.isVisible", true),
Arr::get($command->data, "attributes.backgroundColor", null),
Arr::get($command->data, "attributes.iconColor", null),
Arr::get($command->data, "attributes.labelColor", null)
);

$badge->created_at = time();

// Validate
$this->validator->assertValid($badge->getDirty());


// Save
$badge->save();

Expand Down
34 changes: 27 additions & 7 deletions src/Badge/Command/UpdateBadgeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UpdateBadgeHandler
* @param SettingsRepositoryInterface $settings
*/
public function __construct(
TranslatorInterface $translator,
TranslatorInterface $translator,
SettingsRepositoryInterface $settings,
BadgeValidator $validator
) {
Expand All @@ -50,33 +50,53 @@ public function handle(UpdateBadge $command)
$badge = Badge::findOrFail($command->id);

// Update name
if(Arr::has($command->data, "attributes.name")) {
if (Arr::has($command->data, "attributes.name")) {
$badge->name = Arr::get($command->data, "attributes.name", null);
}

// Update icon
if(Arr::has($command->data, "attributes.icon")) {
if (Arr::has($command->data, "attributes.icon")) {
$badge->icon = Arr::get($command->data, "attributes.icon", null);
}

// Update image
if (Arr::has($command->data, "attributes.image")) {
$badge->image = Arr::get($command->data, "attributes.image", null);
}

// Update order
if(Arr::has($command->data, "attributes.order")) {
if (Arr::has($command->data, "attributes.order")) {
$badge->order = Arr::get($command->data, "attributes.order", null);
}

// Update description
if(Arr::has($command->data, "attributes.description")) {
if (Arr::has($command->data, "attributes.description")) {
$badge->description = Arr::get($command->data, "attributes.description", null);
}

// Update visibility
if(Arr::has($command->data, "attributes.isVisible")) {
if (Arr::has($command->data, "attributes.isVisible")) {
$badge->is_visible = Arr::get($command->data, "attributes.isVisible", null);
}

// Update icon color
if (Arr::has($command->data, "attributes.iconColor")) {
$badge->icon_color = Arr::get($command->data, "attributes.iconColor", null);
}

// Update background color
if (Arr::has($command->data, "attributes.backgroundColor")) {
$badge->background_color = Arr::get($command->data, "attributes.backgroundColor", null);
}

// Update label color
if (Arr::has($command->data, "attributes.labelColor")) {
$badge->label_color = Arr::get($command->data, "attributes.labelColor", null);
}

// Validate
$this->validator->assertValid($badge->getDirty());

// Save
$badge->save();

Expand Down

0 comments on commit c584971

Please sign in to comment.