Add a couple more routes

This commit is contained in:
Colin McDonald 2016-03-21 19:58:08 -04:00
parent e47e140bd3
commit 4802efcfee
2 changed files with 40 additions and 16 deletions

View File

@ -84,7 +84,7 @@ public final class APIv3 {
return APIv3.getDatastore().createQuery(User.class).asList(); return APIv3.getDatastore().createQuery(User.class).asList();
}, gson::toJson); }, gson::toJson);
get("/user/:name", (req, res) -> { get("/user/create/:name", (req, res) -> {
User u = new User(UUID.randomUUID(), req.params("name")); User u = new User(UUID.randomUUID(), req.params("name"));
APIv3.getDatastore().save(u); APIv3.getDatastore().save(u);
@ -94,24 +94,43 @@ public final class APIv3 {
get("/staff", (req, res) -> { get("/staff", (req, res) -> {
Map<String, Rank> staffRanks = new HashMap<>(); Map<String, Rank> staffRanks = new HashMap<>();
for (Rank rank : APIv3.getDatastore().createQuery(Rank.class).asList()) { APIv3.getDatastore().createQuery(Rank.class).forEach(rank -> {
if (rank.isStaff()) { if (rank.isStaff()) {
staffRanks.put(rank.getId(), rank); staffRanks.put(rank.getId(), rank);
} }
} });
Map<UUID, String> staffGrants = new HashMap<>(); Map<UUID, String> staffGrants = new HashMap<>();
for (Grant staffGrant : APIv3.getDatastore().createQuery(Grant.class).field("rank").in(staffRanks.keySet()).asList()) { APIv3.getDatastore().createQuery(Grant.class).field("rank").in(staffRanks.keySet()).forEach(grant -> {
if (staffGrant.isActive()) { if (grant.isActive()) {
staffGrants.put(staffGrant.getTarget(), staffGrant.getRank()); staffGrants.put(grant.getTarget(), grant.getRank());
} }
} });
//Map<Rank, Set<User>> Map<Rank, Set<User>> result = new HashMap<>();
return "idk";
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); }, 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) -> { after((req, res) -> {
res.header("content-type", "application/json"); res.header("content-type", "application/json");
}); });

View File

@ -1,6 +1,7 @@
package net.frozenorb.apiv3.model; package net.frozenorb.apiv3.model;
import lombok.Getter; import lombok.Getter;
import net.frozenorb.apiv3.APIv3;
import net.frozenorb.apiv3.weirdStuff.ExcludeFromReplies; import net.frozenorb.apiv3.weirdStuff.ExcludeFromReplies;
import org.mongodb.morphia.annotations.Entity; import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id; import org.mongodb.morphia.annotations.Id;
@ -14,18 +15,22 @@ import java.util.UUID;
@Entity(value = "users", noClassnameStored = true) @Entity(value = "users", noClassnameStored = true)
public final class User { public final class User {
@Id private UUID id; @Getter @Id private UUID id;
@Getter private String lastName; @Getter private String lastName;
@ExcludeFromReplies @Getter private Map<String, Date> aliases; @Getter @ExcludeFromReplies private Map<String, Date> aliases;
@Getter private String otpCode; @Getter @ExcludeFromReplies private String otpCode;
@Getter private String password; @Getter @ExcludeFromReplies private String password;
@Getter private String passwordSalt; @Getter @ExcludeFromReplies private String passwordSalt;
@Getter private String email; @Getter private String email;
@Getter private int phoneNumber; @Getter private int phoneNumber;
@Getter private String lastSeenOn; @Getter private String lastSeenOn;
@Getter private Date lastSeenAt; @Getter private Date lastSeenAt;
@Getter private Date firstSeen; @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() {} // For Morphia
public User(UUID id, String lastName) { public User(UUID id, String lastName) {
@ -36,8 +41,8 @@ public final class User {
this.password = null; this.password = null;
this.passwordSalt = null; this.passwordSalt = null;
this.email = null; this.email = null;
this.phoneNumber = 0; this.phoneNumber = -1;
this.lastSeenOn = "Unknown"; this.lastSeenOn = null;
this.lastSeenAt = new Date(); this.lastSeenAt = new Date();
this.firstSeen = new Date(); this.firstSeen = new Date();