22
33namespace App \Actions \Sharing ;
44
5- use App \DTO \Shares ;
65use App \Exceptions \Internal \QueryBuilderException ;
6+ use App \Http \Resources \Sharing \SharesResource ;
77use App \Models \Extensions \BaseAlbum ;
88use App \Models \User ;
99use Illuminate \Support \Collection ;
@@ -21,16 +21,16 @@ class ListShare
2121 * which are shared
2222 * @param BaseAlbum|null $baseAlbum the optional album which is shared
2323 *
24- * @return Shares
24+ * @return SharesResource
2525 *
2626 * @throws QueryBuilderException
2727 */
28- public function do (?User $ participant , ?User $ owner , ?BaseAlbum $ baseAlbum ): Shares
28+ public function do (?User $ participant , ?User $ owner , ?BaseAlbum $ baseAlbum ): SharesResource
2929 {
3030 try {
3131 // Active shares, optionally filtered by album ID, participant ID
3232 // and or owner ID
33- $ shared_query = DB ::table ('user_base_album ' )
33+ $ shared = DB ::table ('user_base_album ' )
3434 ->select ([
3535 'user_base_album.id ' ,
3636 'user_id ' ,
@@ -39,34 +39,23 @@ public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): Sha
3939 'title ' ,
4040 ])
4141 ->join ('users ' , 'user_id ' , '= ' , 'users.id ' )
42- ->join ('base_albums ' , 'base_album_id ' , '= ' , 'base_albums.id ' );
43- if ($ participant !== null ) {
44- $ shared_query ->where ('user_base_album.user_id ' , '= ' , $ participant ->id );
45- }
46- if ($ owner !== null ) {
47- $ shared_query ->where ('base_albums.owner_id ' , '= ' , $ owner ->id );
48- }
49- if ($ baseAlbum !== null ) {
50- $ shared_query ->where ('base_albums.id ' , '= ' , $ baseAlbum ->id );
51- }
52- $ shared = $ shared_query
42+ ->join ('base_albums ' , 'base_album_id ' , '= ' , 'base_albums.id ' )
43+ ->when ($ participant !== null , fn ($ q ) => $ q ->where ('user_base_album.user_id ' , '= ' , $ participant ->id ))
44+ ->when ($ owner !== null , fn ($ q ) => $ q ->where ('base_albums.owner_id ' , '= ' , $ owner ->id ))
45+ ->when ($ baseAlbum !== null , fn ($ q ) => $ q ->where ('base_albums.id ' , '= ' , $ baseAlbum ->id ))
5346 ->orderBy ('title ' , 'ASC ' )
5447 ->orderBy ('username ' , 'ASC ' )
5548 ->get ();
5649
5750 // Existing albums which can be shared optionally filtered by
5851 // album ID and/or owner ID
59- $ albums_query = DB ::table ('base_albums ' )
52+ $ albums = DB ::table ('base_albums ' )
6053 ->leftJoin ('albums ' , 'albums.id ' , '= ' , 'base_albums.id ' )
6154 ->select (['base_albums.id ' , 'title ' , 'parent_id ' ])
62- ->orderBy ('title ' , 'ASC ' );
63- if ($ owner !== null ) {
64- $ albums_query ->where ('owner_id ' , '= ' , $ owner ->id );
65- }
66- if ($ baseAlbum !== null ) {
67- $ albums_query ->where ('base_albums.id ' , '= ' , $ baseAlbum ->id );
68- }
69- $ albums = $ albums_query ->get ();
55+ ->when ($ owner !== null , fn ($ q ) => $ q ->where ('owner_id ' , '= ' , $ owner ->id ))
56+ ->when ($ baseAlbum !== null , fn ($ q ) => $ q ->where ('base_albums.id ' , '= ' , $ baseAlbum ->id ))
57+ ->orderBy ('title ' , 'ASC ' )
58+ ->get ();
7059 $ this ->linkAlbums ($ albums );
7160 $ albums ->each (function ($ album ) {
7261 $ album ->title = $ this ->breadcrumbPath ($ album );
@@ -78,19 +67,16 @@ public function do(?User $participant, ?User $owner, ?BaseAlbum $baseAlbum): Sha
7867
7968 // Existing users with whom an album can be shared optionally
8069 // filtered by participant ID
81- $ users_query = DB ::table ('users ' )->select (['id ' , 'username ' ]);
82- if ($ participant !== null ) {
83- $ users_query ->where ('id ' , '= ' , $ participant ->id );
84- } else {
85- $ users_query ->where ('id ' , '> ' , 0 );
86- }
87- $ users = $ users_query ->orderBy ('username ' , 'ASC ' )
70+ $ users = DB ::table ('users ' )->select (['id ' , 'username ' ])
71+ ->when ($ participant !== null , fn ($ q ) => $ q ->where ('id ' , '= ' , $ participant ->id ))
72+ ->when ($ participant === null , fn ($ q ) => $ q ->where ('may_administrate ' , '= ' , false ))
73+ ->orderBy ('username ' , 'ASC ' )
8874 ->get ()
8975 ->each (function ($ user ) {
9076 $ user ->id = intval ($ user ->id );
9177 });
9278
93- return new Shares ($ shared , $ albums , $ users );
79+ return new SharesResource ($ shared , $ albums , $ users );
9480 } catch (\InvalidArgumentException $ e ) {
9581 throw new QueryBuilderException ($ e );
9682 }
0 commit comments