fix for persistant settings management

This commit is contained in:
overcuriousity 2024-07-18 22:42:55 +02:00
parent 5b882f4e13
commit f321bfe41e
1 changed files with 9 additions and 4 deletions

View File

@ -1,15 +1,15 @@
import express from 'express'; import express from 'express';
import db from '../db'; import db from '../db';
import { settings } from '../db/schema'; import { settings, eq } from '../db/schema';
const router = express.Router(); const router = express.Router();
router.post('/', async (req, res) => { router.post('/', async (req, res) => {
try { try {
const { chatModelProvider, chatModel, embeddingModelProvider, embeddingModel, openAIApiKey, openAIBaseURL } = req.body; const { chatModelProvider, chatModel, embeddingModelProvider, embeddingModel, openAIApiKey, openAIBaseURL } = req.body;
// TODO: Add user authentication // TODO: Add user authentication
await db.delete(settings).execute();
await db.insert(settings).values({ await db.insert(settings).values({
chatModelProvider, chatModelProvider,
chatModel, chatModel,
@ -21,6 +21,7 @@ router.post('/', async (req, res) => {
res.status(200).json({ message: 'Settings saved successfully' }); res.status(200).json({ message: 'Settings saved successfully' });
} catch (err) { } catch (err) {
console.error('Error saving settings:', err);
res.status(500).json({ message: 'An error occurred while saving settings' }); res.status(500).json({ message: 'An error occurred while saving settings' });
} }
}); });
@ -29,8 +30,12 @@ router.get('/', async (req, res) => {
try { try {
// TODO: Add user authentication // TODO: Add user authentication
const userSettings = await db.query.settings.findFirst(); const userSettings = await db.query.settings.findFirst();
if (!userSettings) {
return res.status(404).json({ message: 'No settings found' });
}
res.status(200).json(userSettings); res.status(200).json(userSettings);
} catch (err) { } catch (err) {
console.error('Error fetching settings:', err);
res.status(500).json({ message: 'An error occurred while fetching settings' }); res.status(500).json({ message: 'An error occurred while fetching settings' });
} }
}); });