From 1ca82f336811fb4717144f976571385a7f931b59 Mon Sep 17 00:00:00 2001 From: Inohara Date: Sat, 6 May 2023 10:39:09 +0400 Subject: [PATCH] more --- all/node-postgres/index.js | 8 +- all/node-postgres/models/teacher.js | 4 +- all/react-postgres/src/App.js | 6 + all/react-postgres/src/common/Header.jsx | 36 +++++ all/react-postgres/src/common/Page.jsx | 136 +++++++++++++++++ all/react-postgres/src/models/Direction.js | 0 all/react-postgres/src/models/Subject.js | 7 + all/react-postgres/src/models/Teacher.js | 8 + all/react-postgres/src/models/Test.js | 6 + all/react-postgres/src/pages/SubjectPage.jsx | 141 ++++++++++++++++++ all/react-postgres/src/pages/TeacherPage.jsx | 146 +++++++++++++++++++ all/react-postgres/src/pages/TestPage.jsx | 130 +++++++++++++++++ 12 files changed, 622 insertions(+), 6 deletions(-) create mode 100644 all/react-postgres/src/common/Page.jsx create mode 100644 all/react-postgres/src/models/Direction.js create mode 100644 all/react-postgres/src/models/Subject.js create mode 100644 all/react-postgres/src/models/Teacher.js create mode 100644 all/react-postgres/src/models/Test.js create mode 100644 all/react-postgres/src/pages/SubjectPage.jsx create mode 100644 all/react-postgres/src/pages/TeacherPage.jsx create mode 100644 all/react-postgres/src/pages/TestPage.jsx diff --git a/all/node-postgres/index.js b/all/node-postgres/index.js index 64433c9..c8659df 100644 --- a/all/node-postgres/index.js +++ b/all/node-postgres/index.js @@ -154,7 +154,7 @@ app.put('/direction/upd/:id', (req, res) => { // STUDENT------------------------------------- app.post('/student', (req, res) => { - student.create() + student.create(req.params.name, req.params.surname, req.params.book, req.params.directionId) .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.directionId) + student.update(req.params.id, req.params.name, req.params.surname, req.params.book, req.params.directionId) .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.stidentId) + score.create(req.body.value, req.body.subjectId, req.body.testId, req.body.teacherId, req.body.studentId) .then(response => res.status(200).send(response)) .catch(error => res.status(500).send(error)) }) @@ -198,7 +198,7 @@ app.delete('/score/del/:id', (req, res) => { }) app.put('/score/upd/:id', (req, res) => { - score.update(req.params.id, req.body.value, req.body.subjectId, req.body.testId, req.body.teacherId, req.body.stidentId) + score.update(req.params.id, req.body.value, req.body.subjectId, req.body.testId, req.body.teacherId, req.body.studentId) .then(response => res.status(200).send(response)) .catch(error => res.status(500).send(error)) }) diff --git a/all/node-postgres/models/teacher.js b/all/node-postgres/models/teacher.js index 4c3ce96..4fde851 100644 --- a/all/node-postgres/models/teacher.js +++ b/all/node-postgres/models/teacher.js @@ -26,7 +26,7 @@ const del = (id) => { pool.query('DELETE FROM teacher WHERE id = $1', [id], (error, result) => { if(error) reject(error) - resolve(result.rows) + resolve(result) }) }) } @@ -36,7 +36,7 @@ const update = (id, name, surname, post) => { pool.query('UPDATE teacher SET name = $1, surname = $2, post = $3 WHERE id = $4', [name, surname, post, id], (error, result) => { if(error) reject(error) - resolve(result.rows) + resolve(result) }) }) } diff --git a/all/react-postgres/src/App.js b/all/react-postgres/src/App.js index 98db629..faef14d 100644 --- a/all/react-postgres/src/App.js +++ b/all/react-postgres/src/App.js @@ -2,6 +2,9 @@ import React from 'react'; import { Routes,Route } from "react-router-dom"; import Header from "./common/Header"; import FacultyPage from "./pages/FacultyPage"; +import TestPage from './pages/TestPage'; +import TeacherPage from './pages/TeacherPage'; +import SubjectPage from './pages/SubjectPage'; function App() { return ( @@ -10,6 +13,9 @@ function App() { } /> } /> + } /> + } /> + } /> ); diff --git a/all/react-postgres/src/common/Header.jsx b/all/react-postgres/src/common/Header.jsx index f8c3281..334d725 100644 --- a/all/react-postgres/src/common/Header.jsx +++ b/all/react-postgres/src/common/Header.jsx @@ -18,6 +18,42 @@ export default function Header() { FACULTY + +
  • + + TEST + +
  • + +
  • + + TEACHER + +
  • + +
  • + + SUBJECT + +
  • + +
  • + + DIRECTION + +
  • + +
  • + + STUDENT + +
  • + +
  • + + SCORE + +
  • diff --git a/all/react-postgres/src/common/Page.jsx b/all/react-postgres/src/common/Page.jsx new file mode 100644 index 0000000..ccdaae8 --- /dev/null +++ b/all/react-postgres/src/common/Page.jsx @@ -0,0 +1,136 @@ +import React, {useState, useEffect} from 'react'; +import Modal from '../common/Modal'; + +export default function Page(props) { + const [items, setItems] = useState([]); + const [item, setItem] = useState(props.empty) + + 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/${props.name}`) + .then(response => {return response.json()}) + .then(data => setItems(data)) + } + + function create(){ + fetch(`http://localhost:3001/${props.name}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function upd(){ + fetch(`http://localhost:3001/${props.name}/upd/${item.id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function del(id){ + fetch(`http://localhost:3001/${props.name}/del/${id}`, { + method: 'DELETE' + }) + .then(response => get()) + } + + function handleCreate(){ + setEdit(false) + setModalHeader('Create'); + setModalConfirm('Add'); + setModalVisible(true); + } + + function handleFormChange(event) { + setItem({ ...item, [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 ( + <> + + {props.children} + + +
    + + + + + + + { + props.headers.map(header => + + ) + } + + + + + + + { + items.map((data, index) => + + + { + props.headers.map(header => + + ) + } + + + + ) + } + +
    # + {header.label} +
    {index + 1}{data[header.name]} +
    + + +
    +
    +
    + + ); +} \ No newline at end of file diff --git a/all/react-postgres/src/models/Direction.js b/all/react-postgres/src/models/Direction.js new file mode 100644 index 0000000..e69de29 diff --git a/all/react-postgres/src/models/Subject.js b/all/react-postgres/src/models/Subject.js new file mode 100644 index 0000000..f99785f --- /dev/null +++ b/all/react-postgres/src/models/Subject.js @@ -0,0 +1,7 @@ +export default class Subject { + constructor(data) { + this.id = data?.id; + this.name = data?.name || ''; + this.hours_count = data?.hours_count || ''; + } +} \ No newline at end of file diff --git a/all/react-postgres/src/models/Teacher.js b/all/react-postgres/src/models/Teacher.js new file mode 100644 index 0000000..904910e --- /dev/null +++ b/all/react-postgres/src/models/Teacher.js @@ -0,0 +1,8 @@ +export default class Teacher { + constructor(data) { + this.id = data?.id; + this.name = data?.name || ''; + this.name = data?.surname || ''; + this.name = data?.post || ''; + } +} \ No newline at end of file diff --git a/all/react-postgres/src/models/Test.js b/all/react-postgres/src/models/Test.js new file mode 100644 index 0000000..fbf4c91 --- /dev/null +++ b/all/react-postgres/src/models/Test.js @@ -0,0 +1,6 @@ +export default class Test { + constructor(data) { + this.id = data?.id; + this.name = data?.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 new file mode 100644 index 0000000..2d1aa92 --- /dev/null +++ b/all/react-postgres/src/pages/SubjectPage.jsx @@ -0,0 +1,141 @@ +import React, {useState, useEffect} from 'react'; +import Modal from '../common/Modal'; +import Subject from '../models/Subject'; + + +export default function SubjectPage() { + const [items, setItems] = useState([]); + const [item, setItem] = useState(new Subject()) + + 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/subject') + .then(response => {return response.json()}) + .then(data =>{ + //console.log(data) + setItems(data) + } ) + } + + function create(){ + fetch('http://localhost:3001/subject', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function upd(){ + fetch(`http://localhost:3001/subject/upd/${item.id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function del(id){ + fetch(`http://localhost:3001/subject/del/${id}`, { + method: 'DELETE' + }) + .then(response => get()) + } + + function handleCreate(){ + setEdit(false) + setModalHeader('Create'); + setModalConfirm('Add'); + setModalVisible(true); + } + + function handleFormChange(event) { + setItem({ ...item, [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 ( + <> + +
    + + +
    + +
    + + +
    +
    + +
    + + + + + + + + + + + + + + { + items.map((data, index) => + + + + + + + ) + } + +
    #namehours
    {index + 1}{data.name}{data.hours_count} +
    + + +
    +
    +
    + + ); +} \ No newline at end of file diff --git a/all/react-postgres/src/pages/TeacherPage.jsx b/all/react-postgres/src/pages/TeacherPage.jsx new file mode 100644 index 0000000..f4806b1 --- /dev/null +++ b/all/react-postgres/src/pages/TeacherPage.jsx @@ -0,0 +1,146 @@ +import React, {useState, useEffect} from 'react'; +import Modal from '../common/Modal'; +import Teacher from '../models/Teacher'; + + +export default function TeacherPage() { + const [items, setItems] = useState([]); + const [item, setItem] = useState(new Teacher()) + + 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/teacher') + .then(response => {return response.json()}) + .then(data => setItems(data)) + } + + function create(){ + fetch('http://localhost:3001/teacher', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function upd(){ + fetch(`http://localhost:3001/teacher/upd/${item.id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function del(id){ + fetch(`http://localhost:3001/teacher/del/${id}`, { + method: 'DELETE' + }) + .then(response => get()) + } + + function handleCreate(){ + setEdit(false) + setModalHeader('Create'); + setModalConfirm('Add'); + setModalVisible(true); + } + + function handleFormChange(event) { + setItem({ ...item, [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 ( + <> + +
    + + +
    + +
    + + +
    + +
    + + +
    +
    + +
    + + + + + + + + + + + + + + + { + items.map((data, index) => + + + + + + + + ) + } + +
    #namesurnamepost
    {index + 1}{data.name}{data.surname}{data.post} +
    + + +
    +
    +
    + + ); +} \ No newline at end of file diff --git a/all/react-postgres/src/pages/TestPage.jsx b/all/react-postgres/src/pages/TestPage.jsx new file mode 100644 index 0000000..2159c77 --- /dev/null +++ b/all/react-postgres/src/pages/TestPage.jsx @@ -0,0 +1,130 @@ +import React, {useState, useEffect} from 'react'; +import Modal from '../common/Modal'; +import Test from '../models/Test'; + + +export default function TestPage() { + const [items, setItems] = useState([]); + const [item, setItem] = useState(new Test()) + + 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/test') + .then(response => {return response.json()}) + .then(data => setItems(data)) + } + + function create(){ + fetch('http://localhost:3001/test', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function upd(){ + fetch(`http://localhost:3001/test/upd/${item.id}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(item) + }) + .then(response => get()) + } + + function del(id){ + fetch(`http://localhost:3001/test/del/${id}`, { + method: 'DELETE' + }) + .then(response => get()) + } + + function handleCreate(){ + setEdit(false) + setModalHeader('Create'); + setModalConfirm('Add'); + setModalVisible(true); + } + + function handleFormChange(event) { + setItem({ ...item, [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 ( + <> + +
    + + +
    +
    + +
    + + + + + + + + + + + + + { + items.map((data, index) => + + + + + + ) + } + +
    #name
    {index + 1}{data.name} +
    + + +
    +
    +
    + + ); +} \ No newline at end of file