diff --git a/core/api.go b/core/api.go index 96e09c6..2b00697 100644 --- a/core/api.go +++ b/core/api.go @@ -396,43 +396,62 @@ func (caller apiCaller) userinfoAPI(w http.ResponseWriter, r *http.Request) erro if r.Method == "GET" { // ๊ณ„์ • ์กฐํšŒ accid, _ := gocommon.ReadObjectIDFormValue(r.Form, "accid") - if len(accid) == 0 { - logger.Println("[userinfoAPI] accid is empty") - w.WriteHeader(http.StatusBadRequest) - return nil - } - - all, err := mg.mongoClient.FindAll(CollectionAccount, bson.M{ - "accid": accid, - }, options.Find().SetProjection(bson.M{"_id": 1, "accid": 1})) - - if err != nil { - return err - } - - var linkinfos []accountlinkinfo - for _, doc := range all { - id := doc["_id"].(primitive.ObjectID) - - link, err := mg.mongoClient.FindOne(CollectionLink, bson.M{ - "_id": id, - }, options.FindOne().SetProjection(bson.M{"_id": 1, "platform": 1, "uid": 1})) + if len(accid) > 0 { + all, err := mg.mongoClient.FindAll(CollectionAccount, bson.M{ + "accid": accid, + }, options.Find().SetProjection(bson.M{"_id": 1, "accid": 1})) if err != nil { - logger.Error("link failed. FindOneAndUpdate link err:", err) - w.WriteHeader(http.StatusInternalServerError) return err } - var info accountlinkinfo - info.Platform = link["platform"].(string) - info.Uid = link["uid"].(string) - linkinfos = append(linkinfos, info) + var linkinfos []accountlinkinfo + for _, doc := range all { + id := doc["_id"].(primitive.ObjectID) + + link, err := mg.mongoClient.FindOne(CollectionLink, bson.M{ + "_id": id, + }, options.FindOne().SetProjection(bson.M{"_id": 1, "platform": 1, "uid": 1})) + + if err != nil { + logger.Error("link failed. FindOneAndUpdate link err:", err) + w.WriteHeader(http.StatusInternalServerError) + return err + } + + var info accountlinkinfo + info.Platform = link["platform"].(string) + info.Uid = link["uid"].(string) + linkinfos = append(linkinfos, info) + } + + enc := json.NewEncoder(w) + enc.Encode(linkinfos) + } + } else if r.Method == "POST" { + r.ParseMultipartForm(32 << 20) + var body struct { + Platform string + Uid []string + } + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + return err } - enc := json.NewEncoder(w) - enc.Encode(linkinfos) - + if len(body.Platform) > 0 && len(body.Uid) > 0 { + output := make(map[string]any) + for _, uid := range body.Uid { + link, err := mg.mongoClient.FindOne(CollectionLink, bson.M{ + "platform": body.Platform, + "uid": uid, + }, options.FindOne().SetProjection(bson.M{"_id": 1})) + if err != nil { + return err + } + output[uid] = link["_id"] + } + json.NewEncoder(w).Encode(output) + } } return nil