Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add category message #780

Merged
merged 5 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/LANraragi/Controller/Api/Category.pm
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ sub add_to_category {
my ( $result, $err ) = LANraragi::Model::Category::add_to_category( $catid, $arcid );

if ($result) {
render_api_response( $self, "add_to_category" );
my $successMessage = "Added $arcid to Category $catid!";
my %category = LANraragi::Model::Category::get_category($catid);
my $title = LANraragi::Model::Archive::get_title($arcid);

if (%category && defined($title)) {
$successMessage = "Added \"$title\" to category \"$category{name}\"!";
}

render_api_response( $self, "add_to_category", undef, $successMessage );
} else {
render_api_response( $self, "add_to_category", $err );
}
Expand Down
19 changes: 19 additions & 0 deletions lib/LANraragi/Model/Archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use strict;
use warnings;
use utf8;

use feature qw(signatures);
no warnings 'experimental::signatures';

use Cwd 'abs_path';
use Redis;
use Time::HiRes qw(usleep);
Expand All @@ -18,6 +21,22 @@ use LANraragi::Utils::Archive qw(extract_single_file extract_thumbnail);
use LANraragi::Utils::Database
qw(redis_encode redis_decode invalidate_cache set_title set_tags get_archive_json get_archive_json_multi);

# get_archive(id)
# Returns the title for the archive matching the given id.
# Returns undef if the id doesn't exist.
sub get_title($id) {

my $logger = get_logger( "Archives", "lanraragi" );
my $redis = LANraragi::Model::Config->get_redis;

if ( $id eq "" ) {
$logger->debug("No archive ID provided.");
return ();
}

return $redis->hget( $id, "title" );
}

# Functions used when dealing with archives.

# Generates an array of all the archive JSONs in the database that have existing files.
Expand Down
9 changes: 5 additions & 4 deletions lib/LANraragi/Utils/Generic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ sub is_archive {
# Renders the basic success API JSON template.
# Specifying an error message argument will set the success variable to 0.
sub render_api_response {
my ( $mojo, $operation, $errormessage ) = @_;
my ( $mojo, $operation, $errormessage, $successMessage ) = @_;
my $failed = ( defined $errormessage );

$mojo->render(
json => {
operation => $operation,
error => $failed ? $errormessage : "",
success => $failed ? 0 : 1
operation => $operation,
error => $failed ? $errormessage : "",
success => $failed ? 0 : 1,
successMessage => $failed ? "" : $successMessage,
},
status => $failed ? 400 : 200
);
Expand Down
8 changes: 6 additions & 2 deletions public/js/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ Server.callAPI = function (endpoint, method, successMessage, errorMessage, succe
if (Object.prototype.hasOwnProperty.call(data, "success") && !data.success) {
throw new Error(data.error);
} else {
if (successMessage !== null) {
let message = successMessage;
if ("successMessage" in data && data.successMessage !== null) {
message = data.successMessage;
}
if (message !== null) {
LRR.toast({
heading: successMessage,
heading: message,
icon: "success",
hideAfter: 7000,
});
Expand Down
3 changes: 2 additions & 1 deletion tools/Documentation/api-documentation/category-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ Archive ID to add.
```
{
"operation": "add_to_category",
"success": 1
"success": 1,
"successMessage": "Added \"Name of archive\" to category \"Name of category\""
}
```
{% endswagger-response %}
Expand Down