Skip to content

Commit

Permalink
Initial implementation of admin endpoints (#43)
Browse files Browse the repository at this point in the history
* Initial implementation of admin endpoints

* Update changelog
  • Loading branch information
joshgarde authored Aug 20, 2024
1 parent f0e116e commit 1841257
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Implement two new endpoints: query/user and query/l2RasterProducts
### Deprecated
### Removed
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gov.nasa.podaac.swodlr.status.StatusRepository;
import gov.nasa.podaac.swodlr.user.User;
import gov.nasa.podaac.swodlr.user.UserReference;
import gov.nasa.podaac.swodlr.user.UserRepository;
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -96,6 +97,36 @@ public L2RasterProduct invalidateProduct(@Argument UUID id) {
return product;
}

@PreAuthorize("hasRole(\"ROLE_Administrator\")")
@QueryMapping
public List<L2RasterProduct> l2RasterProducts(
@Argument Integer cycle,
@Argument Integer pass,
@Argument Integer scene,
@Argument Boolean outputGranuleExtentFlag,
@Argument GridType outputSamplingGridType,
@Argument Integer rasterResolution,
@Argument Integer utmZoneAdjust,
@Argument Integer mgrsBandAdjust,
@Argument UUID after,
@Argument int limit
) {
List<L2RasterProduct> products = l2RasterProductRepository.findByParameters(
cycle,
pass,
scene,
outputGranuleExtentFlag,
outputSamplingGridType,
rasterResolution,
utmZoneAdjust,
mgrsBandAdjust,
after,
limit
);

return products;
}

@QueryMapping
public L2RasterProduct l2RasterProduct(@Argument UUID id) {
var result = l2RasterProductRepository.findById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ List<L2RasterProduct> findByUser(
UUID after,
int limit
);

List<L2RasterProduct> findByParameters(
Integer cycle,
Integer pass,
Integer scene,
Boolean outputGranuleExtentFlag,
GridType outputSamplingGridType,
Integer rasterResolution,
Integer utmZoneAdjust,
Integer mgrsBandAdjust,
UUID after,
int limit
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public List<L2RasterProduct> findByUser(
(:rasterResolution is NULL OR \"rasterResolution\" = :rasterResolution) AND
(:utmZoneAdjust is NULL OR \"utmZoneAdjust\" = :utmZoneAdjust) AND
(:mgrsBandAdjust is NULL OR \"mgrsBandAdjust\" = :mgrsBandAdjust) AND
(CAST(:beforeTimestamp as TIMESTAMP) is NULL OR \"L2RasterProducts\".timestamp <= :beforeTimestamp) AND
(CAST(:afterTimestamp as TIMESTAMP) is NULL OR \"L2RasterProducts\".timestamp >= :afterTimestamp) AND
(CAST(:beforeTimestamp as TIMESTAMP) is NULL OR \"ProductHistory\".timestamp <= :beforeTimestamp) AND
(CAST(:afterTimestamp as TIMESTAMP) is NULL OR \"ProductHistory\".timestamp >= :afterTimestamp) AND
(\"ProductHistory\".\"requestedById\" = CAST(:userId as UUID)) AND
(
(:after is NULL)
Expand Down Expand Up @@ -105,4 +105,51 @@ public List<L2RasterProduct> findByUser(

return query.getResultList();
}

@Override
public List<L2RasterProduct> findByParameters(
Integer cycle,
Integer pass,
Integer scene,
Boolean outputGranuleExtentFlag,
GridType outputSamplingGridType,
Integer rasterResolution,
Integer utmZoneAdjust,
Integer mgrsBandAdjust,
UUID after,
int limit
) {
@SuppressWarnings("LineLength")
String statement =
"""
SELECT \"L2RasterProducts\".* FROM \"L2RasterProducts\"
WHERE
(:cycle is NULL OR \"cycle\" = :cycle) AND
(:pass is NULL OR \"pass\" = :pass) AND
(:scene is NULL OR \"scene\" = :scene) AND
(:outputGranuleExtentFlag is NULL OR \"outputGranuleExtentFlag\" = :outputGranuleExtentFlag) AND
(:outputSamplingGridType is NULL OR \"outputSamplingGridType\" = :outputSamplingGridType) AND
(:rasterResolution is NULL OR \"rasterResolution\" = :rasterResolution) AND
(:utmZoneAdjust is NULL OR \"utmZoneAdjust\" = :utmZoneAdjust) AND
(:mgrsBandAdjust is NULL OR \"mgrsBandAdjust\" = :mgrsBandAdjust) AND
(:after is NULL OR \"id\" > CAST(:after as UUID))
ORDER BY id DESC LIMIT :limit
""";

Session session = entityManager.unwrap(Session.class);
Query<L2RasterProduct> query = session.createNativeQuery(statement, L2RasterProduct.class);
query.setParameter("cycle", cycle, IntegerType.INSTANCE);
query.setParameter("pass", pass, IntegerType.INSTANCE);
query.setParameter("scene", scene, IntegerType.INSTANCE);
query.setParameter("outputGranuleExtentFlag", outputGranuleExtentFlag, BooleanType.INSTANCE);
query.setParameter("outputSamplingGridType", outputSamplingGridType != null
? outputSamplingGridType.toString() : null, StringType.INSTANCE);
query.setParameter("rasterResolution", rasterResolution, IntegerType.INSTANCE);
query.setParameter("utmZoneAdjust", utmZoneAdjust, IntegerType.INSTANCE);
query.setParameter("mgrsBandAdjust", mgrsBandAdjust, IntegerType.INSTANCE);
query.setParameter("after", after, UUIDCharType.INSTANCE);
query.setParameter("limit", limit, IntegerType.INSTANCE);

return query.getResultList();
}
}
16 changes: 16 additions & 0 deletions src/main/java/gov/nasa/podaac/swodlr/user/UserController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package gov.nasa.podaac.swodlr.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.ContextValue;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;

import gov.nasa.podaac.swodlr.exception.SwodlrException;

@Controller
public class UserController {
@Autowired
Expand All @@ -14,4 +18,16 @@ public class UserController {
public User currentUser(@ContextValue UserReference userRef) {
return userRef.fetch();
}

@PreAuthorize("hasRole(\"ROLE_Administrator\")")
@QueryMapping
public User user(@Argument String username) {
var result = userRepository.findByUsername(username);
if (result.isEmpty()) {
throw new SwodlrException("User not found");
}

User user = result.get();
return user;
}
}
18 changes: 18 additions & 0 deletions src/main/resources/graphql/query.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,22 @@ type Query {
statusByProduct(product: ID!, limit: Int = 10): [Status!]
statusByPrevious(after: ID!, limit: Int = 10): [Status!]
availableScene(cycle: Int!, pass: Int!, scene: Int!): Boolean!

# -- Admin --
user(username: String!): User

l2RasterProducts(
cycle: Int,
pass: Int,
scene: Int,
outputGranuleExtentFlag: Boolean,
outputSamplingGridType: GridType,
rasterResolution: Int,
utmZoneAdjust: Int,
mgrsBandAdjust: Int,

# Pagination
after: ID,
limit: Int = 10
): [L2RasterProduct!]!
}

0 comments on commit 1841257

Please sign in to comment.