diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index fdb8ef3..7954d0c 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -84,7 +84,7 @@ public final class APIv3 { return APIv3.getDatastore().createQuery(User.class).asList(); }, gson::toJson); - get("/user/:name", (req, res) -> { + get("/user/create/:name", (req, res) -> { User u = new User(UUID.randomUUID(), req.params("name")); APIv3.getDatastore().save(u); @@ -94,24 +94,43 @@ public final class APIv3 { get("/staff", (req, res) -> { Map staffRanks = new HashMap<>(); - for (Rank rank : APIv3.getDatastore().createQuery(Rank.class).asList()) { + APIv3.getDatastore().createQuery(Rank.class).forEach(rank -> { if (rank.isStaff()) { staffRanks.put(rank.getId(), rank); } - } + }); Map staffGrants = new HashMap<>(); - for (Grant staffGrant : APIv3.getDatastore().createQuery(Grant.class).field("rank").in(staffRanks.keySet()).asList()) { - if (staffGrant.isActive()) { - staffGrants.put(staffGrant.getTarget(), staffGrant.getRank()); + APIv3.getDatastore().createQuery(Grant.class).field("rank").in(staffRanks.keySet()).forEach(grant -> { + if (grant.isActive()) { + staffGrants.put(grant.getTarget(), grant.getRank()); } - } + }); - //Map> - return "idk"; + Map> result = new HashMap<>(); + + staffGrants.forEach((userId, rankId) -> { + User user = User.byId(userId); + Rank rank = staffRanks.get(rankId); + + if (!result.containsKey(rank)) { + result.put(rank, new HashSet<>()); + } + + result.get(rank).add(user); + }); + + return result; }, gson::toJson); + get("/grants", (req, res) -> { + int limit = Integer.parseInt(req.queryParams("limit")); + int offset = Integer.parseInt(req.queryParams("offset")); + + return APIv3.getDatastore().createQuery(Grant.class).limit(limit).offset(offset).asList(); + } ,gson::toJson); + after((req, res) -> { res.header("content-type", "application/json"); }); diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index 6fe45c2..d721220 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -1,6 +1,7 @@ package net.frozenorb.apiv3.model; import lombok.Getter; +import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.weirdStuff.ExcludeFromReplies; import org.mongodb.morphia.annotations.Entity; import org.mongodb.morphia.annotations.Id; @@ -14,18 +15,22 @@ import java.util.UUID; @Entity(value = "users", noClassnameStored = true) public final class User { - @Id private UUID id; + @Getter @Id private UUID id; @Getter private String lastName; - @ExcludeFromReplies @Getter private Map aliases; - @Getter private String otpCode; - @Getter private String password; - @Getter private String passwordSalt; + @Getter @ExcludeFromReplies private Map aliases; + @Getter @ExcludeFromReplies private String otpCode; + @Getter @ExcludeFromReplies private String password; + @Getter @ExcludeFromReplies private String passwordSalt; @Getter private String email; @Getter private int phoneNumber; @Getter private String lastSeenOn; @Getter private Date lastSeenAt; @Getter private Date firstSeen; + public static User byId(UUID id) { + return APIv3.getDatastore().createQuery(User.class).field("id").equal(id).get(); + } + public User() {} // For Morphia public User(UUID id, String lastName) { @@ -36,8 +41,8 @@ public final class User { this.password = null; this.passwordSalt = null; this.email = null; - this.phoneNumber = 0; - this.lastSeenOn = "Unknown"; + this.phoneNumber = -1; + this.lastSeenOn = null; this.lastSeenAt = new Date(); this.firstSeen = new Date();