Skip to content

Commit

Permalink
service to count downloads by user
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Jul 27, 2023
1 parent 94e832f commit 4870285
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,18 +423,26 @@ public void testListByUserAndStatus(ServiceType serviceType) {
for (int i = 1; i <= 5; i++) {
service.create(getTestInstancePredicateDownload());
}

PagingResponse<Download> downloads =
service.listByUser(
TestConstants.TEST_ADMIN,
new PagingRequest(0, 5),
Download.Status.EXECUTING_STATUSES,
null,
true);

assertTrue(
service
.listByUser(
TestConstants.TEST_ADMIN,
new PagingRequest(0, 5),
Download.Status.EXECUTING_STATUSES,
null,
true)
.getResults()
.size()
> 0,
downloads.getResults().size() > 0,
"List by user and status operation should return 5 records");

long count =
service.countByUser(
TestConstants.TEST_ADMIN,
new PagingRequest(0, 5),
Download.Status.EXECUTING_STATUSES,
null);
assertEquals(downloads.getResults().size(), count);
}

/** Tests the status update of {@link Download}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ List<Download> listByUserLightweight(
@Nullable @Param("type") DownloadType type,
@Nullable @Param("from") Date from);

int countByUserLightweight(
@Param("creator") String creator,
@Param("status") Set<Download.Status> status,
@Nullable @Param("type") DownloadType type,
@Nullable @Param("from") Date from);

List<Download> listByUser(
@Param("creator") String creator,
@Nullable @Param("page") Pageable page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@
</if>
</select>

<select id="countByUserLightweight" resultType="Integer">
SELECT COUNT(*)
<include refid="LIST_BY_USER_COMMON" />
</select>

<sql id="LIST_BY_USER_COMMON">
FROM occurrence_download
WHERE created_by = #{creator,jdbcType=VARCHAR}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ PagingResponse<Download> listByUser(
defaultValue = "true") // true by default to keep backwards compatibility
Boolean statistics);

@RequestMapping(
method = RequestMethod.GET,
value = "user/{user}/count",
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@Override
long countByUser(
@PathVariable("user") String user,
@SpringQueryMap Pageable pageable,
@RequestParam(value = "status", required = false) Set<Download.Status> status,
@RequestParam(value = "from", required = false) Date from);

@RequestMapping(
method = RequestMethod.GET,
value = "internal/eraseAfter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,48 @@ public PagingResponse<Download> listByUser(
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
checkUserIsInSecurityContext(user, authentication);

long count;
long count = occurrenceDownloadMapper.countByUser(user, status, downloadType, from);

List<Download> downloads;
if (Boolean.FALSE.equals(statistics)) {
downloads =
occurrenceDownloadMapper.listByUserLightweight(user, page, status, downloadType, from);
count = occurrenceDownloadMapper.countByUserLightweight(user, status, downloadType, from);
} else {
downloads = occurrenceDownloadMapper.listByUser(user, page, status, downloadType, from);
count = occurrenceDownloadMapper.countByUser(user, status, downloadType, from);
}

return new PagingResponse<>(page, count, downloads);
}

@Tag(name = "Occurrence downloads")
@Operation(
operationId = "countOccurrenceDownloadsByUser",
summary = "Counts all downloads from a user",
description = "Retrieves the counts of occurrence downloads done by the user.",
extensions =
@Extension(
name = "Order",
properties = @ExtensionProperty(name = "Order", value = "0121")))
@Parameter(
name = "user",
description = "Username (administrator account required to see other users).",
in = ParameterIn.PATH)
@Pageable.OffsetLimitParameters
@ApiResponse(responseCode = "200", description = "Occurrence downloads count.")
@Docs.DefaultUnsuccessfulReadResponses
@Docs.DefaultUnsuccessfulWriteResponses
@GetMapping("user/{user}/count")
@Override
public long countByUser(
@PathVariable String user,
Pageable page,
@RequestParam(value = "status", required = false) Set<Download.Status> status,
@RequestParam(value = "from", required = false) Date from) {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
checkUserIsInSecurityContext(user, authentication);
return occurrenceDownloadMapper.countByUser(user, status, downloadType, from);
}

/**
* Lists downloads which may be eligible for erasure, based on age, size etc. This operation can
* be executed by role ADMIN only.
Expand Down

0 comments on commit 4870285

Please sign in to comment.