-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from a0uda/UserFeature
Removal of posts_ids and comments_ids from user
- Loading branch information
Showing
10 changed files
with
404 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Post } from "../db/models/Post.js"; | ||
import { verifyAuthToken } from "./userAuth.js"; | ||
import { User } from "../db/models/User.js"; | ||
import { getSortCriteria } from "../utils/lisitng.js"; | ||
import { getPostsHelper } from "../services/lisitngs.js"; | ||
|
||
export async function getPostsPaginated( | ||
request, | ||
pageNumber = 1, | ||
pageSize = 10, | ||
sortBy | ||
) { | ||
try { | ||
let user = null; | ||
|
||
// Check if request has Authorization header | ||
if (request.headers.authorization) { | ||
const { | ||
success, | ||
err, | ||
status, | ||
user: authenticatedUser, | ||
msg, | ||
} = await verifyAuthToken(request); | ||
if (!authenticatedUser) { | ||
return { success, err, status, user: authenticatedUser, msg }; | ||
} | ||
user = authenticatedUser; | ||
} | ||
|
||
// Calculate the offset based on pageNumber and pageSize | ||
const offset = (pageNumber - 1) * pageSize; | ||
|
||
// Fetch posts with pagination and sorting | ||
const { posts } = await getPostsHelper(user, offset, pageSize, sortBy); | ||
|
||
return { | ||
success: true, | ||
status: 200, | ||
posts, | ||
msg: "Posts retrieved successfully.", | ||
}; | ||
} catch (error) { | ||
console.error("Error:", error); | ||
return { | ||
success: false, | ||
status: 500, | ||
err: "Internal Server Error", | ||
msg: "An error occurred while retrieving posts.", | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.