Skip to content

Commit

Permalink
Fix bug where invalid content item gets cached, also fix bug where ex…
Browse files Browse the repository at this point in the history
…isting movie/show returns "bad request" if you try to add it

The existing movie/show on add will now print a more friendly message
  • Loading branch information
shayaantx committed Nov 9, 2020
1 parent 981f45c commit bbb0a0b
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 26 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/botdarr/api/AddStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public AddStrategy(ChatClientResponseBuilder<? extends ChatClientResponse> chatC
public abstract String getItemId(T item);
public abstract ChatClientResponse addContent(T content);
public abstract ChatClientResponse getResponse(T item);
protected abstract void cacheContent(T addContent);

public ChatClientResponse addWithSearchId(String searchText, String id) {
try {
Expand All @@ -41,7 +40,6 @@ public ChatClientResponse addWithSearchId(String searchText, String id) {
return chatClientResponseBuilder.createErrorMessage(this.contentDisplayName + " already exists");
}
ChatClientResponse chatClientResponse = addContent(item);
cacheContent(item);
return chatClientResponse;
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/botdarr/api/lidarr/LidarrApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ public ChatClientResponse addContent(LidarrArtist content) {
public ChatClientResponse getResponse(LidarrArtist item) {
return chatClientResponseBuilder.getArtistResponse(item);
}

@Override
protected void cacheContent(LidarrArtist addContent) {
LIDARR_CACHE.addArtist(addContent);
}
};
}

Expand Down Expand Up @@ -293,6 +288,8 @@ private ChatClientResponse addArtist(LidarrArtist lidarrArtist) {
if (statusCode != 200 && statusCode != 201) {
return chatClientResponseBuilder.createErrorMessage("Could not add artist, status-code=" + statusCode + ", reason=" + response.getStatusLine().getReasonPhrase());
}
//cache artist after successful response
LIDARR_CACHE.addArtist(lidarrArtist);
LogManager.getLogger("AuditLog").info("User " + username + " added " + lidarrArtist.getArtistName());
apiRequests.auditRequest(apiRequestType, username, lidarrArtist.getArtistName());
return chatClientResponseBuilder.createSuccessMessage("Artist " + lidarrArtist.getArtistName() + " added, lidarr-detail=" + response.getStatusLine().getReasonPhrase());
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/botdarr/api/radarr/RadarrApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,6 @@ public ChatClientResponse addContent(RadarrMovie content) {
public ChatClientResponse getResponse(RadarrMovie item) {
return chatClientResponseBuilder.getMovieResponse(item);
}

@Override
protected void cacheContent(RadarrMovie addContent) {
RADARR_CACHE.add(addContent);
}
};
}

Expand Down Expand Up @@ -327,6 +322,8 @@ private ChatClientResponse addMovie(RadarrMovie radarrMovie) {
if (statusCode != 200 && statusCode != 201) {
return chatClientResponseBuilder.createErrorMessage("Could not add movie, status-code=" + statusCode + ", reason=" + response.getStatusLine().getReasonPhrase());
}
//cache data after a successful request
RADARR_CACHE.add(radarrMovie);
LogManager.getLogger("AuditLog").info("User " + username + " added " + radarrMovie.getTitle());
apiRequests.auditRequest(apiRequestType, username, radarrMovie.getTitle());
return chatClientResponseBuilder.createSuccessMessage("Movie " + radarrMovie.getTitle() + " added, radarr-detail=" + response.getStatusLine().getReasonPhrase());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/botdarr/api/radarr/RadarrCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void removeDeletedMovies(List<Long> addUpdatedMovies) {
for (Long tmdbId : addUpdatedMovies) {
RadarrMovie radarrMovie = existingTmdbIdsToMovies.get(tmdbId);
if (radarrMovie != null) {
existingMovieTitles.add(radarrMovie.getTitle());
existingMovieTitles.add(radarrMovie.getTitle().toLowerCase());
}
}
existingTmdbIdsToMovies.keySet().retainAll(addUpdatedMovies);
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/botdarr/api/sonarr/SonarrApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,6 @@ public ChatClientResponse addContent(SonarrShow content) {
public ChatClientResponse getResponse(SonarrShow item) {
return chatClientResponseBuilder.getShowResponse(item);
}

@Override
protected void cacheContent(SonarrShow addContent) {
SONARR_CACHE.add(addContent);
}
};
}

Expand Down Expand Up @@ -245,6 +240,8 @@ private ChatClientResponse addShow(SonarrShow sonarrShow) {
if (statusCode != 200 && statusCode != 201) {
return chatClientResponseBuilder.createErrorMessage("Could not add show, status-code=" + statusCode + ", reason=" + response.getStatusLine().getReasonPhrase());
}
//cache show after successful request
SONARR_CACHE.add(sonarrShow);
LogManager.getLogger("AuditLog").info("User " + username + " added " + title);
apiRequests.auditRequest(apiRequestType, username, title);
return chatClientResponseBuilder.createSuccessMessage("Show " + title + " added, sonarr-detail=" + response.getStatusLine().getReasonPhrase());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/botdarr/api/sonarr/SonarrCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public void removeDeletedShows(List<Long> addUpdatedTvdbShowIds) {
for (Long tvdbId : addUpdatedTvdbShowIds) {
SonarrShow sonarrShow = existingTvdbIdsToMovies.get(tvdbId);
if (sonarrShow != null) {
existingShowTitles.add(sonarrShow.getTitle());
existingShowTitles.add(sonarrShow.getTitle().toLowerCase());
existingTvRageIds.add(sonarrShow.getTvRageId());
existingTvmazeIds.add(sonarrShow.getTvMazeId());
}
}
existingShowTitlesToSonarrId.keySet().retainAll(addUpdatedTvdbShowIds);
existingShowTitlesToSonarrId.keySet().retainAll(existingShowTitles);
existingTvdbIdsToMovies.keySet().retainAll(addUpdatedTvdbShowIds);
existingTvrageIdsToMovies.keySet().retainAll(existingTvRageIds);
existingTvmazeIdsToMovies.keySet().retainAll(existingTvmazeIds);
Expand Down
6 changes: 0 additions & 6 deletions src/test/java/com/botdarr/api/AddStrategyTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public void addWithSearchId_moviesFound_addMovieResponseReturned() throws Except
mockAddStrategy.getItemId(movie); times = 1; result = searchId;
mockAddStrategy.doesItemExist(movie); times = 1; result = false;
mockAddStrategy.addContent(movie); times = 1; result = expectedResponse;
mockAddStrategy.cacheContent(movie); times = 1;
}};
ChatClientResponse chatClientResponse = mockAddStrategy.addWithSearchId(searchText, searchId);
Assert.assertNotNull(chatClientResponse);
Expand Down Expand Up @@ -287,10 +286,5 @@ public ChatClientResponse addContent(Object content) {
public ChatClientResponse getResponse(Object item) {
return null;
}

@Override
protected void cacheContent(Object addContent) {

}
}
}

0 comments on commit bbb0a0b

Please sign in to comment.