Skip to content

Commit

Permalink
Merge pull request #38 from gla6-fp-team1/DB-1_and_AUTH-4-1
Browse files Browse the repository at this point in the history
#27 and #34- store each suggestion with unique id and alter sentences table - Appolin Semegni Fotso
  • Loading branch information
sztupy authored Oct 12, 2023
2 parents c31e210 + dd4d0f4 commit eaf8fde
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
12 changes: 7 additions & 5 deletions client/src/pages/SubmitSuggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { useState } from "react";
const SubmitSuggestion = (props) => {
const [messageAfterPost, setMessageAfterPost] = useState("");
const submitButton = () => {
const data = new URLSearchParams();
data.append("sentence", props.randomText);
data.append("suggestions", props.suggestionsText);
data.append("selectedSuggestion", props.selectedSuggestion);
const jsonData = {
sentence: props.randomText,
suggestions: props.suggestionsText,
selectedSuggestion: props.selectedSuggestion,
};

fetch("/api/save-suggestions", {
method: "POST",
body: data,
body: JSON.stringify(jsonData),
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
})
Expand Down
41 changes: 20 additions & 21 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@ import db from "./db";

const router = Router();

router.get("/", (_, res) => {
router.get("/", async(_, res) => {
logger.debug("Welcoming everyone...");
const text = [
"Bhiodh iad a' dèanamh móran uisge-bheatha bho chionn fhada ro n a latha againn.",
"Tha iad ris an Gearrloch fhathast.",
"Well, chuala mi gun robh iad ris an Gearrloch a sin.",
"Bha iad a' faicinn bòcan shios ann an Arasaig.",
"agus 's e a'chiad rud a bha iad a' faicinn dhe'n rud colainn gun cheann a' falbh feadh siod.",
"agus bha bodach a' fuireach ann a' sabhall.",
"Chuir iad duine ann a' sabhall a dh' fhuireach oidhche.",
"agus chunnaic e am bòcan a bha seo agus an bòcan bhitheadh aige.",
"agus tha obair uamhasach aige mun d' fhuair e am bòcan a chumail bhuaidh.",
"agus bha coltas air gun deànadh am bòcan an gróthach air.",
"Cha robh buille a bhuaileadh e air nach robh e a' smaoineach' gur h- ann air póca cloìmh leis cho bog 's a bha a' bhuille is cha robh i a' dèanadh feum.",
];

const getRandomIndex = (length) => {
return Math.floor(Math.random() * length);
};
res.json(text[getRandomIndex(text.length)]);
// Select sentences from sentences table
const sentences = await db.query(
"SELECT sentence FROM sentences"
);
const randomSentences = [];
for (let sentence of sentences.rows) {
randomSentences.push(sentence.sentence);
}
// const randomSentence = sentences.rows[getRandomIndex(sentences.rows.length)];
res.json(randomSentences[getRandomIndex(randomSentences.length)]);
});

router.post("/save-suggestions", async (req, res) => {
Expand All @@ -33,18 +30,20 @@ router.post("/save-suggestions", async (req, res) => {
try {
// Check if all data exist in req.body
if (sentence && suggestions && selectedSuggestion) {
// Insert the sentence into the sentences table
// Select id from sentences table
const sentenceResult = await db.query(
"INSERT INTO sentences (sentence) VALUES ($1) RETURNING id",
"SELECT id FROM sentences WHERE sentence = $1",
[sentence]
);
const sentenceId = sentenceResult.rows[0].id;

// Insert the suggestions into the suggestions table
await db.query(
"INSERT INTO suggestions (sentence_id, suggestion) VALUES ($1, $2)",
[sentenceId, suggestions]
);
for (const suggestion of suggestions) {
await db.query(
"INSERT INTO suggestions (sentence_id, suggestion) VALUES ($1, $2)",
[sentenceId, suggestion]
);
}
// for (const suggestion of suggestions) {
// }
// Insert selected suggestion to user_interactions table
Expand Down
42 changes: 42 additions & 0 deletions server/db-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

/* gla6-fp-team1 week 1 Investigate the database schema ticket*/

sentences {
id INT PK,
sentence TEXT,
source TEXT,
count INT

}

sentences--0NE ||--MANY{ suggesions : have}

suggesions {
id INT PK,
sentence_id INT REFERENCES sentences(id),
suggestion TEXT
}
suggesions --ONE || --ONE { sentence : correspond to}
user_interactions{
id SERIAL PRIMARY KEY,
sentence_id INTEGER REFERENCES sentences(id),
selected_suggestion TEXT,
user_provided_suggestion TEXT
}
user_interactions --ONE || ONE {sentence : correspond to}
CREATE TABLE sentences (
id SERIAL PRIMARY KEY,
sentence TEXT
);
CREATE TABLE suggestions (
id SERIAL PRIMARY KEY,
sentence_id INTEGER REFERENCES sentences(id),
suggestion TEXT
);
CREATE TABLE user_interactions (
id SERIAL PRIMARY KEY,
sentence_id INTEGER REFERENCES sentences(id),
selected_suggestion TEXT,
user_provided_suggestion TEXT
);

0 comments on commit eaf8fde

Please sign in to comment.