value; public int $is_show = IsShow::SHOW->value; public string $start_date = ''; public ?string $end_date = null; public array $thumbnail = []; public function rules(): array { return [ 'name' => ['required', 'string', 'max:30'], 'points' => ['required', new UnsignedInteger], 'stock' => ['nullable', new UnsignedInteger], 'category' => ['required', Rule::in(RewardCategory::cases())], 'is_show' => ['required', Rule::in(IsShow::cases())], 'start_date' => ['required', 'date'], 'end_date' => ['nullable', 'date', 'after_or_equal:start_date'], 'thumbnail' => 'array', ]; } public function validationAttributes(): array { return [ 'name' => 'nama', 'points' => 'poin', 'stock' => 'stok', 'category' => 'kategori', 'is_show' => 'visibilitas', 'start_date' => 'tanggal mulai', 'end_date' => 'tanggal berakhir', ]; } public function setReward(Reward $reward): void { $this->reward = $reward; $this->name = $reward->name; $this->points = formatCurrencyNumber($reward->points); $this->stock = formatCurrencyNumber($reward->stock); $this->category = $reward->category->value; $this->is_show = $reward->is_show->value; $this->start_date = $reward->start_date; $this->end_date = $reward->end_date; $this->thumbnail = $this->mapMediaCollection($reward->getMedia('thumbnail')); } public function store(): void { $this->validate(); DB::transaction(function () { $reward = Reward::create([ 'name' => $this->name, 'points' => parseRupiahToInt($this->points), 'stock' => parseRupiahToInt($this->stock), 'category' => $this->category, 'is_show' => $this->is_show, 'start_date' => $this->start_date, 'end_date' => $this->end_date, ]); $this->uploadMedia($this->thumbnail, $reward, 'thumbnail'); }); } public function update(): void { $this->validate(); DB::transaction(function () { $this->reward->update([ 'name' => $this->name, 'points' => parseRupiahToInt($this->points), 'stock' => $this->stock ? parseRupiahToInt($this->stock) : null, 'category' => $this->category, 'is_show' => $this->is_show, 'start_date' => $this->start_date, 'end_date' => $this->end_date, ]); $this->syncMedia($this->thumbnail, $this->reward, 'thumbnail'); $this->uploadMedia($this->thumbnail, $this->reward, 'thumbnail'); }); } }