|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + |
| 10 | + "go.mongodb.org/mongo-driver/bson" |
| 11 | + "go.mongodb.org/mongo-driver/mongo" |
| 12 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 13 | +) |
| 14 | + |
| 15 | +type postAccRequestBody struct { |
| 16 | + Username string `json:"username"` |
| 17 | +} |
| 18 | + |
| 19 | +func postAccHome(c *gin.Context) { |
| 20 | + userid, err := validateToken(c.GetHeader("Authorization"), false, true) |
| 21 | + if err != nil { |
| 22 | + c.IndentedJSON(http.StatusBadRequest, "INVALID TOKEN") |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + var requestBody postAccRequestBody |
| 27 | + if err = c.BindJSON(&requestBody); err != nil { |
| 28 | + c.IndentedJSON(http.StatusBadRequest, "NEED USERNAME IN BODY") |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + result, err := mongoClient.Database("npuzzle").Collection("Akun"). |
| 33 | + UpdateOne(context.TODO(), |
| 34 | + bson.D{{Key: "creator_id", Value: userid}}, |
| 35 | + bson.D{{Key: "$set", Value: bson.D{{Key: "username", Value: requestBody.Username}}}}, |
| 36 | + ) |
| 37 | + if err != nil { |
| 38 | + c.IndentedJSON(http.StatusBadRequest, err) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + if result.MatchedCount != 1 { |
| 43 | + c.IndentedJSON(http.StatusBadRequest, "USER NOT FOUND") |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + c.IndentedJSON(http.StatusOK, nil) |
| 48 | +} |
| 49 | + |
| 50 | +func getAccHome(c *gin.Context) { |
| 51 | + userid, err := validateToken(c.GetHeader("Authorization"), false, true) |
| 52 | + if err != nil { |
| 53 | + c.IndentedJSON(http.StatusBadRequest, err.Error()) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + var pipeline mongo.Pipeline |
| 58 | + pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.D{ |
| 59 | + {Key: "creator_id", Value: userid}, |
| 60 | + }}}) |
| 61 | + |
| 62 | + pipeline = append(pipeline, bson.D{{Key: "$lookup", Value: bson.D{ |
| 63 | + {Key: "from", Value: "Favourites"}, |
| 64 | + {Key: "localField", Value: "creator_id"}, |
| 65 | + {Key: "foreignField", Value: "creator_id"}, |
| 66 | + {Key: "pipeline", Value: mongo.Pipeline{ |
| 67 | + bson.D{{Key: "$project", Value: bson.D{ |
| 68 | + {Key: "_id", Value: 0}, |
| 69 | + {Key: "c", Value: bson.D{{Key: "$size", Value: "$puzzles"}}}, |
| 70 | + }}}, |
| 71 | + }}, |
| 72 | + {Key: "as", Value: "countFav"}, |
| 73 | + }}}) |
| 74 | + |
| 75 | + pipeline = append(pipeline, bson.D{{Key: "$lookup", Value: bson.D{ |
| 76 | + {Key: "from", Value: "UserHistory"}, |
| 77 | + {Key: "localField", Value: "creator_id"}, |
| 78 | + {Key: "foreignField", Value: "creator_id"}, |
| 79 | + {Key: "pipeline", Value: mongo.Pipeline{ |
| 80 | + bson.D{{Key: "$project", Value: bson.D{ |
| 81 | + {Key: "_id", Value: 0}, |
| 82 | + {Key: "puzzles", Value: 1}, |
| 83 | + }}}, |
| 84 | + }}, |
| 85 | + {Key: "as", Value: "comp"}, |
| 86 | + }}}) |
| 87 | + |
| 88 | + pipeline = append(pipeline, bson.D{{Key: "$lookup", Value: bson.D{ |
| 89 | + {Key: "from", Value: "Puzzle"}, |
| 90 | + {Key: "localField", Value: "creator_id"}, |
| 91 | + {Key: "foreignField", Value: "creator_id"}, |
| 92 | + {Key: "pipeline", Value: mongo.Pipeline{ |
| 93 | + bson.D{{Key: "$project", Value: bson.D{ |
| 94 | + {Key: "_id", Value: 0}, |
| 95 | + {Key: "puzzle_id", Value: 1}, |
| 96 | + }}}, |
| 97 | + }}, |
| 98 | + {Key: "as", Value: "countMyP"}, |
| 99 | + }}}) |
| 100 | + |
| 101 | + pipeline = append(pipeline, bson.D{{Key: "$lookup", Value: bson.D{ |
| 102 | + {Key: "from", Value: "SavedState"}, |
| 103 | + {Key: "localField", Value: "creator_id"}, |
| 104 | + {Key: "foreignField", Value: "creator_id"}, |
| 105 | + {Key: "pipeline", Value: mongo.Pipeline{ |
| 106 | + bson.D{{Key: "$project", Value: bson.D{ |
| 107 | + {Key: "_id", Value: 0}, |
| 108 | + {Key: "States", Value: bson.D{{Key: "$map", Value: bson.D{ |
| 109 | + {Key: "input", Value: bson.D{{Key: "$objectToArray", Value: "$States"}}}, |
| 110 | + {Key: "as", Value: "e"}, |
| 111 | + {Key: "in", Value: "$$e.k"}, |
| 112 | + }}}}, |
| 113 | + }}}, |
| 114 | + }}, |
| 115 | + {Key: "as", Value: "sStates"}, |
| 116 | + }}}) |
| 117 | + |
| 118 | + pipeline = append(pipeline, bson.D{{Key: "$set", Value: bson.D{ |
| 119 | + {Key: "countMyP", Value: bson.D{{Key: "$size", Value: "$countMyP"}}}, |
| 120 | + {Key: "countFav", Value: bson.D{{Key: "$cond", Value: bson.D{ |
| 121 | + {Key: "if", Value: bson.D{{Key: "$gt", Value: bson.A{ |
| 122 | + bson.D{{Key: "$size", Value: "$countFav"}}, |
| 123 | + 0, |
| 124 | + }}}}, |
| 125 | + {Key: "then", Value: bson.D{{Key: "$first", Value: "$countFav"}}}, |
| 126 | + {Key: "else", Value: 0}, |
| 127 | + }}}}, |
| 128 | + {Key: "comp", Value: bson.D{{Key: "$cond", Value: bson.D{ |
| 129 | + {Key: "if", Value: bson.D{{Key: "$gt", Value: bson.A{ |
| 130 | + bson.D{{Key: "$size", Value: "$comp"}}, |
| 131 | + 0, |
| 132 | + }}}}, |
| 133 | + {Key: "then", Value: bson.D{{Key: "$first", Value: "$comp"}}}, |
| 134 | + {Key: "else", Value: bson.D{{Key: "puzzles", Value: bson.A{}}}}, |
| 135 | + }}}}, |
| 136 | + {Key: "sStates", Value: bson.D{{Key: "$cond", Value: bson.D{ |
| 137 | + {Key: "if", Value: bson.D{{Key: "$gt", Value: bson.A{ |
| 138 | + bson.D{{Key: "$size", Value: "$sStates"}}, |
| 139 | + 0, |
| 140 | + }}}}, |
| 141 | + {Key: "then", Value: bson.D{{Key: "$first", Value: "$sStates"}}}, |
| 142 | + {Key: "else", Value: bson.D{{Key: "States", Value: bson.A{}}}}, |
| 143 | + }}}}, |
| 144 | + }}}) |
| 145 | + |
| 146 | + pipeline = append(pipeline, bson.D{{Key: "$set", Value: bson.D{ |
| 147 | + {Key: "countComp", Value: bson.D{{Key: "$size", Value: "$comp.puzzles"}}}, |
| 148 | + {Key: "countFav", Value: "$countFav.c"}, |
| 149 | + {Key: "comp", Value: "$comp.puzzles"}, |
| 150 | + {Key: "sStates", Value: "$sStates.States"}, |
| 151 | + }}}) |
| 152 | + |
| 153 | + pipeline = append(pipeline, bson.D{{Key: "$addFields", Value: bson.D{ |
| 154 | + {Key: "countUncomp", Value: bson.D{{Key: "$subtract", Value: bson.A{ |
| 155 | + bson.D{{Key: "$size", Value: "$sStates"}}, |
| 156 | + bson.D{{Key: "$size", Value: bson.D{{Key: "$setIntersection", Value: bson.A{"$sStates", "$comp"}}}}}, |
| 157 | + }}}}, |
| 158 | + }}}) |
| 159 | + |
| 160 | + pipeline = append(pipeline, bson.D{{Key: "$project", Value: bson.D{ |
| 161 | + {Key: "_id", Value: 0}, |
| 162 | + {Key: "ftkn", Value: 0}, |
| 163 | + {Key: "dtkn", Value: 0}, |
| 164 | + {Key: "comp", Value: 0}, |
| 165 | + {Key: "sStates", Value: 0}, |
| 166 | + {Key: "creator_id", Value: 0}, |
| 167 | + }}}) |
| 168 | + |
| 169 | + cursor, err := mongoClient.Database("npuzzle").Collection("Akun"). |
| 170 | + Aggregate(context.TODO(), pipeline, options.Aggregate()) |
| 171 | + |
| 172 | + if err != nil { |
| 173 | + log.Panic(err) |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + var results []bson.D |
| 178 | + if err = cursor.All(context.TODO(), &results); err != nil { |
| 179 | + panic(err) |
| 180 | + } |
| 181 | + |
| 182 | + var arr []map[string]interface{} |
| 183 | + for _, e := range results { |
| 184 | + arr = append(arr, e.Map()) |
| 185 | + } |
| 186 | + |
| 187 | + if len(arr) > 0 { |
| 188 | + c.IndentedJSON(http.StatusOK, gin.H{ |
| 189 | + "username": arr[0]["username"], |
| 190 | + "countFavorites": arr[0]["countFav"], |
| 191 | + "countCompleted": arr[0]["countComp"], |
| 192 | + "countUncompleted": arr[0]["countUncomp"], |
| 193 | + "countMyPuzzles": arr[0]["countMyP"], |
| 194 | + }) |
| 195 | + } else { |
| 196 | + c.IndentedJSON(http.StatusBadRequest, "USER ID NOT FOUND") |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +//curl -X POST --header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmUiOiJTdW4sIDI4IEF1ZyAyMDIyIDAzOjE1OjMyICswOTAwIiwiaWQiOiJhbXNsa2RpdWV3cmE1NDUifQ.PuNqwGfcY0h9NooicnlJRCqXOrbo28LcYdFbz_BBngs" --data "{ \"username\": \"testchange\" }" http://localhost:8080 |
| 201 | +//curl -X GET --header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHBpcmUiOiJTdW4sIDI4IEF1ZyAyMDIyIDAzOjE1OjMyICswOTAwIiwiaWQiOiJhbXNsa2RpdWV3cmE1NDUifQ.PuNqwGfcY0h9NooicnlJRCqXOrbo28LcYdFbz_BBngs" http://localhost:8080 |
0 commit comments