Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Menu
Open sidebar
Can
CurrencyManager
Commits
b1678f7c
Commit
b1678f7c
authored
Nov 24, 2018
by
Can
Browse files
controller converted to trait
parent
99d40498
Pipeline
#37783676
passed with stages
in 2 minutes and 20 seconds
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
src/CurrencyManager/Controllers/CurrencyController.php
View file @
b1678f7c
...
...
@@ -12,174 +12,47 @@ use CurrencyManager\Forms\UpdateParams;
use
CurrencyManager\Repositories\CurrencyRepository
;
use
Illuminate\Http\Request
;
use
Illuminate\Support\Facades\Redirect
;
use
CurrencyManager\Traits\CurrencyControllerAsTrait
;
class
CurrencyController
extends
Controller
{
private
$currencyRepository
;
private
$currencyCrudForm
;
private
$currencyCrudFormQuickBuilder
;
private
$currencyHelper
;
private
$editBladePath
=
''
;
private
$indexBladePath
=
''
;
private
$indexRedirectRoute
=
''
;
use
CurrencyControllerAsTrait
;
public
function
__construct
()
{
$this
->
currencyRepository
=
$this
->
newInstanceCurrencyRepository
();
$this
->
currencyCrudForm
=
$this
->
newInstanceCurrencyCrudForm
();
$this
->
currencyCrudFormQuickBuilder
=
$this
->
newInstanceCurrencyCrudFormFactory
();
$this
->
currencyHelper
=
$this
->
newCurrencyHelperInstance
();
$this
->
indexRedirectRoute
=
$this
->
indexRedirectRoute
();
$this
->
editBladePath
=
$this
->
getEditBladePath
();
$this
->
indexBladePath
=
$this
->
getIndexBladePath
();
$this
->
__constructTrait
();
}
protected
function
getEditBladePath
()
{
return
'CurrencyManager::editWrapper'
;
}
protected
function
getIndexBladePath
()
{
return
'CurrencyManager::index'
;
}
protected
function
indexRedirectRoute
()
{
return
route
(
'currency.index'
);
}
protected
function
newInstanceCurrencyRepository
()
:
CurrencyRepository
{
$currencyRepository
=
CurrencyRepositoryFactory
::
makeForCrud
();
return
$currencyRepository
;
}
protected
function
newInstanceCurrencyCrudForm
()
:
CurrencyCrudForm
{
return
new
CurrencyCrudForm
();
}
protected
function
newInstanceCurrencyCrudFormFactory
()
:
CurrencyCrudFormFactory
{
return
new
CurrencyCrudFormFactory
();
}
protected
function
newCurrencyHelperInstance
()
{
return
CurrencyHelperFactory
::
make
();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public
function
index
()
{
$data
=
[
'currencies'
=>
$this
->
currencyRepository
->
all
(),
'currentCurrencyId'
=>
null
,
];
$currentCurrencyInstance
=
$this
->
currencyHelper
->
getCurrencyOrDefault
();
$hasCurrencyHelperHasError
=
$this
->
currencyHelper
->
errorThrower
->
hasError
;
if
(
!
$hasCurrencyHelperHasError
)
{
$data
[
'currentCurrencyId'
]
=
$currentCurrencyInstance
->
id
;
}
$data
[
'errors'
]
=
$this
->
currencyHelper
->
errorThrower
->
getErrors
();
return
view
(
$this
->
indexBladePath
)
->
with
(
$data
);
return
$this
->
indexTraitMethod
();
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public
function
create
()
{
$this
->
currencyCrudForm
->
setFormParams
(
new
StoreParams
());
$data
=
$this
->
currencyCrudFormQuickBuilder
->
build
(
$this
->
currencyCrudForm
);
return
view
(
$this
->
editBladePath
)
->
with
(
$data
);
return
$this
->
createTraitMethod
();
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public
function
store
(
Request
$request
)
{
$this
->
currencyRepository
->
updateOrStore
(
$request
);
return
Redirect
::
to
(
$this
->
indexRedirectRoute
);
}
/**
* Display the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public
function
show
(
$id
)
{
//
return
$this
->
storeTraitMethod
(
$request
);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public
function
edit
(
$id
)
{
$currency
=
$this
->
currencyRepository
->
getById
(
$id
);
$this
->
currencyCrudForm
->
setCurrencyInstance
(
$currency
);
$this
->
currencyCrudForm
->
setFormParams
(
new
UpdateParams
([
'id'
=>
$id
]));
$data
=
$this
->
currencyCrudFormQuickBuilder
->
build
(
$this
->
currencyCrudForm
);
return
view
(
$this
->
editBladePath
)
->
with
(
$data
);
return
$this
->
editTraitMethod
(
$id
);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public
function
update
(
Request
$request
,
$id
)
public
function
update
(
Request
$request
,
$id
)
{
$this
->
currencyRepository
->
updateOrStore
(
$request
,
$id
);
return
Redirect
::
to
(
$this
->
indexRedirectRoute
);
$id
=
$request
->
get
(
"id"
);
return
$this
->
updateTraitMethod
(
$request
,
$id
);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public
function
destroy
(
$id
)
{
$this
->
currencyRepository
->
delete
(
$id
);
return
Redirect
::
to
(
$this
->
indexRedirectRoute
);
return
$this
->
destroyTraitMethod
(
$id
);
}
}
src/CurrencyManager/Traits/CurrencyControllerAsTrait.php
0 → 100644
View file @
b1678f7c
<?php
/**
* Created by PhpStorm.
* User: dd
* Date: 24.11.2018
* Time: 22:03
*/
namespace
CurrencyManager\Traits
;
use
CurrencyManager\Factories\CurrencyCrudFormFactory
;
use
CurrencyManager\Factories\CurrencyHelperFactory
;
use
CurrencyManager\Factories\CurrencyRepositoryFactory
;
use
CurrencyManager\Forms\CurrencyCrudForm
;
use
CurrencyManager\Forms\StoreParams
;
use
CurrencyManager\Forms\UpdateParams
;
use
CurrencyManager\Repositories\CurrencyRepository
;
use
Illuminate\Http\Request
;
use
Illuminate\Support\Facades\Redirect
;
trait
CurrencyControllerAsTrait
{
private
$currencyRepository
;
private
$currencyCrudForm
;
private
$currencyCrudFormQuickBuilder
;
private
$currencyHelper
;
private
$editBladePath
=
''
;
private
$indexBladePath
=
''
;
private
$indexRedirectRoute
=
''
;
public
function
__constructTrait
()
{
$this
->
currencyRepository
=
$this
->
newInstanceCurrencyRepository
();
$this
->
currencyCrudForm
=
$this
->
newInstanceCurrencyCrudForm
();
$this
->
currencyCrudFormQuickBuilder
=
$this
->
newInstanceCurrencyCrudFormFactory
();
$this
->
currencyHelper
=
$this
->
newCurrencyHelperInstance
();
$this
->
indexRedirectRoute
=
$this
->
indexRedirectRoute
();
$this
->
editBladePath
=
$this
->
getEditBladePath
();
$this
->
indexBladePath
=
$this
->
getIndexBladePath
();
}
protected
function
getEditBladePath
()
{
return
'CurrencyManager::editWrapper'
;
}
protected
function
getIndexBladePath
()
{
return
'CurrencyManager::index'
;
}
protected
function
indexRedirectRoute
()
{
return
route
(
'currency.index'
);
}
protected
function
newInstanceCurrencyRepository
()
:
CurrencyRepository
{
$currencyRepository
=
CurrencyRepositoryFactory
::
makeForCrud
();
return
$currencyRepository
;
}
protected
function
newInstanceCurrencyCrudForm
()
:
CurrencyCrudForm
{
return
new
CurrencyCrudForm
();
}
protected
function
newInstanceCurrencyCrudFormFactory
()
:
CurrencyCrudFormFactory
{
return
new
CurrencyCrudFormFactory
();
}
protected
function
newCurrencyHelperInstance
()
{
return
CurrencyHelperFactory
::
make
();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public
function
indexTraitMethod
()
{
$data
=
[
'currencies'
=>
$this
->
currencyRepository
->
all
(),
'currentCurrencyId'
=>
null
,
];
$currentCurrencyInstance
=
$this
->
currencyHelper
->
getCurrencyOrDefault
();
$hasCurrencyHelperHasError
=
$this
->
currencyHelper
->
errorThrower
->
hasError
;
if
(
!
$hasCurrencyHelperHasError
)
{
$data
[
'currentCurrencyId'
]
=
$currentCurrencyInstance
->
id
;
}
$data
[
'errors'
]
=
$this
->
currencyHelper
->
errorThrower
->
getErrors
();
return
view
(
$this
->
indexBladePath
)
->
with
(
$data
);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public
function
createTraitMethod
()
{
$this
->
currencyCrudForm
->
setFormParams
(
new
StoreParams
());
$data
=
$this
->
currencyCrudFormQuickBuilder
->
build
(
$this
->
currencyCrudForm
);
return
view
(
$this
->
editBladePath
)
->
with
(
$data
);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public
function
storeTraitMethod
(
Request
$request
)
{
$this
->
currencyRepository
->
updateOrStore
(
$request
);
return
Redirect
::
to
(
$this
->
indexRedirectRoute
);
}
/**
* Display the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public
function
showTraitMethod
(
$id
)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public
function
editTraitMethod
(
$id
)
{
$currency
=
$this
->
currencyRepository
->
getById
(
$id
);
$this
->
currencyCrudForm
->
setCurrencyInstance
(
$currency
);
$this
->
currencyCrudForm
->
setFormParams
(
new
UpdateParams
([
'id'
=>
$id
]));
$data
=
$this
->
currencyCrudFormQuickBuilder
->
build
(
$this
->
currencyCrudForm
);
return
view
(
$this
->
editBladePath
)
->
with
(
$data
);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public
function
updateTraitMethod
(
Request
$request
,
$id
)
{
$this
->
currencyRepository
->
updateOrStore
(
$request
,
$id
);
return
Redirect
::
to
(
$this
->
indexRedirectRoute
);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public
function
destroyTraitMethod
(
$id
)
{
$this
->
currencyRepository
->
delete
(
$id
);
return
Redirect
::
to
(
$this
->
indexRedirectRoute
);
}
}
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment