db changes, refactoring with axios

This commit is contained in:
Mikayla Dobson
2022-11-23 09:46:35 -06:00
parent 20b13855ea
commit 7cc2851de7
3 changed files with 43 additions and 24 deletions

View File

@@ -3,6 +3,7 @@ import axios from "axios";
const API = import.meta.env.APISTRING || "http://localhost:8080"; const API = import.meta.env.APISTRING || "http://localhost:8080";
axios.defaults.withCredentials = true; axios.defaults.withCredentials = true;
axios.defaults.headers['Content-Type'] = 'application/json';
export const getBaseAPI = async () => { export const getBaseAPI = async () => {
return fetch(API); return fetch(API);
@@ -30,11 +31,9 @@ export const checkCredientials = async () => {
url: API + '/auth', url: API + '/auth',
}); });
const data = Promise.resolve(response.data); return Promise.resolve(response.data);
return data;
} catch (e: any) { } catch (e: any) {
throw e; console.log(e);
} }
} }
@@ -47,29 +46,47 @@ export const attemptLogout = async () => {
} catch (e: any) { } catch (e: any) {
throw e; throw e;
} }
// const result = await fetch(API + 'auth/logout', { method: "DELETE" }).then(response => response.json());
// return result;
} }
export const attemptRegister = async (data: IUser) => { export const attemptRegister = async (body: IUser) => {
const result = await fetch(API + 'auth/register/', { try {
const response = await axios({
method: "POST", method: "POST",
headers: { url: API + '/auth/register',
"Content-Type": "application/json" data: JSON.stringify(body)
}, })
body: JSON.stringify(data)
}).then(response => response.json()); return Promise.resolve(response.data);
} catch (e: any) {
return result; throw e;
}
} }
// on recipe route // on recipe route
export const getRecipeByID = async (id: string | number) => { export const getRecipeByID = async (id: string | number) => {
const result = await fetch(API + 'recipe/' + id).then(response => response.json()); try {
return result; const response = await axios({
method: "GET",
url: API + '/recipe/' + id
})
return Promise.resolve(response.data);
} catch (e: any) {
throw e;
}
} }
export const getAllRecipes = async () => { export const getAllRecipes = async () => {
const result = await fetch(API + 'recipe').then(response => response.json()); try {
return result; const response = await axios({
method: "GET",
url: API + '/recipe'
})
return Promise.resolve(response.data);
} catch (e: any) {
throw e;
}
// const result = await fetch(API + 'recipe').then(response => response.json());
// return result;
} }

View File

@@ -69,7 +69,7 @@ export default async function populate() {
` `
const populateComments = ` const populateComments = `
INSERT INTO recipin.cmp_recipecomments INSERT INTO recipin.recipecomments
(commentbody, datecreated, recipeid, authorid) (commentbody, datecreated, recipeid, authorid)
VALUES VALUES
('Very cool recipe!', $1, 2, 2), ('Very cool recipe!', $1, 2, 2),
@@ -90,8 +90,9 @@ export default async function populate() {
try { try {
await pool.query(s, [now]); await pool.query(s, [now]);
} catch(e: any) { } catch(e: any) {
console.error(e);
console.log('Last executed: ' + s); console.log('Last executed: ' + s);
throw new Error(e); process.exit(0);
} }
} }

View File

@@ -1,6 +1,6 @@
import { Client } from 'pg'; import { Client } from 'pg';
import dotenv from 'dotenv'; import dotenv from 'dotenv';
import populate from "./examplevals"; import populate from "./populate";
import pool from "."; import pool from ".";
dotenv.config(); dotenv.config();
@@ -73,7 +73,7 @@ dotenv.config();
` `
const recipecomments = ` const recipecomments = `
CREATE TABLE IF NOT EXISTS recipin.cmp_recipecomments ( CREATE TABLE IF NOT EXISTS recipin.recipecomments (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
commentbody varchar NOT NULL, commentbody varchar NOT NULL,
datecreated varchar NOT NULL, datecreated varchar NOT NULL,
@@ -122,7 +122,8 @@ dotenv.config();
await pool.query(s); await pool.query(s);
} }
} catch(e: any) { } catch(e: any) {
throw new Error(e); console.error(e);
process.exit(0);
} }
console.log("Database seed successful. Attempting to populate..."); console.log("Database seed successful. Attempting to populate...");