44
55namespace App \Livewire \Forms \Inventory ;
66
7+ use App \Actions \Inventory \CreateItem ;
8+ use App \Actions \Inventory \UpdateItem ;
79use App \Enums \ItemType ;
810use App \Models \Item ;
9- use Illuminate \Http \UploadedFile ;
1011use Illuminate \Support \Facades \Auth ;
1112use Illuminate \Support \Facades \Storage ;
12- use Illuminate \Support \Str ;
1313use Illuminate \Validation \Rule ;
1414use Livewire \Features \SupportFileUploads \TemporaryUploadedFile ;
1515use Livewire \Form ;
@@ -70,15 +70,10 @@ public function save(): void
7070 ->mapWithKeys (fn (array $ pair ) => [$ pair ['key ' ] => $ pair ['value ' ]])
7171 ->all () ?: null ;
7272
73- $ photo = $ data ['photo ' ] ?? null ;
74- unset($ data ['photo ' ]);
75-
7673 if ($ this ->editingItem ) {
77- $ this ->editingItem ->update ($ data );
78- $ this ->handlePhotoUpdate ($ this ->editingItem , $ photo );
74+ (new UpdateItem )->handle ($ this ->editingItem , $ data , $ this ->removePhoto );
7975 } else {
80- $ item = Auth::user ()->currentTeam ->items ()->create ($ data );
81- $ this ->handlePhotoStore ($ item , $ photo );
76+ (new CreateItem )->handle (Auth::user ()->currentTeam , $ data );
8277 }
8378
8479 $ this ->reset ();
@@ -97,30 +92,4 @@ protected function rules(): array
9792 'photo ' => ['nullable ' , 'image ' , 'max:5120 ' ],
9893 ];
9994 }
100-
101- private function handlePhotoStore (Item $ item , mixed $ photo ): void
102- {
103- if ($ photo instanceof UploadedFile) {
104- $ filename = Str::slug ($ item ->name ) . '. ' . $ photo ->getClientOriginalExtension ();
105- $ path = $ photo ->storeAs ("teams/ {$ item ->team_id }/items " , $ filename );
106- $ item ->update (['photo_path ' => $ path ]);
107- }
108- }
109-
110- private function handlePhotoUpdate (Item $ item , mixed $ photo ): void
111- {
112- if ($ this ->removePhoto && ! $ photo instanceof UploadedFile) {
113- if ($ item ->photo_path ) {
114- Storage::delete ($ item ->photo_path );
115- }
116- $ item ->update (['photo_path ' => null ]);
117- } elseif ($ photo instanceof UploadedFile) {
118- if ($ item ->photo_path ) {
119- Storage::delete ($ item ->photo_path );
120- }
121- $ filename = Str::slug ($ item ->name ) . '. ' . $ photo ->getClientOriginalExtension ();
122- $ path = $ photo ->storeAs ("teams/ {$ item ->team_id }/items " , $ filename );
123- $ item ->update (['photo_path ' => $ path ]);
124- }
125- }
12695}
0 commit comments