pixwhile/routes/users/illustrations.cpp

39 lines
1.4 KiB
C++

#include <utility>
#include "../routes.h"
#include "../../numberhelper.h"
#include "../../servehelper.h"
#include "../../pixivclient.h"
#include "common.h"
void user_illustrations_route(const httplib::Request& req, httplib::Response& res, const Config& config, PixivClient& pixiv_client) {
uint64_t user_id = to_ull(req.matches[1].str());
uint64_t page = req.has_param("p") ? to_ull(req.get_param_value("p")) - 1 : 0;
User user;
Illusts illusts;
try {
user = pixiv_client.get_user(user_id);
illusts = pixiv_client.get_illusts(user_id, page);
} catch (const PixivException& e) {
if (e.status == 404) {
res.status = 404;
serve_error(req, res, config, "404: User not found", e.what());
} else {
res.status = 500;
serve_error(req, res, config, "500: Internal server error", "Failed to fetch user information", e.what());
}
return;
} catch (const std::exception& e) {
res.status = 500;
serve_error(req, res, config, "500: Internal server error", "Failed to fetch user information", e.what());
return;
}
Element body("body", {
generate_user_header(std::move(user), config),
generate_illusts_pager(req, config, illusts, page, "illusts")
});
serve(req, res, config, user.display_name + "'s illustrations", std::move(body));
}