| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api\Resource; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
use FluentCart\App\Models\ProductDownload; |
| 7 |
use FluentCart\Framework\Database\Orm\Builder; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class ProductDownloadResource extends BaseResourceApi |
| 11 |
{ |
| 12 |
public static function getQuery(): Builder |
| 13 |
{ |
| 14 |
return ProductDownload::query(); |
| 15 |
} |
| 16 |
|
| 17 |
public static function get(array $params = []) |
| 18 |
{ |
| 19 |
|
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Find downloadable file by ID |
| 24 |
* |
| 25 |
* @param int $id Required. The ID of the file to find. |
| 26 |
* @param array $params Optional. Additional parameters for finding file. |
| 27 |
* |
| 28 |
*/ |
| 29 |
public static function find($id, $params = []) |
| 30 |
{ |
| 31 |
return static::getQuery()->find($id); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Create a new file with the given data |
| 36 |
* |
| 37 |
* @param array $data Required. Array containing the necessary parameters for file creation. |
| 38 |
* [ |
| 39 |
* 'title' => (string) Required. The title of the file, |
| 40 |
* 'type' => (string) Required. The type of the file, |
| 41 |
* 'driver' => (string) Required. The driver of the file, |
| 42 |
* 'file_name' => (string) Required. The file_name of the file, |
| 43 |
* 'file_path' => (string) Required. The file_path of the file, |
| 44 |
* 'file_url' => (string) Required. The file_url of the file, |
| 45 |
* 'settings' => (string) Optional. The settings of the file, |
| 46 |
* 'serial' => (int) Required. The serial of the file, |
| 47 |
* ] |
| 48 |
* @param array $params Optional. Additional parameters for file creation. |
| 49 |
* [ |
| 50 |
* // Include optional parameters, if any. |
| 51 |
* ] |
| 52 |
* |
| 53 |
*/ |
| 54 |
public static function create($data, $params = []) |
| 55 |
{ |
| 56 |
$isCreated = static::getQuery()->create($data); |
| 57 |
|
| 58 |
if ($isCreated) { |
| 59 |
return static::makeSuccessResponse( |
| 60 |
$isCreated, |
| 61 |
__('File added successfully', 'fluent-cart') |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
return static::makeErrorResponse([ |
| 66 |
['code' => 400, 'message' => __('Failed to add file!', 'fluent-cart')] |
| 67 |
]); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Update file with the given data |
| 72 |
* |
| 73 |
* @param array $data Required. Array containing the necessary parameters for file update. |
| 74 |
* [ |
| 75 |
* 'title' => (string) Required. The title of the file, |
| 76 |
* 'type' => (string) Required. The type of the file, |
| 77 |
* 'driver' => (string) Required. The driver of the file, |
| 78 |
* 'file_name' => (string) Required. The file_name of the file, |
| 79 |
* 'file_path' => (string) Required. The file_path of the file, |
| 80 |
* 'file_url' => (string) Required. The file_url of the file, |
| 81 |
* 'settings' => (string) Optional. The settings of the file, |
| 82 |
* 'serial' => (int) Required. The serial of the file, |
| 83 |
* ] |
| 84 |
* @param int $id Required. The ID of the file. |
| 85 |
* @param array $params Optional. Additional parameters for file update. |
| 86 |
* [ |
| 87 |
* // Include optional parameters, if any. |
| 88 |
* ] |
| 89 |
* |
| 90 |
*/ |
| 91 |
public static function update($data, $id, $params = []) |
| 92 |
{ |
| 93 |
$isUpdated = ProductDownloadResource::find($id)->update($data); |
| 94 |
|
| 95 |
if ($isUpdated) { |
| 96 |
return static::makeSuccessResponse( |
| 97 |
$isUpdated, |
| 98 |
__('File updated successfully', 'fluent-cart') |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
return static::makeErrorResponse([ |
| 103 |
['code' => 400, 'message' => __('Failed to update file!', 'fluent-cart')] |
| 104 |
]); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Delete a downloadable file with the given ID and parameters. |
| 109 |
* |
| 110 |
* @param int $id Required. The ID of the product downloadable file to delete single file. |
| 111 |
* @param array $params Optional. Additional parameters for deleting multiple files. |
| 112 |
* [ |
| 113 |
* 'type' => (string) Optional. Possible deletion type (all|byProduct) |
| 114 |
* ('all - means delete files by product id except specific files') |
| 115 |
* ('byProduct - means delete files by product id') |
| 116 |
* 'post_id' => (int) Optional. Delete by particular product. |
| 117 |
* 'download_file_ids' => (array) Optional. Ignore to delete those files which are |
| 118 |
* in download file ids array. |
| 119 |
* ] |
| 120 |
* |
| 121 |
*/ |
| 122 |
public static function delete($id, $params = []) |
| 123 |
{ |
| 124 |
if (static::getQuery()->where('id', $id)->delete()) { |
| 125 |
return static::makeSuccessResponse( |
| 126 |
'', |
| 127 |
__('File has been deleted successfully', 'fluent-cart') |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
return static::makeErrorResponse([ |
| 132 |
['code' => 400, 'message' => __('Failed to delete!', 'fluent-cart')] |
| 133 |
]); |
| 134 |
} |
| 135 |
|
| 136 |
} |