Skip to content

Commit

Permalink
Update userprofile to show favorite series & update GET function to g…
Browse files Browse the repository at this point in the history
…et user data and include series data.
  • Loading branch information
nkoerner93 committed Aug 21, 2024
1 parent 2ab1f74 commit b26602f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
37 changes: 29 additions & 8 deletions src/app/actions/tvSeriesActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,47 @@ export async function getFavoriteTVSeriesBySession() {
}
}

// GET FAVORITE TV SERIES BY ACTIVE SESSION
// GET FAVORITE TV SERIES BY USERNAME
export async function getFavoriteTVSeriesByUsername(username: string) {
try {
// Fetch the subscribed TV series for a user by their username
const favoriteTVSeries = await prisma.subscribed_TVSeries.findMany({
// Fetch the user and their subscribed TV series by their username
const user = await prisma.users.findUnique({
where: {
user: {
username: username, // Filter based on the username in the related Users table
},
username: username,
},
include: {
subscribed_series: true, // Include the subscribed TV series
},
});

if (!user) {
return {
success: false,
reason: "User not found.",
totalSeries: 0,
userData: null,
};
}

const favoriteTVSeries = user.subscribed_series;
const totalSeries = favoriteTVSeries.length;

if (totalSeries === 0) {
// If no favorite series found, return a specific object
return { success: false, reason: "No favorites found.", totalSeries: 0 };
return {
success: false,
reason: "No favorites found.",
totalSeries: 0,
userData: user, // Return the user data even if no favorites are found
};
}

return { success: true, data: favoriteTVSeries, totalSeries };
return {
success: true,
userData: user, // Return the user object with favorite series
totalSeries,
reason: "", // No error message
};
} catch (error) {
console.error("Error finding favorite series:", error);
throw error; // Re-throw the error for further handling in the component
Expand Down
36 changes: 6 additions & 30 deletions src/components/ui/UserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,18 @@ import {
AvatarFallback,
AvatarImage,
} from "@/components/ui/shad-cn/avatar";
import {
Card,
CardHeader,
CardContent,
CardFooter,
} from "@/components/ui/shad-cn/card";
import { Badge } from "@/components/ui/shad-cn/badge";
import {
CalendarIcon,
MapPinIcon,
BriefcaseIcon,
MailIcon,
} from "lucide-react";
import { getUserByName } from "@/app/api/memberApi";
import { Card, CardHeader, CardContent } from "@/components/ui/shad-cn/card";
import { MonitorPlay } from "lucide-react";
import { getFavoriteTVSeriesByUsername } from "@/app/actions/tvSeriesActions";

interface UserProfileProps {
username: string;
}

export default async function UserProfile({ username }: UserProfileProps) {
const userData = await getUserByName(username);
const { success, userData, totalSeries, reason } =
await getFavoriteTVSeriesByUsername(username);
const usernameShort = userData?.username.slice(0, 2);
const FavoriteSeries = await getFavoriteTVSeriesByUsername(username);

return (
<>
Expand All @@ -54,20 +42,8 @@ export default async function UserProfile({ username }: UserProfileProps) {
</CardHeader>
<CardContent className="space-y-4">
<div className="flex items-center space-x-2">
<BriefcaseIcon className="text-muted-foreground" />
<span>Works at TechCorp</span>
</div>
<div className="flex items-center space-x-2">
<MapPinIcon className="text-muted-foreground" />
<span>San Francisco, CA</span>
</div>
<div className="flex items-center space-x-2">
<CalendarIcon className="text-muted-foreground" />
<span>Joined January 2020</span>
</div>
<div className="flex items-center space-x-2">
<MailIcon className="text-muted-foreground" />
<span>jane.doe@example.com</span>
<MonitorPlay className="text-muted-foreground" />
<span>Favorite Series: {totalSeries}</span>
</div>
</CardContent>
</Card>
Expand Down
2 changes: 1 addition & 1 deletion src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ model Users {
password String @db.VarChar(255)
created_on DateTime? @default(now()) @db.Timestamp(6)
last_login DateTime? @db.Timestamp(6)
username String @db.VarChar(255)
username String @unique @db.VarChar(255)
isPublic Boolean @default(false)
sessions Session[]
mapResults MapResult[]
Expand Down

0 comments on commit b26602f

Please sign in to comment.