<?php

namespace App\Classes\Modules\Milestones\Services;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Classes\Modules\Milestones\DataTransferObjects\AchievementObject;
use Illuminate\Database\QueryException;

class AssignsReward extends AbstractUpdateRecord
{
    /**
     * @param AchievementObject $object
     * @return \Illuminate\Database\Eloquent\Model
     * @throws MalformedRequestException
     */
    public function execute(AchievementObject $object)
    {
        try {
            // $object->getMilestone()->rewards()->attach($object->getReward()->id);
            $object->getMilestone()->rewards()->sync($object->getRewardIds());

            return $object->getMilestone();

        } catch (QueryException $exception){
            throw new MalformedRequestException($exception);
        }
    }
}
