Skip to content

Commit

Permalink
FIX :: search users
Browse files Browse the repository at this point in the history
  • Loading branch information
anys34 committed Aug 27, 2023
1 parent 8276746 commit f1b0fa3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
2 changes: 0 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const session = require("express-session");

const pageRouter = require("./routes/page");
const authRouter = require("./routes/auth");
const userRouter = require("./routes/user");
const searchRouter = require("./routes/search");

const app = express();
Expand All @@ -30,7 +29,6 @@ app.use(

app.use("/", pageRouter);
app.use("/auth", authRouter);
app.use("/user", userRouter);
app.use("/search", searchRouter);

app.listen(app.get("port"), () => {
Expand Down
99 changes: 50 additions & 49 deletions routes/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require("dotenv").config();
const env = process.env;

const header = `headers: {
Authorization: "Bearer " + ${env.GITHUB_TOKEN},
Authorization: Bearer ${env.GITHUB_TOKEN},
}`;

router.get("/:id", async (req, res) => {
Expand All @@ -34,67 +34,68 @@ router.get("/:id", async (req, res) => {
});
res.send(user_Info);
} catch (error) {
console.log("API 호출 실패:", error.response.status);
console.log(error.response.data);
console.log(error);
res.status(500).send(error);
}
});

router.get("/users/:id", async (req, res) => {
const { id } = req.params;
const page = req.query.page;
let users_Info = [];
axios
.get(
try {
const response = await axios.get(
`https://api.github.com/search/users?q=${id} type:user&per_page=10&page=${page}`,
{
header,
}
)
.then(async (response) => {
const users = response.data;
const requests = users.items.map((item) => {
if (item === undefined) {
return null;
}
return axios.get(item.url, {
header,
});
);
const users = response.data;
const requests = users.items.map((item) => {
if (item === undefined) {
return null;
}
return axios.get(item.url, {
header,
});
});

const responses = await Promise.all(requests);
const responses = await Promise.all(requests);

const users_Info = await Promise.all(
responses.map(async (user_Info) => {
if (user_Info) {
const user = user_Info.data;
const commit = await getCommitCount(user.html_url);
return {
profile_img: user.avatar_url,
id: user.login,
url: user.html_url,
name: user.name,
bio: user.bio,
company: user.company,
public_repos: user.public_repos,
followers: user.followers,
following: user.following,
location: user.location,
commit: commit,
};
}
})
);
if (users_Info.length > 0 || page == 1) {
users_Info.push({
total: users.total_count,
});
}
res.send(users_Info);
})
.catch((error) => {
console.log("API 호출 실패:", error.response.status);
console.log(error.response.data);
});
const users_Info = await Promise.all(
responses.map(async (user_Info) => {
if (user_Info) {
const user = user_Info.data;
const commit = await getCommitCount(user.html_url);
return {
profile_img: user.avatar_url,
id: user.login,
url: user.html_url,
name: user.name,
bio: user.bio,
company: user.company,
public_repos: user.public_repos,
followers: user.followers,
following: user.following,
location: user.location,
commit: commit,
};
}
})
);
if (users_Info.length > 0 || page == 1) {
users_Info.push({
total: users.total_count,
});
return res.send(users_Info);
}
if (!(users_Info.length > 0)) {
return res.send(null);
}
res.send(users_Info);
} catch (error) {
console.log(error);
res.status(500).send(error);
}
});

async function getCommitCount(url) {
Expand Down

0 comments on commit f1b0fa3

Please sign in to comment.