mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Save a product
This commit is contained in:
parent
08c4579464
commit
39617644ad
33
app/Factory/ProductFactory.php
Normal file
33
app/Factory/ProductFactory.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Factory;
|
||||
|
||||
use App\Models\Product;
|
||||
|
||||
class ProductFactory
|
||||
{
|
||||
public static function create(int $company_id, int $user_id) :Product
|
||||
{
|
||||
$product = new Product;
|
||||
$product->company_id = $company_id;
|
||||
$product->user_id = $user_id;
|
||||
|
||||
$product->product_key = '';
|
||||
$product->notes = '';
|
||||
$product->cost = 0;
|
||||
$product->qty = 0;
|
||||
$product->tax_name1 = '';
|
||||
$product->tax_rate1 = 0;
|
||||
$product->tax_name2 = '';
|
||||
$product->tax_rate2 = 0;
|
||||
$product->custom_value1 = '';
|
||||
$product->custom_value2 = '';
|
||||
$product->custom_value3 = '';
|
||||
$product->custom_value4 = '';
|
||||
$product->is_deleted = 0;
|
||||
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -55,23 +55,23 @@ class BaseController extends Controller
|
||||
|
||||
protected function listResponse($query)
|
||||
{
|
||||
$transformer = new $this->entityTransformer(Input::get('serializer'));
|
||||
$transformer = new $this->entity_transformer(Input::get('serializer'));
|
||||
|
||||
$includes = $transformer->getDefaultIncludes();
|
||||
$includes = $this->getRequestIncludes($includes);
|
||||
|
||||
$query->with($includes);
|
||||
|
||||
$data = $this->createCollection($query, $transformer, $this->entityType);
|
||||
$data = $this->createCollection($query, $transformer, $this->entity_type);
|
||||
|
||||
return $this->response($data);
|
||||
}
|
||||
|
||||
protected function createCollection($query, $transformer, $entityType)
|
||||
protected function createCollection($query, $transformer, $entity_type)
|
||||
{
|
||||
|
||||
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
|
||||
$entityType = null;
|
||||
$entity_type = null;
|
||||
}
|
||||
|
||||
if (is_a($query, "Illuminate\Database\Eloquent\Builder")) {
|
||||
@ -79,10 +79,10 @@ class BaseController extends Controller
|
||||
|
||||
$paginator = $query->paginate($limit);
|
||||
$query = $paginator->getCollection();
|
||||
$resource = new Collection($query, $transformer, $entityType);
|
||||
$resource = new Collection($query, $transformer, $entity_type);
|
||||
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||
} else {
|
||||
$resource = new Collection($query, $transformer, $entityType);
|
||||
$resource = new Collection($query, $transformer, $entity_type);
|
||||
}
|
||||
|
||||
return $this->manager->createData($resource)->toArray();
|
||||
@ -115,20 +115,20 @@ class BaseController extends Controller
|
||||
protected function itemResponse($item)
|
||||
{
|
||||
|
||||
$transformer = new $this->entityTransformer(Input::get('serializer'));
|
||||
$transformer = new $this->entity_transformer(Input::get('serializer'));
|
||||
|
||||
$data = $this->createItem($item, $transformer, $this->entityType);
|
||||
$data = $this->createItem($item, $transformer, $this->entity_type);
|
||||
|
||||
return $this->response($data);
|
||||
}
|
||||
|
||||
protected function createItem($data, $transformer, $entityType)
|
||||
protected function createItem($data, $transformer, $entity_type)
|
||||
{
|
||||
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
|
||||
$entityType = null;
|
||||
$entity_type = null;
|
||||
}
|
||||
|
||||
$resource = new Item($data, $transformer, $entityType);
|
||||
$resource = new Item($data, $transformer, $entity_type);
|
||||
|
||||
return $this->manager->createData($resource)->toArray();
|
||||
}
|
||||
|
@ -32,9 +32,9 @@ class ClientController extends BaseController
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
protected $entityType = Client::class;
|
||||
protected $entity_type = Client::class;
|
||||
|
||||
protected $entityTransformer = ClientTransformer::class;
|
||||
protected $entity_transformer = ClientTransformer::class;
|
||||
|
||||
/**
|
||||
* @var ClientRepository
|
||||
@ -102,7 +102,7 @@ class ClientController extends BaseController
|
||||
|
||||
$client = UpdateClient::dispatchNow($request, $client);
|
||||
|
||||
return response()->json($client, 200);
|
||||
return $this->itemResponse($client);
|
||||
|
||||
}
|
||||
|
||||
@ -115,13 +115,8 @@ class ClientController extends BaseController
|
||||
{
|
||||
$client = ClientFactory::create(auth()->user()->company()->id, auth()->user()->id);
|
||||
|
||||
$data = [
|
||||
'client' => $client,
|
||||
'hashed_id' => '',
|
||||
'countries' => Country::all()
|
||||
];
|
||||
return $this->itemResponse($client);
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,12 +129,9 @@ class ClientController extends BaseController
|
||||
{
|
||||
|
||||
$client = StoreClient::dispatchNow($request, ClientFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
||||
|
||||
$client->load('contacts', 'primary_contact');
|
||||
|
||||
$client->hashed_id = $this->encodePrimarykey($client->id);
|
||||
|
||||
return response()->json($client, 200);
|
||||
return $this->itemResponse($client);
|
||||
|
||||
}
|
||||
|
||||
@ -178,7 +170,7 @@ class ClientController extends BaseController
|
||||
});
|
||||
|
||||
//todo need to return the updated dataset
|
||||
return response()->json('success', 200);
|
||||
return response()->json([], 200);
|
||||
|
||||
}
|
||||
|
||||
|
@ -2,10 +2,14 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Factory\ProductFactory;
|
||||
use App\Filters\ProductFilters;
|
||||
use App\Http\Requests\Product\ShowProductRequest;
|
||||
use App\Http\Requests\Product\CreateProductRequest;
|
||||
use App\Http\Requests\Product\EditProductRequest;
|
||||
use App\Http\Requests\Product\ShowProductRequest;
|
||||
use App\Http\Requests\Product\StoreProductRequest;
|
||||
use App\Models\Product;
|
||||
use App\Repositories\ProductRepository;
|
||||
use App\Transformers\ProductTransformer;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Request;
|
||||
@ -15,18 +19,21 @@ class ProductController extends BaseController
|
||||
|
||||
use MakesHash;
|
||||
|
||||
protected $entityType = Product::class;
|
||||
protected $entity_type = Product::class;
|
||||
|
||||
protected $entityTransformer = ProductTransformer::class;
|
||||
protected $entity_transformer = ProductTransformer::class;
|
||||
|
||||
protected $product_repo;
|
||||
|
||||
/**
|
||||
* ProductController constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(ProductRepository $product_repo)
|
||||
{
|
||||
|
||||
parent::__construct();
|
||||
|
||||
$this->product_repo = $product_repo;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -46,9 +53,11 @@ class ProductController extends BaseController
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
public function create(CreateProductRequest $request)
|
||||
{
|
||||
//
|
||||
$product = ProductFactory::create(auth()->user()->company()->id, auth()->user()->id);
|
||||
|
||||
return $this->itemResponse($product);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -57,9 +66,11 @@ class ProductController extends BaseController
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function store(StoreProductRequest $request)
|
||||
{
|
||||
//
|
||||
$product = $this->product_repo->save($request, ProductFactory::create(auth()->user()->company()->id, auth()->user()->id));
|
||||
|
||||
return $this->itemResponse($product);
|
||||
}
|
||||
|
||||
/**
|
||||
|
28
app/Http/Requests/Product/CreateProductRequest.php
Normal file
28
app/Http/Requests/Product/CreateProductRequest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Product;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Models\Product;
|
||||
|
||||
class CreateProductRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->can('create', Product::Class);
|
||||
}
|
||||
|
||||
public function rules() : array
|
||||
{
|
||||
return [
|
||||
'product_key' => 'required',
|
||||
]
|
||||
}
|
||||
|
||||
}
|
@ -24,7 +24,7 @@ class EditProductRequest extends Request
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
//
|
||||
'product_key' => 'required',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
29
app/Http/Requests/Product/StoreProductRequest.php
Normal file
29
app/Http/Requests/Product/StoreProductRequest.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Product;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Models\Product;
|
||||
|
||||
class StoreProductRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->can('create', Product::class);
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'product_key' => 'required|unique:products,product_key,null,null,company_id,'.auth()->user()->companyId(),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -14,8 +14,12 @@ class Product extends BaseModel
|
||||
use Filterable;
|
||||
|
||||
protected $guarded = [
|
||||
'id',
|
||||
];
|
||||
'id',
|
||||
'updated_at',
|
||||
'created_at',
|
||||
'deleted_at',
|
||||
'q',
|
||||
];
|
||||
|
||||
public function company()
|
||||
{
|
||||
|
27
app/Repositories/ProductRepository.php
Normal file
27
app/Repositories/ProductRepository.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Product;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class ProductRepository extends BaseRepository
|
||||
{
|
||||
|
||||
public function getClassName()
|
||||
{
|
||||
return Product::class;
|
||||
}
|
||||
|
||||
public function save(Request $request, Product $product) : ?Product
|
||||
{
|
||||
$product->fill($request->input());
|
||||
$product->save();
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
}
|
@ -11,13 +11,10 @@ Route::post('logout', 'Auth\LoginController@logout')->name('logout');
|
||||
* Signup Routes
|
||||
*/
|
||||
|
||||
//Route::redirect('/', '/login', 301);
|
||||
//Route::get('signup', 'AccountController@index')->name('signup');
|
||||
//Route::post('signup', 'AccountController@store')->name('signup.submit');
|
||||
|
||||
|
||||
Auth::routes(['register' => false]);
|
||||
|
||||
Route::redirect('/', '/login', 301);
|
||||
Route::get('signup', 'AccountController@index')->name('signup');
|
||||
Route::post('signup', 'AccountController@store')->name('signup.submit');
|
||||
s
|
||||
Route::get('contact/login', 'Auth\ContactLoginController@showLoginForm')->name('contact.login');
|
||||
Route::post('contact/login', 'Auth\ContactLoginController@login')->name('contact.login.submit');
|
||||
|
||||
|
@ -154,9 +154,7 @@ class ClientTest extends TestCase
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $token,
|
||||
])->put('/api/v1/clients/'.$this->encodePrimaryKey($client->id), $client_update)
|
||||
->assertJson([
|
||||
'name' => 'A Funky Name'
|
||||
]);
|
||||
->assertStatus(200);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
@ -170,9 +168,7 @@ class ClientTest extends TestCase
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $token,
|
||||
])->post('/api/v1/clients/', ['name' => 'New Client'])
|
||||
->assertJson([
|
||||
'name' => 'New Client'
|
||||
]);
|
||||
->assertStatus(200);
|
||||
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
Loading…
x
Reference in New Issue
Block a user