66
77use Dingo \Api \Exception \StoreResourceFailedException ;
88
9- use Pterodactyl \Transformers \UserTransformer ;
109use Pterodactyl \Models ;
10+ use Pterodactyl \Transformers \UserTransformer ;
1111use Pterodactyl \Repositories \UserRepository ;
12+ use Pterodactyl \Exceptions \DisplayValidationException ;
13+ use Pterodactyl \Exceptions \DisplayException ;
1214
1315/**
14- * @Resource("Users", uri="/users" )
16+ * @Resource("Users")
1517 */
1618class UserController extends BaseController
1719{
@@ -21,7 +23,7 @@ class UserController extends BaseController
2123 *
2224 * Lists all users currently on the system.
2325 *
24- * @Get("/{?page}")
26+ * @Get("/users/ {?page}")
2527 * @Versions({"v1"})
2628 * @Parameters({
2729 * @Parameter("page", type="integer", description="The page of results to view.", default=1)
@@ -39,15 +41,15 @@ public function getUsers(Request $request)
3941 *
4042 * Lists specific fields about a user or all fields pertaining to that user.
4143 *
42- * @Get("/{id}/{fields}")
44+ * @Get("/users/ {id}/{fields}")
4345 * @Versions({"v1"})
4446 * @Parameters({
4547 * @Parameter("id", type="integer", required=true, description="The ID of the user to get information on."),
4648 * @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.")
4749 * })
4850 * @Response(200)
4951 */
50- public function getUserByID (Request $ request , $ id , $ fields = null )
52+ public function getUser (Request $ request , $ id , $ fields = null )
5153 {
5254 $ query = Models \User::where ('id ' , $ id );
5355
@@ -65,34 +67,38 @@ public function getUserByID(Request $request, $id, $fields = null)
6567 /**
6668 * Create a New User
6769 *
68- * @Post("/")
70+ * @Post("/users ")
6971 * @Versions({"v1"})
7072 * @Transaction({
7173 * @Request({
7274 * "email": "foo@example.com",
7375 * "password": "foopassword",
7476 * "admin": false
7577 * }, headers={"Authorization": "Bearer <jwt-token>"}),
76- * @Response(200, body={"id": 1} ),
77- * @Response(422, body{
78+ * @Response(201 ),
79+ * @Response(422, body= {
7880 * "message": "A validation error occured.",
7981 * "errors": {
80- * "email": [ "The email field is required."] ,
81- * "password": [ "The password field is required."] ,
82- * "admin": [ "The admin field is required."]
82+ * "email": { "The email field is required."} ,
83+ * "password": { "The password field is required."} ,
84+ * "admin": { "The admin field is required."}
8385 * },
8486 * "status_code": 422
8587 * })
8688 * })
8789 */
88- public function postUsers (Request $ request )
90+ public function postUser (Request $ request )
8991 {
9092 try {
9193 $ user = new UserRepository ;
9294 $ create = $ user ->create ($ request ->input ('email ' ), $ request ->input ('password ' ), $ request ->input ('admin ' ));
93- return [ 'id ' => $ create ];
94- } catch (\Pterodactyl \Exceptions \DisplayValidationException $ ex ) {
95+ return $ this ->response ->created (route ('api.users.view ' , [
96+ 'id ' => $ create
97+ ]));
98+ } catch (DisplayValidationException $ ex ) {
9599 throw new StoreResourceFailedException ('A validation error occured. ' , json_decode ($ ex ->getMessage (), true ));
100+ } catch (DisplayException $ ex ) {
101+ throw new StoreResourceFailedException ($ ex ->getMessage ());
96102 } catch (\Exception $ ex ) {
97103 throw new StoreResourceFailedException ('Unable to create a user on the system due to an error. ' );
98104 }
@@ -103,7 +109,7 @@ public function postUsers(Request $request)
103109 *
104110 * The data sent in the request will be used to update the existing user on the system.
105111 *
106- * @Patch("/{id}")
112+ * @Patch("/users/ {id}")
107113 * @Versions({"v1"})
108114 * @Transaction({
109115 * @Request({
@@ -118,13 +124,23 @@ public function postUsers(Request $request)
118124 */
119125 public function patchUser (Request $ request , $ id )
120126 {
121- //
127+ try {
128+ $ user = new UserRepository ;
129+ $ user ->update ($ id , $ request ->all ());
130+ return Models \User::findOrFail ($ id );
131+ } catch (DisplayValidationException $ ex ) {
132+ throw new StoreResourceFailedException ('A validation error occured. ' , json_decode ($ ex ->getMessage (), true ));
133+ } catch (DisplayException $ ex ) {
134+ throw new StoreResourceFailedException ($ ex ->getMessage ());
135+ } catch (\Exception $ ex ) {
136+ throw new StoreResourceFailedException ('Unable to create a user on the system due to an error. ' );
137+ }
122138 }
123139
124140 /**
125141 * Delete a User
126142 *
127- * @Delete("/{id}")
143+ * @Delete("/users/ {id}")
128144 * @Versions({"v1"})
129145 * @Transaction({
130146 * @Request(headers={"Authorization": "Bearer <jwt-token>"}),
@@ -137,7 +153,15 @@ public function patchUser(Request $request, $id)
137153 */
138154 public function deleteUser (Request $ request , $ id )
139155 {
140- //
156+ try {
157+ $ user = new UserRepository ;
158+ $ user ->delete ($ id );
159+ return $ this ->response ->noContent ();
160+ } catch (DisplayException $ ex ) {
161+ throw new StoreResourceFailedException ($ ex ->getMessage ());
162+ } catch (\Exception $ ex ) {
163+ throw new StoreResourceFailedException ('Unable to delete this user due to an error. ' );
164+ }
141165 }
142166
143167}
0 commit comments