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();
}, 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,22 +94,41 @@ public final class APIv3 {
get("/staff", (req, res) -> {
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()) {
staffRanks.put(rank.getId(), rank);
}
}
});
Map<UUID, String> 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<Rank, Set<User>> 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<>());
}
//Map<Rank, Set<User>>
return "idk";
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) -> {

View File

@ -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<String, Date> aliases;
@Getter private String otpCode;
@Getter private String password;
@Getter private String passwordSalt;
@Getter @ExcludeFromReplies private Map<String, Date> 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();