first commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
'use server';
|
||||
|
||||
import { db } from '@/lib/db';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
|
||||
const DEFAULT_SETTINGS: Record<string, string> = {
|
||||
channelName: 'DevHub YouTube',
|
||||
youtubeUrl: 'https://youtube.com/@DevHubChannel',
|
||||
githubUrl: 'https://github.com/ayrisdev',
|
||||
contactEmail: 'contact@youtube-devhub.com',
|
||||
defaultCategory: 'Next.js',
|
||||
};
|
||||
|
||||
export async function getSettings() {
|
||||
try {
|
||||
const dbSettings = await db.setting.findMany();
|
||||
const result = { ...DEFAULT_SETTINGS };
|
||||
|
||||
dbSettings.forEach((item) => {
|
||||
result[item.key] = item.value;
|
||||
});
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('Error fetching settings:', error);
|
||||
return DEFAULT_SETTINGS;
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateSettings(data: Record<string, string>) {
|
||||
try {
|
||||
const promises = Object.entries(data).map(([key, value]) =>
|
||||
db.setting.upsert({
|
||||
where: { key },
|
||||
update: { value },
|
||||
create: { key, value },
|
||||
})
|
||||
);
|
||||
|
||||
await Promise.all(promises);
|
||||
revalidatePath('/[locale]/admin');
|
||||
return { success: true };
|
||||
} catch (error: any) {
|
||||
console.error('Error updating settings:', error);
|
||||
return { success: false, error: error.message || 'Failed to update settings' };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user