diff --git a/all/node-postgres/connect.js b/all/node-postgres/connect.js new file mode 100644 index 0000000..1889932 --- /dev/null +++ b/all/node-postgres/connect.js @@ -0,0 +1,15 @@ +const { request } = require('express'); +var config = require('./config.json'); + +const Pool = require('pg').Pool +const pool = new Pool({ + user: config.user, + host: config.host, + database: config.database, + password: config.password, + port: config.port +}) + +module.exports = { + pool +} \ No newline at end of file diff --git a/all/node-postgres/index.js b/all/node-postgres/index.js index c8659df..e5e9a3c 100644 --- a/all/node-postgres/index.js +++ b/all/node-postgres/index.js @@ -102,7 +102,7 @@ app.put('/teacher/upd/:id', (req, res) => { // SUBJECT------------------------------------- app.post('/subject', (req, res) => { - subject.create(req.body.name, req.body.hours) + subject.create(req.body.name, req.body.hours_count) .then(response => res.status(200).send(response)) .catch(error => res.status(500).send(error)) }) @@ -120,7 +120,7 @@ app.delete('/subject/del/:id', (req, res) => { }) app.put('/subject/upd/:id', (req, res) => { - subject.update(req.params.id, req.body.name, req.body.hours) + subject.update(req.params.id, req.body.name, req.body.hours_count) .then(response => res.status(200).send(response)) .catch(error => res.status(500).send(error)) }) @@ -128,7 +128,7 @@ app.put('/subject/upd/:id', (req, res) => { // DIRECTION------------------------------------- app.post('/direction', (req, res) => { - direction.create(req.body.name, req.body.facultyId) + direction.create(req.body.name, req.body.faculty_id) .then(response => res.status(200).send(response)) .catch(error => res.status(500).send(error)) }) @@ -154,7 +154,7 @@ app.put('/direction/upd/:id', (req, res) => { // STUDENT------------------------------------- app.post('/student', (req, res) => { - student.create(req.params.name, req.params.surname, req.params.book, req.params.directionId) + student.create(req.body.name, req.body.surname, req.body.record_book, req.body.direction_id) .then(response => res.status(200).send(response)) .catch(error => res.status(500).send(error)) }) @@ -172,7 +172,7 @@ app.delete('/student/del/:id', (req, res) => { }) app.put('/student/upd/:id', (req, res) => { - student.update(req.params.id, req.params.name, req.params.surname, req.params.book, req.params.directionId) + student.update(req.params.id, req.body.name, req.body.surname, req.body.record_book, req.body.direction_id) .then(response => res.status(200).send(response)) .catch(error => res.status(500).send(error)) }) @@ -180,7 +180,7 @@ app.put('/student/upd/:id', (req, res) => { // SCORE------------------------------------- app.post('/score', (req, res) => { - score.create(req.body.value, req.body.subjectId, req.body.testId, req.body.teacherId, req.body.studentId) + score.create(req.body.value, req.body.subject_id, req.body.test_type_id, req.body.teacher_id, req.body.student_id) .then(response => res.status(200).send(response)) .catch(error => res.status(500).send(error)) }) diff --git a/all/node-postgres/models/direction.js b/all/node-postgres/models/direction.js index cb6aeee..cc403b4 100644 --- a/all/node-postgres/models/direction.js +++ b/all/node-postgres/models/direction.js @@ -1,14 +1,5 @@ const { request } = require('express'); -var config = require('../config.json'); - -const Pool = require('pg').Pool -const pool = new Pool({ - user: config.user, - host: config.host, - database: config.database, - password: config.password, - port: config.port -}) +const pool = require('../connect').pool const create = (nameF, facultyId) => { return new Promise((resolve, reject) => { @@ -43,7 +34,8 @@ const update = (id, name, faculty_id) => { const get = () => { return new Promise(function(resolve, reject) { - pool.query('SELECT * FROM direction', (error, results) => { + pool.query('select d.id, d."name", d.faculty_id, f."name" as faculty_name from direction d, faculty f where d.faculty_id = f.id', + (error, results) => { if (error) { reject(error) } diff --git a/all/node-postgres/models/score.js b/all/node-postgres/models/score.js index 4e94b9a..60194b0 100644 --- a/all/node-postgres/models/score.js +++ b/all/node-postgres/models/score.js @@ -1,14 +1,5 @@ const { request } = require('express'); -var config = require('../config.json'); - -const Pool = require('pg').Pool -const pool = new Pool({ - user: config.user, - host: config.host, - database: config.database, - password: config.password, - port: config.port -}) +const pool = require('../connect').pool const create = (value, subject_id, test_type_id, teacher_id, student_id) => { return new Promise((resolve, reject) => { @@ -43,15 +34,18 @@ const update = (id, value, subject_id, test_type_id, teacher_id, student_id) => } const get = () => { - return new Promise(function(resolve, reject) { - pool.query('SELECT * FROM direction', (error, results) => { - if (error) { - reject(error) - } - resolve(results.rows) - }) - }) - } + const a = 'select s.*, s2."name" as student_name, s2.surname as student_surname, s2.surname as student_surname, s3."name" as subject_name, t."name" as teacher_name, t.surname as teacher_surname, tt."name" as test_type_name from score s ' + const b = 'join student s2 on s.student_id = s2.id join subject s3 on s.subject_id = s3.id join teacher t on s.teacher_id = t.id join test_type tt on s.test_type_id = tt.id' + const res = a + b + return new Promise(function(resolve, reject) { + pool.query(res, (error, results) => { + if (error) { + reject(error) + } + resolve(results.rows) + }) + }) +} module.exports = { create, diff --git a/all/node-postgres/models/student.js b/all/node-postgres/models/student.js index 8c3357a..48904d0 100644 --- a/all/node-postgres/models/student.js +++ b/all/node-postgres/models/student.js @@ -1,14 +1,5 @@ const { request } = require('express'); -var config = require('../config.json'); - -const Pool = require('pg').Pool -const pool = new Pool({ - user: config.user, - host: config.host, - database: config.database, - password: config.password, - port: config.port -}) +const pool = require('../connect').pool const create = (name, surname, book, directionId) => { return new Promise((resolve, reject) => { @@ -44,7 +35,8 @@ const update = (id, name, surname, book, directionId) => { const get = () => { return new Promise(function(resolve, reject) { - pool.query('SELECT * FROM student', (error, results) => { + pool.query('select s.id, s."name", s.surname, s.record_book, s.direction_id, d."name" as direction_name from student s , direction d where s.direction_id = d.id ', + (error, results) => { if (error) { reject(error) } diff --git a/all/node-postgres/models/subject.js b/all/node-postgres/models/subject.js index cb7801c..122a4bd 100644 --- a/all/node-postgres/models/subject.js +++ b/all/node-postgres/models/subject.js @@ -1,14 +1,5 @@ const { request } = require('express'); -var config = require('../config.json'); - -const Pool = require('pg').Pool -const pool = new Pool({ - user: config.user, - host: config.host, - database: config.database, - password: config.password, - port: config.port -}) +const pool = require('../connect').pool const create = (name, hours) => { return new Promise((resolve, reject) => { diff --git a/all/node-postgres/models/teacher.js b/all/node-postgres/models/teacher.js index 4fde851..b147394 100644 --- a/all/node-postgres/models/teacher.js +++ b/all/node-postgres/models/teacher.js @@ -1,14 +1,6 @@ const { request } = require('express'); -var config = require('../config.json'); +const pool = require('../connect').pool -const Pool = require('pg').Pool -const pool = new Pool({ - user: config.user, - host: config.host, - database: config.database, - password: config.password, - port: config.port -}) const create = (name, surname, post) => { return new Promise((resolve, reject) => { diff --git a/all/react-postgres/src/App.js b/all/react-postgres/src/App.js index faef14d..81453cb 100644 --- a/all/react-postgres/src/App.js +++ b/all/react-postgres/src/App.js @@ -5,6 +5,9 @@ import FacultyPage from "./pages/FacultyPage"; import TestPage from './pages/TestPage'; import TeacherPage from './pages/TeacherPage'; import SubjectPage from './pages/SubjectPage'; +import DirectionPage from './pages/DirectionPage'; +import StudentPage from './pages/StudentPage'; +import ScorePage from './pages/ScorePage'; function App() { return ( @@ -16,6 +19,9 @@ function App() { } /> } /> } /> + } /> + } /> + } /> ); diff --git a/all/react-postgres/src/models/Direction.js b/all/react-postgres/src/models/Direction.js index e69de29..71fba72 100644 --- a/all/react-postgres/src/models/Direction.js +++ b/all/react-postgres/src/models/Direction.js @@ -0,0 +1,8 @@ +export default class Direction { + constructor(data) { + this.id = data?.id; + this.name = data?.name || ''; + this.faculty_id = data?.faculty_id || ''; + this.faculty_name = data?.faculty_name || ''; + } +} \ No newline at end of file diff --git a/all/react-postgres/src/models/Score.js b/all/react-postgres/src/models/Score.js new file mode 100644 index 0000000..655f44d --- /dev/null +++ b/all/react-postgres/src/models/Score.js @@ -0,0 +1,16 @@ +export default class Score { + constructor(data) { + this.id = data?.id; + this.value = data?.value || ''; + this.subject_id = data?.subject_id || ''; + this.subject_name = data?.subject_name || ''; + this.test_type_id = data?.test_type_id || ''; + this.test_type_name = data?.test_type_name || ''; + this.teacher_id = data?.teacher_id || ''; + this.teacher_name = data?.teacher_name || ''; + this.teacher_surname = data?.teacher_surname || ''; + this.student_id = data?.student_id || ''; + this.student_name = data?.student_name || ''; + this.student_surname = data?.student_surname || ''; + } +} \ No newline at end of file diff --git a/all/react-postgres/src/models/Student.js b/all/react-postgres/src/models/Student.js new file mode 100644 index 0000000..0252155 --- /dev/null +++ b/all/react-postgres/src/models/Student.js @@ -0,0 +1,10 @@ +export default class Student { + constructor(data) { + this.id = data?.id; + this.name = data?.name || ''; + this.surname = data?.surname || ''; + this.record_book = data?.record_book || ''; + this.direction_id = data?.direction_id || ''; + this.direction_name = data?.direction_name || ''; + } +} \ No newline at end of file diff --git a/all/react-postgres/src/pages/DirectionPage.jsx b/all/react-postgres/src/pages/DirectionPage.jsx new file mode 100644 index 0000000..b926db0 --- /dev/null +++ b/all/react-postgres/src/pages/DirectionPage.jsx @@ -0,0 +1,151 @@ +import React, {useState, useEffect} from 'react'; +import Modal from '../common/Modal'; +import Direction from '../models/Direction'; + + +export default function DirectionPage() { + const [items, setItems] = useState([]); + const [item, setItem] = useState(new Direction()) + + const [faculties, setFaculties] = useState([]) + + const [modalHeader, setModalHeader] = useState('') + const [modalConfirm, setModalConfirm] = useState('') + const [modalVisible, setModalVisible] = useState(false) + const [isEdit, setEdit] = useState(false) + + useEffect(() => { + get() + //eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + function save(){ + if(isEdit) upd() + else create() + get() + } + + function get(){ + fetch('http://localhost:3001/direction') + .then(response => {return response.json()}) + .then(data => setItems(data)) + + fetch('http://localhost:3001/faculty') + .then(response => {return response.json()}) + .then(data => setFaculties(data)) + } + + function create(){ + fetch('http://localhost:3001/direction', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function upd(){ + fetch(`http://localhost:3001/direction/upd/${item.id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function del(id){ + fetch(`http://localhost:3001/direction/del/${id}`, { + method: 'DELETE' + }) + .then(response => get()) + } + + function handleCreate(){ + setEdit(false) + setModalHeader('Create'); + setModalConfirm('Add'); + setModalVisible(true); + } + + function handleFormChange(event) { + setItem(prevState => ({ ...prevState, [event.target.id]: event.target.value })) + } + + function handleEdit(data){ + setItem(data) + setEdit(true) + setModalHeader('Update'); + setModalConfirm('Save'); + setModalVisible(true); + } + + const hideModal = () => setModalVisible(false) + const modalDone = () => save() + + return ( + <> + +
+ + +
+ +
+

Faculty

+ +
+
+ +
+ + + + + + + + + + + + + + { + items.map((data, index) => + + + + + + + ) + } + +
#namefaculty
{index + 1}{data.name}{data.faculty_name} +
+ + +
+
+
+ + ); +} \ No newline at end of file diff --git a/all/react-postgres/src/pages/FacultyPage.jsx b/all/react-postgres/src/pages/FacultyPage.jsx index 3fca3e5..a48c012 100644 --- a/all/react-postgres/src/pages/FacultyPage.jsx +++ b/all/react-postgres/src/pages/FacultyPage.jsx @@ -30,13 +30,12 @@ export default function FacultyPage() { } function create(){ - const name = item.name fetch('http://localhost:3001/faculty', { method: 'POST', headers: { 'Content-Type': 'application/json', }, - body: JSON.stringify({name}) + body: JSON.stringify(item) }) .then(response => get()) } diff --git a/all/react-postgres/src/pages/ScorePage.jsx b/all/react-postgres/src/pages/ScorePage.jsx new file mode 100644 index 0000000..074bdaf --- /dev/null +++ b/all/react-postgres/src/pages/ScorePage.jsx @@ -0,0 +1,212 @@ +import React, {useState, useEffect} from 'react'; +import Modal from '../common/Modal'; +import Score from '../models/Score'; + + +export default function ScorePage() { + const [items, setItems] = useState([]); + const [item, setItem] = useState(new Score()) + + const [subjects, setSubjects] = useState([]) + const [tests, setTests] = useState([]) + const [teachers, setTeachers] = useState([]) + const [students, setStudents] = useState([]) + + const [modalHeader, setModalHeader] = useState('') + const [modalConfirm, setModalConfirm] = useState('') + const [modalVisible, setModalVisible] = useState(false) + const [isEdit, setEdit] = useState(false) + + useEffect(() => { + get() + //eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + function save(){ + if(isEdit) upd() + else create() + get() + } + + function get(){ + fetch('http://localhost:3001/score') + .then(response => {return response.json()}) + .then(data => setItems(data)) + + fetch('http://localhost:3001/subject') + .then(response => {return response.json()}) + .then(data => setSubjects(data)) + + fetch('http://localhost:3001/test') + .then(response => {return response.json()}) + .then(data => setTests(data)) + + fetch('http://localhost:3001/teacher') + .then(response => {return response.json()}) + .then(data => setTeachers(data)) + + fetch('http://localhost:3001/student') + .then(response => {return response.json()}) + .then(data => setStudents(data)) + } + + function create(){ + console.log(item) + fetch('http://localhost:3001/score', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function upd(){ + fetch(`http://localhost:3001/score/upd/${item.id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function del(id){ + fetch(`http://localhost:3001/score/del/${id}`, { + method: 'DELETE' + }) + .then(response => get()) + } + + function handleCreate(){ + setEdit(false) + setModalHeader('Create'); + setModalConfirm('Add'); + setModalVisible(true); + } + + function handleFormChange(event) { + setItem(prevState => ({ ...prevState, [event.target.id]: event.target.value })) + } + + function handleEdit(data){ + setItem(data) + setEdit(true) + setModalHeader('Update'); + setModalConfirm('Save'); + setModalVisible(true); + } + + const hideModal = () => setModalVisible(false) + const modalDone = () => save() + + return ( + <> + +
+ + +
+ +
+

Subject

+ +
+ +
+

Test type

+ +
+ +
+

Teacher

+ +
+ +
+

Student

+ +
+
+ +
+ + + + + + + + + + + + + + + + + { + items.map((data, index) => + + + + + + + + + + ) + } + +
#test typevaluestudentsubjectteacher
{index + 1}{data.test_type_name}{data.value}{data.student_name} {data.student_surname}{data.subject_name}{data.teacher_name} {data.teacher_surname} +
+ + +
+
+
+ + ); +} \ No newline at end of file diff --git a/all/react-postgres/src/pages/StudentPage.jsx b/all/react-postgres/src/pages/StudentPage.jsx new file mode 100644 index 0000000..b424558 --- /dev/null +++ b/all/react-postgres/src/pages/StudentPage.jsx @@ -0,0 +1,168 @@ +import React, {useState, useEffect} from 'react'; +import Modal from '../common/Modal'; +import Student from '../models/Student'; + + +export default function StudentPage() { + const [items, setItems] = useState([]); + const [item, setItem] = useState(new Student()) + + const [directions, setDirections] = useState([]) + + const [modalHeader, setModalHeader] = useState('') + const [modalConfirm, setModalConfirm] = useState('') + const [modalVisible, setModalVisible] = useState(false) + const [isEdit, setEdit] = useState(false) + + useEffect(() => { + get() + //eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + function save(){ + if(isEdit) upd() + else create() + get() + } + + function get(){ + fetch('http://localhost:3001/student') + .then(response => {return response.json()}) + .then(data => setItems(data)) + + fetch('http://localhost:3001/direction') + .then(response => {return response.json()}) + .then(data => setDirections(data)) + } + + function create(){ + console.log(item) + fetch('http://localhost:3001/student', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function upd(){ + fetch(`http://localhost:3001/student/upd/${item.id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function del(id){ + fetch(`http://localhost:3001/student/del/${id}`, { + method: 'DELETE' + }) + .then(response => get()) + } + + function handleCreate(){ + setEdit(false) + setModalHeader('Create'); + setModalConfirm('Add'); + setModalVisible(true); + } + + function handleFormChange(event) { + setItem(prevState => ({ ...prevState, [event.target.id]: event.target.value })) + } + + function handleEdit(data){ + setItem(data) + setEdit(true) + setModalHeader('Update'); + setModalConfirm('Save'); + setModalVisible(true); + } + + const hideModal = () => setModalVisible(false) + const modalDone = () => save() + + return ( + <> + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+

Direction

+ +
+
+ +
+ + + + + + + + + + + + + + + + { + items.map((data, index) => + + + + + + + + + ) + } + +
#namesurnamerecord bookdirection
{index + 1}{data.name}{data.surname}{data.record_book}{data.direction_name} +
+ + +
+
+
+ + ); +} \ No newline at end of file diff --git a/all/react-postgres/src/pages/SubjectPage.jsx b/all/react-postgres/src/pages/SubjectPage.jsx index 2d1aa92..60ed4b1 100644 --- a/all/react-postgres/src/pages/SubjectPage.jsx +++ b/all/react-postgres/src/pages/SubjectPage.jsx @@ -33,6 +33,7 @@ export default function SubjectPage() { } function create(){ + console.log(item) fetch('http://localhost:3001/subject', { method: 'POST', headers: { @@ -69,7 +70,7 @@ export default function SubjectPage() { } function handleFormChange(event) { - setItem({ ...item, [event.target.id]: event.target.value }) + setItem(prevState => ({ ...prevState, [event.target.id]: event.target.value })) } function handleEdit(data){ @@ -98,8 +99,8 @@ export default function SubjectPage() {
- - Hours +
diff --git a/all/react-postgres/src/pages/TeacherPage.jsx b/all/react-postgres/src/pages/TeacherPage.jsx index f4806b1..bc8fe8d 100644 --- a/all/react-postgres/src/pages/TeacherPage.jsx +++ b/all/react-postgres/src/pages/TeacherPage.jsx @@ -66,7 +66,8 @@ export default function TeacherPage() { } function handleFormChange(event) { - setItem({ ...item, [event.target.id]: event.target.value }) + console.log(event.target.id) + setItem(prevState => ({ ...prevState, [event.target.id]: event.target.value })) } function handleEdit(data){