diff --git a/addons/default/visiosoft/advs-module/src/Adv/AdvApiCollection.php b/addons/default/visiosoft/advs-module/src/Adv/AdvApiCollection.php index 8423798c0..cd428ce96 100644 --- a/addons/default/visiosoft/advs-module/src/Adv/AdvApiCollection.php +++ b/addons/default/visiosoft/advs-module/src/Adv/AdvApiCollection.php @@ -1,5 +1,6 @@ dispatch(new CheckRequiredParams(['ad_id'], $params)); - if (!$ad = $this->newQuery()->find($params['ad_id'])) { - throw new \Exception(trans('visiosoft.module.advs::message.ad_doesnt_exist'),404); - } - if ($ad->created_by_id != Auth::id()) { - throw new \Exception(trans('visiosoft.module.advs::message.permission_error'),403); - } + $ad = $this->checkAd($params['ad_id']); + + $this->checkOwner($ad); return $ad->delete(); } + + public function updateAd(array $params) + { + $this->dispatch(new CheckRequiredParams(['ad_id'], $params)); + + $ad = $this->checkAd($params['ad_id']); + + $this->checkOwner($ad); + + unset($params['ad_id'],$params['id'], $params['created_at'], $params['updated_at'], + $params['deleted_at'], $params['created_by_id'], $params['updated_by_id']); + + + $update_params = [ + 'updated_by_id' => Auth::id(), + 'updated_at' => Carbon::now() + ]; + + $ad->update(array_merge($update_params,$params)); + + return $ad; + } + + public function getAds() + { + return $this->currentAds(); + } + + public function checkAd($id) + { + if (!$ad = $this->newQuery()->find($id)) { + throw new \Exception(trans('visiosoft.module.advs::message.ad_doesnt_exist'), 404); + die; + } + return $ad; + } + + public function checkOwner($ad) + { + if ($ad->created_by_id != Auth::id()) { + throw new \Exception(trans('visiosoft.module.advs::message.permission_error'), 403); + die; + } + } }