mirror of
https://github.com/samkaraca/lazuri-doviguram.git
synced 2026-04-29 17:59:51 +00:00
Update getTheme and getThemeMetas so that themes, lessons, and activities are sorted based on createdAt.
This commit is contained in:
parent
5b424ad8e6
commit
0ecf93cdab
@ -4,24 +4,23 @@ export const themeSchema = new Schema({
|
||||
_id: { type: Schema.Types.ObjectId, required: true },
|
||||
slug: { type: String, required: true },
|
||||
title: { type: String, required: true },
|
||||
explanation: { type: String, required: true },
|
||||
explanation: { type: String, required: false },
|
||||
image: { type: String, required: false },
|
||||
youtubeVideoUrl: { type: String, required: false },
|
||||
createdAt: { type: Number, required: true },
|
||||
});
|
||||
}, { timestamps: true });
|
||||
|
||||
export const lessonSchema = new Schema({
|
||||
_id: { type: Schema.Types.ObjectId, required: true },
|
||||
title: { type: String, required: true },
|
||||
explanation: { type: String, required: true },
|
||||
explanation: { type: String, required: false },
|
||||
themeId: { type: Schema.Types.ObjectId, ref: "Theme", required: true },
|
||||
});
|
||||
}, { timestamps: true });
|
||||
|
||||
export const activitySchema = new Schema({
|
||||
_id: { type: Schema.Types.ObjectId, required: true },
|
||||
lessonId: { type: Schema.Types.ObjectId, ref: "Lesson", required: true },
|
||||
title: { type: String, required: true },
|
||||
explanation: { type: String, required: true },
|
||||
explanation: { type: String, required: false },
|
||||
textContent: { type: String, required: false },
|
||||
image: { type: String, required: false },
|
||||
youtubeVideoUrl: { type: String, required: false },
|
||||
@ -32,9 +31,11 @@ export const activitySchema = new Schema({
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
});
|
||||
}, { timestamps: true });
|
||||
|
||||
themeSchema.index({ slug: 1 }, { unique: true });
|
||||
lessonSchema.index({ themeId: 1 });
|
||||
activitySchema.index({ lessonId: 1 });
|
||||
|
||||
export const Activity = models.Activity || model("Activity", activitySchema);
|
||||
export const Lesson = models.Lesson || model("Lesson", lessonSchema);
|
||||
|
||||
@ -10,7 +10,6 @@ export const deleteActivity = async ({
|
||||
try {
|
||||
await dbConnect();
|
||||
const mongooseActivity = await Activity.deleteOne({ _id: activityId });
|
||||
console.log("mongooseActivity", mongooseActivity);
|
||||
|
||||
return { status: "success", message: "Aktivite başarıyla silindi." };
|
||||
} catch (error) {
|
||||
|
||||
@ -11,7 +11,7 @@ export const updateActivity = async ({
|
||||
}): Promise<ApiResponse> => {
|
||||
try {
|
||||
await dbConnect();
|
||||
console.log("activity", JSON.stringify(activity, null, 2));
|
||||
|
||||
const mongooseActivity = await Activity.updateOne({ _id: new Types.ObjectId(activity._id) }, {
|
||||
$set: {
|
||||
audio: activity.audio,
|
||||
@ -25,7 +25,7 @@ export const updateActivity = async ({
|
||||
type: activity.type,
|
||||
}
|
||||
});
|
||||
console.log("mongooseActivity", mongooseActivity);
|
||||
|
||||
return { status: "success", message: "Aktivite başarıyla kaydedildi." };
|
||||
} catch (error) {
|
||||
console.error("ActivityApiService -> saveActivity: ", error);
|
||||
|
||||
@ -24,7 +24,6 @@ export const createTheme = async (theme: ITheme): Promise<ApiResponse> => {
|
||||
image: theme.image,
|
||||
youtubeVideoUrl: theme.youtubeVideoUrl,
|
||||
slug: theme.slug,
|
||||
createdAt: theme.createdAt,
|
||||
});
|
||||
await newTheme.save();
|
||||
|
||||
|
||||
@ -44,7 +44,12 @@ export const getTheme = async (themeSlug: string): Promise<ApiResponse<ITheme>>
|
||||
slug: 1,
|
||||
lessons: {
|
||||
$map: {
|
||||
input: {
|
||||
$sortArray: {
|
||||
input: "$lessons",
|
||||
sortBy: { createdAt: 1 }
|
||||
}
|
||||
},
|
||||
as: "lesson",
|
||||
in: {
|
||||
_id: "$$lesson._id",
|
||||
@ -53,7 +58,12 @@ export const getTheme = async (themeSlug: string): Promise<ApiResponse<ITheme>>
|
||||
themeId: "$$lesson.themeId",
|
||||
activities: {
|
||||
$filter: {
|
||||
input: {
|
||||
$sortArray: {
|
||||
input: "$allActivities",
|
||||
sortBy: { createdAt: 1 }
|
||||
}
|
||||
},
|
||||
as: "activity",
|
||||
cond: { $eq: ["$$activity.lessonId", "$$lesson._id"] }
|
||||
}
|
||||
|
||||
@ -25,16 +25,20 @@ export const getThemeMetas = async (): Promise<ApiResponse<ThemeMetaDTO[]>> => {
|
||||
lessons: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
createdAt: 1
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
return {
|
||||
status: "success", message: "", data: themes.map(theme => ({
|
||||
const sortedThemes = themes.map(theme => ({
|
||||
...theme,
|
||||
lessons: theme.lessons.map((lesson: any) => ({
|
||||
id: lesson._id.toString(),
|
||||
title: lesson.title,
|
||||
})),
|
||||
}))
|
||||
lessons: theme.lessons.sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime())
|
||||
}));
|
||||
|
||||
return {
|
||||
status: "success", message: "", data: sortedThemes
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("ThemeApiService -> getThemeMetas: ", error);
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
export interface DBActivity {
|
||||
title: string;
|
||||
explanation: string;
|
||||
textContent: string | null;
|
||||
image: string | null;
|
||||
youtubeVideoUrl: string | null;
|
||||
audio: string | null;
|
||||
type:
|
||||
| "true-false"
|
||||
| "type-in-blanks"
|
||||
| "drag-into-blanks"
|
||||
| "multiple-choice"
|
||||
| "pair-texts-with-images";
|
||||
savedAt: number;
|
||||
exercise: any;
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
import { DBActivity } from "./db_activity";
|
||||
|
||||
export interface DBLesson {
|
||||
title: string;
|
||||
explanation: string;
|
||||
activities: {
|
||||
idOrder: DBActivity[] | any;
|
||||
[key: string]: DBActivity;
|
||||
};
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
import { DBLesson } from "./db_lesson";
|
||||
|
||||
export interface DBTheme {
|
||||
pk: "theme";
|
||||
id: string;
|
||||
title: string;
|
||||
explanation: string;
|
||||
image: string;
|
||||
youtubeVideoUrl: string;
|
||||
createdAt: number;
|
||||
lessons: {
|
||||
idOrder: string[] | any;
|
||||
[key: string]: DBLesson;
|
||||
};
|
||||
}
|
||||
@ -11,7 +11,7 @@ export interface ViewModel {
|
||||
image?: string | null;
|
||||
youtubeVideoUrl: string;
|
||||
lessons: ILesson[];
|
||||
createdAt: number;
|
||||
createdAt: Date;
|
||||
// view related
|
||||
activeLesson: number | null;
|
||||
activeActivityId: string | null;
|
||||
|
||||
@ -29,11 +29,6 @@ export function useViewModel(theme: ITheme): ViewModel {
|
||||
setLessons(theme.lessons);
|
||||
}, [theme]);
|
||||
|
||||
useEffect(() => {
|
||||
console.log("ANANAA2iöçerde", theme);
|
||||
console.log("ANANAA2iöçerde", explanation, theme.explanation);
|
||||
}, [theme]);
|
||||
|
||||
// LESSON ROUTER STATE
|
||||
const router = useRouter();
|
||||
const lessonQueryParam = router.query["ders"];
|
||||
|
||||
@ -13,7 +13,6 @@ export const defaultTheme = (theme?: Partial<ITheme>) => {
|
||||
youtubeVideoUrl: "https://youtu.be/dlyQJ9fctfM",
|
||||
lessons: [],
|
||||
slug: slugifyLaz(title),
|
||||
createdAt: Date.now(),
|
||||
},
|
||||
...theme,
|
||||
} as ITheme;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import ILesson from "../lesson/lesson";
|
||||
|
||||
export default interface ITheme {
|
||||
createdAt: number;
|
||||
createdAt: Date;
|
||||
_id: string;
|
||||
slug: string;
|
||||
title: string;
|
||||
|
||||
@ -5,5 +5,5 @@ export interface ThemeMetaDTO {
|
||||
title: string;
|
||||
image: string;
|
||||
lessons: LessonMetaDTO[];
|
||||
createdAt: number;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
@ -24,7 +24,6 @@ export async function middleware(req: NextRequest) {
|
||||
if (isLoginPageRequest) return NextResponse.redirect(adminHomeUrl);
|
||||
return NextResponse.next();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
if (isApiRequest) return new NextResponse(null, { status: 401 });
|
||||
if (isLoginPageRequest) return NextResponse.next();
|
||||
return NextResponse.redirect(loginUrl);
|
||||
|
||||
@ -8,7 +8,6 @@ export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<ApiResponse>
|
||||
) {
|
||||
console.log("GELİDDİİİİ")
|
||||
const theme = req.query.theme as string;
|
||||
|
||||
if (req.method === "GET") {
|
||||
|
||||
@ -26,8 +26,11 @@ export async function getStaticProps() {
|
||||
return {
|
||||
props: {
|
||||
themeMetas: res.data.sort((a, b) =>
|
||||
a.createdAt > b.createdAt ? 1 : -1
|
||||
),
|
||||
a.createdAt.getTime() > b.createdAt.getTime() ? 1 : -1
|
||||
).map((theme) => ({
|
||||
...theme,
|
||||
createdAt: theme.createdAt.toISOString(),
|
||||
})),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@ -15,7 +15,10 @@ export async function getStaticProps(context: GetServerSidePropsContext) {
|
||||
if (res.status === "success" && res.data) {
|
||||
return {
|
||||
props: {
|
||||
themeData: res.data,
|
||||
themeData: {
|
||||
...res.data,
|
||||
createdAt: res.data.createdAt.toISOString(),
|
||||
},
|
||||
},
|
||||
revalidate: 60 * 15,
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user