|
| 1 | +package com.scorpiac.javarant; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.scorpiac.javarant.exceptions.NoSuchRantException; |
| 6 | + |
| 7 | +public class Collab extends Rant { |
| 8 | + private String projectType; |
| 9 | + |
| 10 | + // Data that needs to be fetched. |
| 11 | + private boolean fetched = false; |
| 12 | + private String description; |
| 13 | + private String techStack; |
| 14 | + private String teamSize; |
| 15 | + private String url; |
| 16 | + |
| 17 | + protected Collab(int id, User user, int upvotes, int downvotes, String projectType, String summary, int commentCount) { |
| 18 | + super(id, user, upvotes, downvotes, summary, null, new String[0], commentCount); |
| 19 | + this.projectType = projectType; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Get a collab by its id. |
| 24 | + * |
| 25 | + * @param id The id of the collab to get. |
| 26 | + * @return The collab. |
| 27 | + */ |
| 28 | + public static Collab byId(int id) { |
| 29 | + // Collabs url, collab id, app id. |
| 30 | + String url = String.format("%1$s/%2$d?app=%3$s", DevRant.API_RANTS_URL, id, DevRant.APP_ID); |
| 31 | + JsonObject json = DevRant.request(url); |
| 32 | + |
| 33 | + // Check if the collab exists. |
| 34 | + if (!Util.jsonSuccess(json)) |
| 35 | + throw new NoSuchRantException(id); |
| 36 | + |
| 37 | + // Get the collab and data. |
| 38 | + JsonObject obj = json.get("rant").getAsJsonObject(); |
| 39 | + Collab collab = fromJson(obj); |
| 40 | + collab.setData(obj, json.get("comments").getAsJsonArray()); |
| 41 | + return collab; |
| 42 | + } |
| 43 | + |
| 44 | + static Collab fromJson(JsonObject json) { |
| 45 | + return new Collab( |
| 46 | + json.get("id").getAsInt(), |
| 47 | + User.fromJson(json), |
| 48 | + json.get("num_upvotes").getAsInt(), |
| 49 | + json.get("num_downvotes").getAsInt(), |
| 50 | + json.get("c_type_long").getAsString(), |
| 51 | + json.get("text").getAsString(), |
| 52 | + json.get("num_comments").getAsInt() |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + private void setData(JsonObject collab, JsonArray comments) { |
| 57 | + commentsFromJson(comments); |
| 58 | + description = collab.get("c_description").getAsString(); |
| 59 | + techStack = collab.get("c_tech_stack").getAsString(); |
| 60 | + teamSize = collab.get("c_team_size").getAsString(); |
| 61 | + url = collab.get("c_url").getAsString(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Fetch the data for this collab. If the data is already fetched, it will not be fetched again. |
| 66 | + * |
| 67 | + * @return Whether the data was fetched successfully. |
| 68 | + */ |
| 69 | + public boolean fetchData() { |
| 70 | + return fetchData(false); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Fetch the data for this collab. |
| 75 | + * |
| 76 | + * @param force Whether to fetch the data even if it was already fetched. |
| 77 | + * @return Whether the data was fetched successfully. |
| 78 | + */ |
| 79 | + public boolean fetchData(boolean force) { |
| 80 | + // Check if we already fetched and force is false. |
| 81 | + if (fetched && !force) |
| 82 | + return true; |
| 83 | + |
| 84 | + // Collabs url, collab id, app id. |
| 85 | + String url = String.format("%1$s/%2$d?app=%3$s", DevRant.API_RANTS_URL, getId(), DevRant.APP_ID); |
| 86 | + JsonObject json = DevRant.request(url); |
| 87 | + |
| 88 | + // Check for success. |
| 89 | + if (!Util.jsonSuccess(json)) |
| 90 | + return false; |
| 91 | + fetched = true; |
| 92 | + |
| 93 | + setData(json.get("rant").getAsJsonObject(), json.get("comments").getAsJsonArray()); |
| 94 | + |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get the link to the collab. |
| 100 | + */ |
| 101 | + @Override |
| 102 | + public String link() { |
| 103 | + return DevRant.link(DevRant.COLLAB_URL + "/" + getId()); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get the project type. |
| 108 | + */ |
| 109 | + public String getProjectType() { |
| 110 | + return projectType; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get the project description. |
| 115 | + */ |
| 116 | + public String getDescription() { |
| 117 | + fetchData(); |
| 118 | + return description; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get the project tech stack. |
| 123 | + */ |
| 124 | + public String getTechStack() { |
| 125 | + fetchData(); |
| 126 | + return techStack; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Get the team size. |
| 131 | + */ |
| 132 | + public String getTeamSize() { |
| 133 | + fetchData(); |
| 134 | + return teamSize; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Get the project url. |
| 139 | + */ |
| 140 | + public String getUrl() { |
| 141 | + fetchData(); |
| 142 | + return url; |
| 143 | + } |
| 144 | +} |
0 commit comments