Skip to content

Commit

Permalink
add getUserChannelProfile API and update
Browse files Browse the repository at this point in the history
Subscription model name
  • Loading branch information
hiteshchoudhary committed Dec 20, 2023
1 parent df2484a commit 94a01db
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
78 changes: 76 additions & 2 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ const getCurrentUser = asyncHandler(async(req, res) => {
.status(200)
.json(new ApiResponse(
200,
user,
req.user,
"User fetched successfully"
))
})
Expand Down Expand Up @@ -353,6 +353,78 @@ const updateUserCoverImage = asyncHandler(async(req, res) => {
})


const getUserChannelProfile = asyncHandler(async(req, res) => {
const {username} = req.params

if (!username?.trim()) {
throw new ApiError(400, "username is missing")
}

const channel = await User.aggregate([
{
$match: {
username: username?.toLowerCase()
}
},
{
$lookup: {
from: "subscriptions",
localField: "_id",
foreignField: "channel",
as: "subscribers"
}
},
{
$lookup: {
from: "subscriptions",
localField: "_id",
foreignField: "subscriber",
as: "subscribedTo"
}
},
{
$addFields: {
subscribersCount: {
$size: "$subscribers"
},
channelsSubscribedToCount: {
$size: "$subscribedTo"
},
isSubscribed: {
$cond: {
if: {$in: [req.user?._id, "$subscribers.subscriber"]},
then: true,
else: false
}
}
}
},
{
$project: {
fullName: 1,
username: 1,
subscribersCount: 1,
channelsSubscribedToCount: 1,
isSubscribed: 1,
avatar: 1,
coverImage: 1,
email: 1

}
}
])

if (!channel?.length) {
throw new ApiError(404, "channel does not exists")
}

return res
.status(200)
.json(
new ApiResponse(200, channel[0], "User channel fetched successfully")
)
})


export {
registerUser,
Expand All @@ -363,5 +435,7 @@ export {
getCurrentUser,
updateAccountDetails,
updateUserAvatar,
updateUserCoverImage
updateUserCoverImage,
getUserChannelProfile

}
2 changes: 1 addition & 1 deletion src/models/subscription.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ const subscriptionSchema = new Schema({



export const Subsciption = mongoose.model("Subsciption", subscriptionSchema)
export const Subsciption = mongoose.model("Subscription", subscriptionSchema)

0 comments on commit 94a01db

Please sign in to comment.