| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace App\Services;
- use App\Models\Cart;
- use Illuminate\Support\Arr;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Support\Facades\DB;
- class CartService
- {
- public function getAll(): Collection
- {
- return Cart::query()
- ->with(['items.schedule'])
- ->orderBy('created_at', 'desc')
- ->get();
- }
- public function findById(int $id): ?Cart
- {
- return Cart::query()
- ->with(['items.schedule'])
- ->find($id);
- }
- public function create(array $data): Cart
- {
- return Cart::create($data);
- }
- public function createWithSchedules(array $data): Cart
- {
- return DB::transaction(function () use ($data) {
- $items = $this->scheduleItems($data);
- $schedules = app(ScheduleService::class)->createSingleOrMultiple(
- baseData: $this->baseScheduleData($data),
- schedules: $items,
- );
- $cart = Cart::create([
- 'client_id' => $data['client_id'],
- ]);
- foreach ($schedules as $schedule) {
- $cart->items()->create([
- 'schedule_id' => $schedule->id,
- ]);
- }
- return $cart->fresh(['items.schedule.client.user', 'items.schedule.provider.user', 'items.schedule.address']);
- });
- }
- public function update(int $id, array $data): ?Cart
- {
- $model = $this->findById($id);
- if (! $model) {
- return null;
- }
- $model->update($data);
- return $model->fresh();
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (! $model) {
- return false;
- }
- return $model->delete();
- }
- //
- private function baseScheduleData(array $data): array
- {
- return Arr::except($data, [
- 'schedules',
- 'dates',
- 'date',
- 'period_type',
- 'start_time',
- 'end_time',
- 'total_amount',
- 'offers_meal',
- ]);
- }
- private function scheduleItems(array $data): array
- {
- if (! empty($data['schedules'])) {
- return $data['schedules'];
- }
- if (! empty($data['dates'])) {
- return array_map(
- fn (string $date) => array_merge(
- Arr::only($data, ['period_type', 'start_time', 'end_time', 'total_amount', 'offers_meal']),
- ['date' => $date],
- ),
- $data['dates'],
- );
- }
- return [
- Arr::only($data, ['date', 'period_type', 'start_time', 'end_time', 'total_amount', 'offers_meal']),
- ];
- }
- }
|