Skip to content

Commit

Permalink
update serverless file for post-api, change userId in createPost endp…
Browse files Browse the repository at this point in the history
…oint to use actual user from token, chnage types in frontend and update PostCard component to use actual users username
  • Loading branch information
lokytech5 committed Sep 19, 2024
1 parent a1c68a7 commit 06a787c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
7 changes: 5 additions & 2 deletions backend/services/post-api/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ provider:
region: us-east-1
stage: ${opt:stage, 'dev'}
environment:
USERS_TABLE: ${cf:backend-${self:provider.stage}.UsersTableName}
USERS_TABLE_ARN:
"Fn::ImportValue": UsersTableArn-${self:provider.stage}
USERS_TABLE_NAME:
"Fn::ImportValue": UsersTableName-${self:provider.stage}
POSTS_TABLE: ${self:custom.projectPrefix}-posts-table-${self:provider.stage}
POSTHASHTAGS_TABLE: ${self:custom.projectPrefix}-posthashtags-table-${self:provider.stage}
CATEGORIES_TABLE: ${self:custom.projectPrefix}-categories-table-${self:provider.stage}
Expand Down Expand Up @@ -41,7 +44,7 @@ provider:
- arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.projectPrefix}-categories-table-${self:provider.stage}
- arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.projectPrefix}-likes-table-${self:provider.stage}
- arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.projectPrefix}-posthashtags-table-${self:provider.stage}

- ${self:provider.environment.USERS_TABLE_ARN}
custom:
projectPrefix: egetwhy

Expand Down
52 changes: 46 additions & 6 deletions backend/services/post-api/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { BatchGetCommand, BatchWriteCommand, GetCommand, PutCommand, QueryComman
import { buildResponse } from "../../../lib/responseUtils";

const POSTS_TABLE = process.env.POSTS_TABLE;
const USERS_TABLE = process.env.USERS_TABLE;


export const createPost = async (req: Request, res: Response) => {
const { userId, content, hashtags, categoryID, isAnonymous } = req.body;
const userId = req.user;
const { content, hashtags, categoryID, isAnonymous } = req.body;
const timestamp = new Date().toISOString();

if (!userId || !content) {
Expand Down Expand Up @@ -75,7 +78,7 @@ export const getAllPosts = async (req: Request, res: Response) => {
const params: {
TableName: string;
Limit: number;
ExclusiveStartKey?: Record<string, any>; // Optional field to handle pagination key
ExclusiveStartKey?: Record<string, any>;
} = {
TableName: POSTS_TABLE!,
Limit: limit ? parseInt(limit as string) : 10,
Expand All @@ -88,11 +91,48 @@ export const getAllPosts = async (req: Request, res: Response) => {
try {
const command = new ScanCommand(params);
const data = await docClient.send(command);
const posts = data.Items || [];

const userIds = [... new Set(posts.map(post => post.UserID))];

res.status(200).json({
posts: data.Items,
lastKey: data.LastEvaluatedKey || null,
});
if(userIds.length > 0) {
const userKeys = userIds.map(userId => ({ userId }));
const userParams = {
RequestItems: {
[USERS_TABLE!]: {
Keys: userKeys,
}
}
}

const usersCommand = new BatchGetCommand(userParams);
const usersData = await docClient.send(usersCommand);
const users = usersData.Responses?.[USERS_TABLE!] || [];

const postsWithUserDetails = posts.map(post => {
const user = users.find(u => u.userId === post.UserID);
return {
...post,
userDetails: user ? {
username: user.username,
profilePicture: user.profilePictureURL || null, // Example fields
} : {
username: 'ANONYMOUS',
profilePicture: null, // Or a default anonymous picture
},
};
});
res.status(200).json({
posts: postsWithUserDetails,
lastKey: data.LastEvaluatedKey || null,
});
} else {
// If there are no posts or no user IDs
res.status(200).json({
posts: [],
lastKey: null,
});
}

} catch (error) {
if (error instanceof Error) {
Expand Down
4 changes: 4 additions & 0 deletions frontend/app/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export interface RegisterData {
IsAnonymous: boolean;
CreatedAt: string;
UpdatedAt: string;
userDetails: {
username: string;
profilePicture?: string;
};
}

export interface PostFeedResponse {
Expand Down
10 changes: 7 additions & 3 deletions frontend/app/feed/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export interface Post {
IsAnonymous: boolean;
CreatedAt: string;
UpdatedAt: string;
userDetails: {
username: string;
profilePicture?: string;
};
}

interface UserProfile {
Expand All @@ -32,7 +36,7 @@ interface PostCardProps {
}

const PostCard: React.FC<PostCardProps> = ({ post, userProfile }) => {
const { PostID, Content, Hashtags, IsAnonymous, CreatedAt } = post;
const { PostID, Content, Hashtags, IsAnonymous, CreatedAt, userDetails } = post;

const [newComment, setNewComment] = useState('');
const [showComments, setShowComments] = useState(false);
Expand Down Expand Up @@ -139,14 +143,14 @@ const PostCard: React.FC<PostCardProps> = ({ post, userProfile }) => {
/>
) : (
<span className="text-sm font-bold">
{extractUserInitials(userProfile.fullName)}
{extractUserInitials(userDetails.username)}
</span>
)}
</div>
</div>
<div>
<h4 className="text-sm font-semibold">
{IsAnonymous ? "Anonymous" : userProfile.fullName}
{IsAnonymous ? "Anonymous" : userDetails.username}
</h4>
<span className="text-xs text-gray-400">Jobs in Tech</span>
</div>
Expand Down

0 comments on commit 06a787c

Please sign in to comment.