66use App \Http \Controllers \Controller ;
77use App \Http \Resources \EjaculationResource ;
88use App \User ;
9+ use Carbon \CarbonImmutable ;
910use Illuminate \Http \Request ;
1011use Illuminate \Support \Facades \Auth ;
1112use Illuminate \Support \Facades \DB ;
13+ use Illuminate \Validation \Rule ;
1214use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
1315
1416class UserCheckinController extends Controller
@@ -21,8 +23,28 @@ public function index(Request $request, User $user)
2123
2224 $ inputs = $ request ->validate ([
2325 'per_page ' => 'nullable|integer|between:10,100 ' ,
26+ 'order ' => ['nullable ' , Rule::in (['asc ' , 'desc ' ])],
27+ 'since ' => 'nullable|date_format:Y-m-d|after_or_equal:2000-01-01|before_or_equal:2099-12-31 ' ,
28+ 'until ' => 'nullable|date_format:Y-m-d|after_or_equal:2000-01-01|before_or_equal:2099-12-31 ' ,
2429 ]);
2530
31+ if (!empty ($ inputs ['since ' ]) && !empty ($ inputs ['until ' ])) {
32+ $ since = CarbonImmutable::createFromFormat ('Y-m-d ' , $ inputs ['since ' ])->startOfDay ();
33+ $ until = CarbonImmutable::createFromFormat ('Y-m-d ' , $ inputs ['until ' ])->startOfDay ()->addDay ();
34+ if ($ until ->isBefore ($ since )) {
35+ [$ since , $ until ] = [$ until , $ since ];
36+ }
37+ } elseif (!empty ($ inputs ['since ' ])) {
38+ $ since = CarbonImmutable::createFromFormat ('Y-m-d ' , $ inputs ['since ' ])->startOfDay ();
39+ $ until = null ;
40+ } elseif (!empty ($ inputs ['until ' ])) {
41+ $ since = null ;
42+ $ until = CarbonImmutable::createFromFormat ('Y-m-d ' , $ inputs ['until ' ])->startOfDay ()->addDay ();
43+ } else {
44+ $ since = null ;
45+ $ until = null ;
46+ }
47+
2648 $ query = Ejaculation::select (DB ::raw (
2749 <<<'SQL'
2850ejaculations.id,
@@ -32,7 +54,8 @@ public function index(Request $request, User $user)
3254is_too_sensitive,
3355link,
3456source,
35- discard_elapsed_time
57+ discard_elapsed_time,
58+ user_id
3659SQL
3760 ))
3861 ->where ('user_id ' , $ user ->id );
@@ -42,8 +65,15 @@ public function index(Request $request, User $user)
4265 if (!Auth::check () || $ user ->id !== Auth::id ()) {
4366 $ query = $ query ->where ('is_private ' , false );
4467 }
45- $ ejaculations = $ query ->orderBy ('ejaculated_date ' , 'desc ' )
46- ->with ('tags ' )
68+ if ($ since !== null ) {
69+ $ query = $ query ->where ('ejaculated_date ' , '>= ' , $ since );
70+ }
71+ if ($ until !== null ) {
72+ $ query = $ query ->where ('ejaculated_date ' , '< ' , $ until );
73+ }
74+ $ order = ($ inputs ['order ' ] ?? 'desc ' ) === 'asc ' ? 'asc ' : 'desc ' ;
75+ $ ejaculations = $ query ->orderBy ('ejaculated_date ' , $ order )
76+ ->with ('tags ' , 'user ' )
4777 ->withLikes ()
4878 ->withMutedStatus ()
4979 ->paginate ($ inputs ['per_page ' ] ?? 20 );
0 commit comments