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";
axios.defaults.withCredentials = true;
axios.defaults.headers['Content-Type'] = 'application/json';
export const getBaseAPI = async () => {
return fetch(API);
@@ -30,11 +31,9 @@ export const checkCredientials = async () => {
url: API + '/auth',
});
const data = Promise.resolve(response.data);
return data;
return Promise.resolve(response.data);
} catch (e: any) {
throw e;
console.log(e);
}
}
@@ -47,29 +46,47 @@ export const attemptLogout = async () => {
} catch (e: any) {
throw e;
}
// const result = await fetch(API + 'auth/logout', { method: "DELETE" }).then(response => response.json());
// return result;
}
export const attemptRegister = async (data: IUser) => {
const result = await fetch(API + 'auth/register/', {
export const attemptRegister = async (body: IUser) => {
try {
const response = await axios({
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
}).then(response => response.json());
return result;
url: API + '/auth/register',
data: JSON.stringify(body)
})
return Promise.resolve(response.data);
} catch (e: any) {
throw e;
}
}
// on recipe route
export const getRecipeByID = async (id: string | number) => {
const result = await fetch(API + 'recipe/' + id).then(response => response.json());
return result;
try {
const response = await axios({
method: "GET",
url: API + '/recipe/' + id
})
return Promise.resolve(response.data);
} catch (e: any) {
throw e;
}
}
export const getAllRecipes = async () => {
const result = await fetch(API + 'recipe').then(response => response.json());
return result;
try {
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 = `
INSERT INTO recipin.cmp_recipecomments
INSERT INTO recipin.recipecomments
(commentbody, datecreated, recipeid, authorid)
VALUES
('Very cool recipe!', $1, 2, 2),
@@ -90,8 +90,9 @@ export default async function populate() {
try {
await pool.query(s, [now]);
} catch(e: any) {
console.error(e);
console.log('Last executed: ' + s);
throw new Error(e);
process.exit(0);
}
}

View File

@@ -1,6 +1,6 @@
import { Client } from 'pg';
import dotenv from 'dotenv';
import populate from "./examplevals";
import populate from "./populate";
import pool from ".";
dotenv.config();
@@ -73,7 +73,7 @@ dotenv.config();
`
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,
commentbody varchar NOT NULL,
datecreated varchar NOT NULL,
@@ -122,7 +122,8 @@ dotenv.config();
await pool.query(s);
}
} catch(e: any) {
throw new Error(e);
console.error(e);
process.exit(0);
}
console.log("Database seed successful. Attempting to populate...");