2023-06-08 15:10:27 +01:00
< ? php
namespace api\blog ;
2023-06-09 14:50:20 +01:00
require_once __DIR__ . " /../utils/routesInterface.php " ;
require_once " blogData.php " ;
2023-06-08 15:10:27 +01:00
2023-11-14 01:02:27 +00:00
2023-06-09 14:50:20 +01:00
use api\utils\routesInterface ;
use Psr\Http\Message\ResponseInterface as Response ;
use Psr\Http\Message\ServerRequestInterface as Request ;
2023-06-08 15:10:27 +01:00
use Slim\App ;
2023-06-09 14:50:20 +01:00
class blogRoutes implements routesInterface
2023-06-08 15:10:27 +01:00
{
private blogData $blogData ;
2023-10-18 23:58:21 +01:00
2023-06-09 14:50:20 +01:00
/**
* constructor used to instantiate a base blog routes , to be used in the index . php file .
* @ param App $app - the slim app used to create the routes
*/
2023-06-08 15:10:27 +01:00
public function __construct ( App $app )
{
$this -> blogData = new blogData ();
$this -> createRoutes ( $app );
}
2023-06-09 14:50:20 +01:00
/**
* creates the routes for the blog
* @ param App $app - the slim app used to create the routes
* @ return void - returns nothing
*/
2023-06-08 15:10:27 +01:00
public function createRoutes ( App $app ) : void
{
2023-10-18 00:28:34 +01:00
$app -> get ( " /blog/categories " , function ( Request $request , Response $response )
{
$post = $this -> blogData -> getCategories ();
if ( array_key_exists ( " errorMessage " , $post ))
{
$response -> getBody () -> write ( json_encode ( $post ));
return $response -> withStatus ( 404 );
}
$response -> getBody () -> write ( json_encode ( $post ));
return $response ;
});
$app -> get ( " /blog/categories/ { category} " , function ( Request $request , Response $response , $args )
{
if ( $args [ " category " ] != null )
{
$post = $this -> blogData -> getPostsByCategory ( $args [ " category " ]);
if ( array_key_exists ( " errorMessage " , $post ))
{
$response -> getBody () -> write ( json_encode ( $post ));
return $response -> withStatus ( 404 );
}
$response -> getBody () -> write ( json_encode ( $post ));
return $response ;
}
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide a category " )));
return $response -> withStatus ( 400 );
});
2023-07-12 03:29:56 +01:00
$app -> get ( " /blog/post " , function ( Request $request , Response $response )
{
$posts = $this -> blogData -> getBlogPosts ();
$json = json_encode ( $posts );
$response -> getBody () -> write ( $json );
if ( array_key_exists ( " errorMessage " , $posts ))
{
$response -> withStatus ( 404 );
}
return $response ;
});
2023-10-18 00:28:34 +01:00
$app -> get ( " /blog/post/ { type} " , function ( Request $request , Response $response , $args )
2023-07-12 03:29:56 +01:00
{
2023-11-14 01:02:27 +00:00
if ( $args [ " type " ] == null )
2023-07-12 03:29:56 +01:00
{
2023-11-14 01:02:27 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide a title " )));
return $response -> withStatus ( 400 );
}
2023-10-18 00:28:34 +01:00
2023-11-14 01:02:27 +00:00
if ( $args [ " type " ] == " latest " )
{
$post = $this -> blogData -> getLatestBlogPost ();
if ( array_key_exists ( " errorMessage " , $post ))
2023-10-18 00:28:34 +01:00
{
$response -> getBody () -> write ( json_encode ( $post ));
2023-11-14 01:02:27 +00:00
return $response -> withStatus ( 404 );
2023-10-18 00:28:34 +01:00
}
2023-11-14 01:02:27 +00:00
$response -> getBody () -> write ( json_encode ( $post ));
return $response ;
}
if ( $args [ " type " ] == " featured " )
{
$post = $this -> blogData -> getFeaturedBlogPost ();
2023-07-12 03:29:56 +01:00
if ( array_key_exists ( " errorMessage " , $post ))
{
$response -> getBody () -> write ( json_encode ( $post ));
return $response -> withStatus ( 404 );
}
$response -> getBody () -> write ( json_encode ( $post ));
return $response ;
}
2023-11-14 01:02:27 +00:00
$post = $this -> blogData -> getBlogPost ( $args [ " type " ]);
if ( array_key_exists ( " errorMessage " , $post ))
{
$response -> getBody () -> write ( json_encode ( $post ));
return $response -> withStatus ( 404 );
}
$response -> getBody () -> write ( json_encode ( $post ));
return $response ;
});
$app -> get ( " /blog/feed/ { type} " , function ( Request $request , Response $response , $args )
{
if ( $args [ " type " ] == null )
{
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide a title " )));
return $response -> withStatus ( 400 );
}
$feed = $this -> blogData -> getFeed ( $args [ " type " ]);
if ( is_array ( $feed ))
{
$response -> getBody () -> write ( json_encode ( $feed ));
return $response -> withStatus ( 404 );
}
if ( $args [ " type " ] == " atom " )
{
$response -> getBody () -> write ( $feed );
return $response -> withHeader ( " Content-Type " , " application/atom+xml " );
}
if ( $args [ " type " ] == " rss " )
{
$response -> getBody () -> write ( $feed );
return $response -> withHeader ( " Content-Type " , " application/rss+xml " );
}
if ( $args [ " type " ] == " json " )
{
$response -> getBody () -> write ( json_encode ( $feed ));
return $response -> withHeader ( " Content-Type " , " application/feed+json " );
}
$response -> getBody () -> write ( json_encode ( array ( " error " => " Invalid feed type " )));
2023-07-12 03:29:56 +01:00
return $response -> withStatus ( 400 );
});
2023-10-31 19:36:51 +00:00
$app -> get ( " /blog/search/ { searchTerm} " , function ( Request $request , $response , $args )
{
2023-11-14 01:02:27 +00:00
if ( $args [ " searchTerm " ] == null )
2023-10-31 19:36:51 +00:00
{
2023-11-14 01:02:27 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide a search term " )));
return $response -> withStatus ( 400 );
}
2023-10-31 19:36:51 +00:00
2023-11-14 01:02:27 +00:00
$posts = $this -> blogData -> searchBlog ( $args [ " searchTerm " ]);
2023-10-31 19:36:51 +00:00
2023-11-14 01:02:27 +00:00
$json = json_encode ( $posts );
2023-10-31 19:36:51 +00:00
2023-11-14 01:02:27 +00:00
$response -> getBody () -> write ( $json );
2023-10-31 19:36:51 +00:00
2023-11-14 01:02:27 +00:00
if ( array_key_exists ( " errorMessage " , $posts ))
{
$response -> withStatus ( 404 );
2023-10-31 19:36:51 +00:00
}
2023-11-14 01:02:27 +00:00
return $response ;
2023-10-31 19:36:51 +00:00
});
2023-07-12 03:29:56 +01:00
$app -> patch ( " /blog/post/ { id} " , function ( Request $request , Response $response , $args )
{
$data = $request -> getParsedBody ();
2023-11-14 01:02:27 +00:00
if ( $args [ " id " ] == null )
2023-07-12 03:29:56 +01:00
{
2023-11-14 01:02:27 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide an ID " )));
return $response -> withStatus ( 400 );
}
2023-10-18 00:28:34 +01:00
2023-11-14 01:02:27 +00:00
if ( empty ( $data [ " title " ]) || strlen ( $data [ " featured " ]) == 0 || empty ( $data [ " body " ]) || empty ( $data [ " bodyText " ]) || empty ( $data [ " dateModified " ]) || empty ( $data [ " categories " ]))
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Only some of the data was sent " )));
return $response -> withStatus ( 400 );
}
2023-07-12 03:29:56 +01:00
2023-11-14 01:02:27 +00:00
if ( ! preg_match ( '/[a-zA-Z0-9 ]+, |\w+/mx' , $data [ " categories " ]))
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Categories must be in a CSV format " )));
return $response -> withStatus ( 400 );
}
2023-07-12 03:29:56 +01:00
2023-11-14 01:02:27 +00:00
$message = $this -> blogData -> updatePost ( $args [ " id " ], $data [ " title " ], intval ( $data [ " featured " ]), $data [ " abstract " ], $data [ " body " ], $data [ " bodyText " ], $data [ " dateModified " ], $data [ " categories " ]);
2023-07-12 03:29:56 +01:00
2023-11-14 01:02:27 +00:00
if ( $message === " post not found " )
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Error, post not found " )));
return $response -> withStatus ( 404 );
}
2023-07-12 03:29:56 +01:00
2023-11-14 01:02:27 +00:00
if ( $message === " unset featured " )
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Error, cannot unset featured post, try updating another post to be featured first " )));
return $response -> withStatus ( 409 );
}
2023-10-18 00:28:34 +01:00
2023-11-14 01:02:27 +00:00
if ( ! is_bool ( $message ) || $message === false )
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => $message )));
return $response -> withStatus ( 500 );
2023-07-12 03:29:56 +01:00
}
2023-11-14 01:02:27 +00:00
$response -> withStatus ( 201 );
return $response ;
2023-07-12 03:29:56 +01:00
});
$app -> delete ( " /blog/post/ { id} " , function ( Request $request , Response $response , $args )
{
2023-11-14 01:02:27 +00:00
if ( $args [ " id " ] == null )
2023-07-12 03:29:56 +01:00
{
2023-11-14 01:02:27 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide an ID " )));
return $response -> withStatus ( 400 );
}
2023-07-12 03:29:56 +01:00
2023-11-14 01:02:27 +00:00
$message = $this -> blogData -> deletePost ( $args [ " id " ]);
2023-07-12 03:29:56 +01:00
2023-11-14 01:02:27 +00:00
if ( $message === " post not found " )
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Error, post not found " )));
return $response -> withStatus ( 404 );
}
2023-07-12 03:29:56 +01:00
2023-11-14 01:02:27 +00:00
if ( $message === " error " )
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Error, something went wrong " )));
return $response -> withStatus ( 500 );
2023-07-12 03:29:56 +01:00
}
2023-11-14 01:02:27 +00:00
if ( $message === " cannot delete " )
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => " Error, cannot delete featured post " )));
return $response -> withStatus ( 409 );
}
return $response ;
2023-07-12 03:29:56 +01:00
});
2023-12-06 00:38:33 +00:00
$app -> delete ( " /blog/newsletter/ { email} " , function ( Request $request , Response $response , $args )
{
if ( $args [ " email " ] == null )
{
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide an email " )));
return $response -> withStatus ( 400 );
}
$message = $this -> blogData -> deleteNewsletterEmail ( $args [ " email " ]);
if ( $message === " email not found " )
{
// uh oh something went wrong
2023-12-13 03:10:23 +00:00
$response -> getBody () -> write ( json_encode ( array ( " message " => " Woah, you're already trying to leave without signing up? " )));
2023-12-06 00:38:33 +00:00
return $response -> withStatus ( 404 );
}
if ( $message === " error " )
{
// uh oh something went wrong
2023-12-13 03:10:23 +00:00
$response -> getBody () -> write ( json_encode ( array ( " message " => " Error, something went wrong " )));
2023-12-06 00:38:33 +00:00
return $response -> withStatus ( 500 );
}
2023-12-13 03:10:23 +00:00
$response -> getBody () -> write ( json_encode ( array ( " message " => " Sorry to see you go! You'll no longer receive any emails from me. If you change your mind, you can always sign up again. " )));
2023-12-06 00:38:33 +00:00
return $response ;
});
2023-07-12 03:29:56 +01:00
$app -> post ( " /blog/post " , function ( Request $request , Response $response )
2023-06-26 03:54:25 +01:00
{
$data = $request -> getParsedBody ();
$files = $request -> getUploadedFiles ();
2023-12-06 00:38:33 +00:00
if ( empty ( $data [ " title " ]) || strlen ( $data [ " featured " ]) == 0 || empty ( $data [ " body " ]) || empty ( $data [ " bodyText " ]) || empty ( $data [ " abstract " ]) || empty ( $data [ " dateCreated " ]) || empty ( $data [ " categories " ]))
2023-06-26 03:54:25 +01:00
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Error, empty data sent " )));
return $response -> withStatus ( 400 );
}
2023-10-18 23:58:21 +01:00
if ( ! preg_match ( '/[a-zA-Z0-9 ]+, |\w+/mx' , $data [ " categories " ]))
2023-10-18 00:28:34 +01:00
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Categories must be in a CSV format " )));
return $response -> withStatus ( 400 );
}
2023-12-06 00:38:33 +00:00
if ( array_key_exists ( " headerImg " , $files ))
{
$headerImg = $files [ " headerImg " ];
}
2023-06-26 16:52:48 +01:00
if ( empty ( $files [ " headerImg " ]))
{
$headerImg = null ;
}
2023-12-13 03:10:23 +00:00
// $featured = $data["featured"] === "true";
$insertedID = $this -> blogData -> createPost ( $data [ " title " ], $data [ " abstract " ], $data [ " body " ], $data [ " bodyText " ], $data [ " dateCreated " ], intval ( $data [ " featured " ]), $data [ " categories " ], $headerImg );
2023-06-26 03:54:25 +01:00
if ( ! is_int ( $insertedID ))
{
// uh oh something went wrong
$response -> getBody () -> write ( json_encode ( array ( " error " => $insertedID )));
return $response -> withStatus ( 500 );
}
$response -> getBody () -> write ( json_encode ( array ( " ID " => $insertedID )));
return $response -> withStatus ( 201 );
});
$app -> post ( " /blog/uploadPostImage " , function ( Request $request , Response $response )
{
$files = $request -> getUploadedFiles ();
if ( empty ( $files ))
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => array ( " message " => " Error, empty data sent " ))));
return $response -> withStatus ( 400 );
}
$message = $this -> blogData -> uploadPostImage ( $files [ " upload " ]);
if ( ! is_array ( $message ))
{
$response -> getBody () -> write ( json_encode ( array ( " error " => array ( " message " => $message ))));
return $response -> withStatus ( 500 );
}
$response -> getBody () -> write ( json_encode ( $message ));
return $response -> withStatus ( 201 );
});
2023-07-12 03:29:56 +01:00
$app -> post ( " /blog/headerImage/ { id} " , function ( Request $request , Response $response , $args )
{
$files = $request -> getUploadedFiles ();
2023-11-14 01:02:27 +00:00
if ( $args [ " id " ] == null )
2023-07-12 03:29:56 +01:00
{
2023-11-14 01:02:27 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide an ID " )));
return $response -> withStatus ( 400 );
}
2023-07-12 03:29:56 +01:00
2023-11-14 01:02:27 +00:00
if ( empty ( $files ))
{
// uh oh sent some empty data
2023-12-06 00:38:33 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => " Error, empty data sent " )));
2023-11-14 01:02:27 +00:00
return $response -> withStatus ( 400 );
}
2023-07-12 03:29:56 +01:00
2023-11-14 01:02:27 +00:00
$message = $this -> blogData -> uploadHeaderImage ( $args [ " id " ], $files [ " headerImg " ]);
if ( ! is_array ( $message ))
{
2023-12-06 00:38:33 +00:00
$response -> getBody () -> write ( json_encode ( array ( " error " => $message )));
2023-11-14 01:02:27 +00:00
return $response -> withStatus ( 500 );
2023-07-12 03:29:56 +01:00
}
2023-11-14 01:02:27 +00:00
$response -> getBody () -> write ( json_encode ( $message ));
return $response -> withStatus ( 201 );
2023-07-12 03:29:56 +01:00
});
2023-12-06 00:38:33 +00:00
$app -> post ( " /blog/newsletter " , function ( Request $request , Response $response )
{
$data = $request -> getParsedBody ();
if ( empty ( $data [ " subject " ]) || empty ( $data [ " message " ]))
{
// uh oh sent some empty data
$response -> getBody () -> write ( json_encode ( array ( " error " => " Error, empty data sent " )));
return $response -> withStatus ( 400 );
}
$message = $this -> blogData -> sendNewsletter ( strtolower ( $data [ " subject " ]), $data [ " message " ]);
if ( is_array ( $message ))
{
$response -> getBody () -> write ( json_encode ( array ( " error " => " Error, something went wrong " )));
return $response -> withStatus ( 500 );
}
$response -> getBody () -> write ( json_encode ( array ( " message " => " Message sent " )));
return $response -> withStatus ( 201 );
});
$app -> post ( " /blog/newsletter/ { email} " , function ( Request $request , Response $response , $args )
{
if ( $args [ " email " ] == null )
{
$response -> getBody () -> write ( json_encode ( array ( " error " => " Please provide an email " )));
return $response -> withStatus ( 400 );
}
$message = $this -> blogData -> addNewsletterEmail ( $args [ " email " ]);
if ( $message === " Email already exists " )
{
$response -> getBody () -> write ( json_encode ( array ( " message " => " exists " )));
return $response -> withStatus ( 409 );
}
if ( is_array ( $message ) || ! $message || $message === " error " )
{
$response -> getBody () -> write ( json_encode ( array ( " message " => " Something went wrong " )));
return $response -> withStatus ( 500 );
}
$response -> getBody () -> write ( json_encode ( array ( " message " => " Thanks for signing up! " )));
return $response -> withStatus ( 201 );
});
2023-06-08 15:10:27 +01:00
}
}