1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Removed extra entity load in the repos save function

This commit is contained in:
Hillel Coren 2016-05-02 20:42:13 +03:00
parent 325cac1603
commit e6bcdb36b3
23 changed files with 65 additions and 72 deletions

View File

@ -24,28 +24,4 @@ class BaseController extends Controller
$this->layout = View::make($this->layout);
}
}
protected function authorizeCreate() {
$this->authorize('create', $this->entityType);
}
/*
protected function authorizeUpdate($entity) {
$this->authorize('edit', $entity);
}
*/
protected function authorizeUpdate($input){
$creating = empty($input['public_id']) || $input['public_id'] == '-1';
if($creating){
$this->authorize('create', $this->entityType);
}
else{
$className = Utils::getEntityName($this->entityType);
$object = call_user_func(array("App\\Models\\{$className}", 'scope'), $input['public_id'])->firstOrFail();
$this->authorize('edit', $object);
}
}
}

View File

@ -116,7 +116,7 @@ class ClientApiController extends BaseAPIController
$data = $request->input();
$data['public_id'] = $publicId;
$client = $this->clientRepo->save($data);
$client = $this->clientRepo->save($data, $request->entity());
return $this->itemResponse($client);
}

View File

@ -146,7 +146,7 @@ class ExpenseController extends BaseController
$data = $request->input();
$data['documents'] = $request->file('documents');
$expense = $this->expenseService->save($data);
$expense = $this->expenseService->save($data, $request->entity());
Session::flash('message', trans('texts.updated_expense'));

View File

@ -318,7 +318,7 @@ class InvoiceApiController extends BaseAPIController
$data = $request->input();
$data['public_id'] = $publicId;
$this->invoiceService->save($data);
$this->invoiceService->save($data, $request->entity());
$invoice = Invoice::scope($publicId)
->with('client', 'invoice_items', 'invitations')

View File

@ -396,9 +396,7 @@ class InvoiceController extends BaseController
{
$data = $request->input();
$data['documents'] = $request->file('documents');
$this->authorizeUpdate($data);
$action = Input::get('action');
$entityType = Input::get('entityType');
@ -435,13 +433,11 @@ class InvoiceController extends BaseController
{
$data = $request->input();
$data['documents'] = $request->file('documents');
$this->authorizeUpdate($data);
$action = Input::get('action');
$entityType = Input::get('entityType');
$invoice = $this->invoiceService->save($data);
$invoice = $this->invoiceService->save($data, $request->entity());
$entityType = $invoice->getEntityType();
$message = trans("texts.updated_{$entityType}");
Session::flash('message', $message);

View File

@ -85,7 +85,7 @@ class PaymentApiController extends BaseAPIController
$data = $request->input();
$data['public_id'] = $publicId;
$payment = $this->paymentRepo->save($data);
$payment = $this->paymentRepo->save($data, $request->entity());
return $this->itemResponse($payment);
}

View File

@ -603,7 +603,7 @@ class PaymentController extends BaseController
public function update(UpdatePaymentRequest $request)
{
$payment = $this->paymentRepo->save($request->input());
$payment = $this->paymentRepo->save($request->input(), $request->entity());
Session::flash('message', trans('texts.updated_payment'));

View File

@ -42,7 +42,7 @@ class ProductApiController extends BaseAPIController
$data = $request->input();
$data['public_id'] = $publicId;
$product = $this->productRepo->save($data);
$product = $this->productRepo->save($data, $request->entity());
return $this->itemResponse($product);
}

View File

@ -42,7 +42,7 @@ class TaxRateApiController extends BaseAPIController
$data = $request->input();
$data['public_id'] = $publicId;
$taxRate = $this->taxRateRepo->save($data);
$taxRate = $this->taxRateRepo->save($data, $request->entity());
return $this->itemResponse($taxRate);
}

View File

@ -75,9 +75,7 @@ class TaxRateController extends BaseController
public function update(UpdateTaxRateRequest $request, $publicId)
{
$taxRate = TaxRate::scope($publicId)->firstOrFail();
$this->taxRateRepo->save($request->input(), $taxRate);
$this->taxRateRepo->save($request->input(), $request->entity());
Session::flash('message', trans('texts.updated_tax_rate'));
return Redirect::to('settings/' . ACCOUNT_TAX_RATES);

View File

@ -182,7 +182,7 @@ class VendorController extends BaseController
*/
public function update(UpdateVendorRequest $request)
{
$vendor = $this->vendorService->save($request->input());
$vendor = $this->vendorService->save($request->input(), $request->entity());
Session::flash('message', trans('texts.updated_vendor'));

View File

@ -72,12 +72,13 @@ class ClientRepository extends BaseRepository
if ($client) {
// do nothing
} if (!$publicId || $publicId == '-1') {
} elseif (!$publicId || $publicId == '-1') {
$client = Client::createNew();
} else {
$client = Client::scope($publicId)->with('contacts')->firstOrFail();
\Log::warning('Entity not set in client repo save');
}
// convert currency code to id
if (isset($data['currency_code'])) {
$currencyCode = strtolower($data['currency_code']);

View File

@ -59,12 +59,15 @@ class CreditRepository extends BaseRepository
return $query;
}
public function save($input)
public function save($input, $credit = null)
{
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
if ($publicId) {
if ($credit) {
// do nothing
} elseif ($publicId) {
$credit = Credit::scope($publicId)->firstOrFail();
\Log::warning('Entity not set in credit repo save');
} else {
$credit = Credit::createNew();
}

View File

@ -122,12 +122,15 @@ class ExpenseRepository extends BaseRepository
return $query;
}
public function save($input)
public function save($input, $expense = null)
{
$publicId = isset($input['public_id']) ? $input['public_id'] : false;
if ($publicId) {
if ($expense) {
// do nothing
} elseif ($publicId) {
$expense = Expense::scope($publicId)->firstOrFail();
\Log::warning('Entity not set in expense repo save');
} else {
$expense = Expense::createNew();
}

View File

@ -201,14 +201,16 @@ class InvoiceRepository extends BaseRepository
->make();
}
public function save($data)
public function save($data, $invoice = null)
{
$account = \Auth::user()->account;
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
$isNew = !$publicId || $publicId == '-1';
if ($isNew) {
if ($invoice) {
// do nothing
} elseif ($isNew) {
$entityType = ENTITY_INVOICE;
if (isset($data['is_recurring']) && filter_var($data['is_recurring'], FILTER_VALIDATE_BOOLEAN)) {
$entityType = ENTITY_RECURRING_INVOICE;
@ -224,6 +226,7 @@ class InvoiceRepository extends BaseRepository
}
} else {
$invoice = Invoice::scope($publicId)->firstOrFail();
\Log::warning('Entity not set in invoice repo save');
}
$invoice->fill($data);

View File

@ -123,12 +123,15 @@ class PaymentRepository extends BaseRepository
return $query;
}
public function save($input)
public function save($input, $payment = null)
{
$publicId = isset($input['public_id']) ? $input['public_id'] : false;
if ($publicId) {
if ($payment) {
// do nothing
} elseif ($publicId) {
$payment = Payment::scope($publicId)->firstOrFail();
\Log::warning('Entity not set in payment repo save');
} else {
$payment = Payment::createNew();
}

View File

@ -31,12 +31,15 @@ class ProductRepository extends BaseRepository
);
}
public function save($data)
public function save($data, $product = null)
{
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
if ($publicId) {
if ($product) {
// do nothing
} elseif ($publicId) {
$product = Product::scope($publicId)->firstOrFail();
\Log::warning('Entity not set in product repo save');
} else {
$product = Product::createNew();
}

View File

@ -64,10 +64,13 @@ class TaskRepository
return $query;
}
public function save($publicId, $data)
public function save($publicId, $data, $task = null)
{
if ($publicId) {
if ($task) {
// do nothing
} elseif ($publicId) {
$task = Task::scope($publicId)->firstOrFail();
\Log::warning('Entity not set in task repo save');
} else {
$task = Task::createNew();
}

View File

@ -20,14 +20,15 @@ class TaxRateRepository extends BaseRepository
->select('tax_rates.public_id', 'tax_rates.name', 'tax_rates.rate', 'tax_rates.deleted_at');
}
public function save($data, $taxRate = false)
public function save($data, $taxRate = null)
{
if ( ! $taxRate) {
if (isset($data['public_id'])) {
$taxRate = TaxRate::scope($data['public_id'])->firstOrFail();
} else {
$taxRate = TaxRate::createNew();
}
if ($taxRate) {
// do nothing
} elseif (isset($data['public_id'])) {
$taxRate = TaxRate::scope($data['public_id'])->firstOrFail();
\Log::warning('Entity not set in tax rate repo save');
} else {
$taxRate = TaxRate::createNew();
}
$taxRate->fill($data);

View File

@ -62,14 +62,17 @@ class VendorRepository extends BaseRepository
return $query;
}
public function save($data)
public function save($data, $vendor = null)
{
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
if (!$publicId || $publicId == '-1') {
if ($vendor) {
// do nothing
} elseif (!$publicId || $publicId == '-1') {
$vendor = Vendor::createNew();
} else {
$vendor = Vendor::scope($publicId)->with('vendorcontacts')->firstOrFail();
\Log::warning('Entity not set in vendor repo save');
}
$vendor->fill($data);

View File

@ -28,7 +28,7 @@ class ExpenseService extends BaseService
return $this->expenseRepo;
}
public function save($data)
public function save($data, $expense = null)
{
if (isset($data['client_id']) && $data['client_id']) {
$data['client_id'] = Client::getPrivateId($data['client_id']);
@ -38,7 +38,7 @@ class ExpenseService extends BaseService
$data['vendor_id'] = Vendor::getPrivateId($data['vendor_id']);
}
return $this->expenseRepo->save($data);
return $this->expenseRepo->save($data, $expense);
}
public function getDatatable($search)

View File

@ -30,7 +30,7 @@ class InvoiceService extends BaseService
return $this->invoiceRepo;
}
public function save($data)
public function save($data, $invoice = null)
{
if (isset($data['client'])) {
$canSaveClient = false;
@ -46,7 +46,7 @@ class InvoiceService extends BaseService
}
}
$invoice = $this->invoiceRepo->save($data);
$invoice = $this->invoiceRepo->save($data, $invoice);
$client = $invoice->client;
$client->load('contacts');

View File

@ -26,13 +26,13 @@ class VendorService extends BaseService
return $this->vendorRepo;
}
public function save($data)
public function save($data, $vendor = null)
{
if (Auth::user()->account->isNinjaAccount() && isset($data['plan'])) {
$this->ninjaRepo->updatePlanDetails($data['public_id'], $data);
}
return $this->vendorRepo->save($data);
return $this->vendorRepo->save($data, $vendor);
}
public function getDatatable($search)