Posts Real Estate to the API

This commit is contained in:
Vazhin Tayeb 2021-01-17 13:59:50 +03:00
parent de8b920ea2
commit d96726d6e6
3 changed files with 45 additions and 58 deletions

View File

@ -1,33 +1,38 @@
const Pool = require('pg').Pool const Pool = require("pg").Pool;
const pool = new Pool({ const pool = new Pool({
user: 'postgres', user: "postgres",
host: 'localhost', host: "localhost",
database: 'real_estate', database: "real_estate",
password: 'root', password: "root",
port: 5432, port: 5432,
}); });
const getAllRealEstate = (request, response) => { const getAllRealEstate = (request, response) => {
pool.query('SELECT * FROM real_estate ORDER BY id DESC', (error, results) => { pool.query("SELECT * FROM real_estate ORDER BY id DESC", (error, results) => {
if (error) { if (error) {
throw error throw error;
} }
response.status(200).json(results.rows) response.status(200).json(results.rows);
}) });
}; };
// We don't need the citizen_upn here? ** Temporary **
const insertRealEstate = (request, response) => { const insertRealEstate = (request, response) => {
const { address, area } = request.body const { address, area, citizen_upn } = request.body;
pool.query('INSERT INTO real_estate (address, area) VALUES ($1, $2)', [address, area], (error, results) => { pool.query(
"INSERT INTO real_estate (address, area, citizen_upn) VALUES ($1, $2, $3)",
[address, area, citizen_upn],
(error, results) => {
if (error) { if (error) {
throw error throw error;
} }
response.status(201).send('done :)') response.status(201).send("done :)");
}) }
} );
};
module.exports = { module.exports = {
getAllRealEstate, getAllRealEstate,
insertRealEstate insertRealEstate,
} };

View File

@ -53,7 +53,7 @@ const Navbar = () => {
className="text-white font-inter font-semibold hover:text-white focus:text-white tracking-normal mr-20" className="text-white font-inter font-semibold hover:text-white focus:text-white tracking-normal mr-20"
style={{ fontSize: "1.335rem" }} style={{ fontSize: "1.335rem" }}
> >
Your Real Estates Your Real Estate
</Link> </Link>
</li> </li>
<li className="m-0"> <li className="m-0">
@ -62,7 +62,7 @@ const Navbar = () => {
className="text-white font-inter font-semibold hover:text-white focus:text-white tracking-normal mr-20" className="text-white font-inter font-semibold hover:text-white focus:text-white tracking-normal mr-20"
style={{ fontSize: "1.335rem" }} style={{ fontSize: "1.335rem" }}
> >
Register Real Estates Register Real Estate
</Link> </Link>
</li> </li>
<li <li

View File

@ -1,5 +1,6 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { useHistory } from "react-router-dom";
// Services // Services
import * as apiService from "../../services/apiService"; import * as apiService from "../../services/apiService";
@ -9,61 +10,42 @@ import Card from "./Card";
import Heading3 from "./Heading3"; import Heading3 from "./Heading3";
import Button from "./Button"; import Button from "./Button";
import InputText from "./inputs/InputText"; import InputText from "./inputs/InputText";
import InputSelect from "./inputs/InputSelect";
const VehicleRegisterForm = () => { const VehicleRegisterForm = () => {
const [model, setModel] = useState(""); const [address, setAddress] = useState("");
const [licensePlate, setlicensePlate] = useState(""); const [area, setArea] = useState("");
const [color, setColor] = useState("White");
const [type, setType] = useState(1);
const user = useSelector((state) => state.auth.user); const user = useSelector((state) => state.auth.user);
const history = useHistory();
async function registerVehicle() { async function registerVehicle() {
await apiService.registerVehicle( await apiService.registerVehicle(
{ model, licensePlate, color, type }, { address, area, citizen_upn: user.profile.sub },
user.access_token user.access_token
); );
// await getVehicles(); history.push("/");
} }
return ( return (
<Card styles={{ padding: "60px" }} classes="xl:w-2/5"> <Card styles={{ padding: "60px" }} classes="xl:w-2/5">
<form onSubmit={(e) => e.preventDefault()} className="m-0 flex flex-col"> <form onSubmit={(e) => e.preventDefault()} className="m-0 flex flex-col">
<Heading3 styles={{ marginTop: "0" }}>Register A Vehicle</Heading3> <Heading3 styles={{ marginTop: "0" }}>Register Real Estate</Heading3>
<div className="flex flex-col"> <div className="flex flex-col">
<InputText <InputText
label="Model" label="Address"
value={model} value={address}
name="model" name="address"
placeholder="Model" placeholder="Address"
onChangeEvent={(e) => setModel(e.target.value)} onChangeEvent={(e) => setAddress(e.target.value)}
/> />
<InputText <InputText
label="License Plate" label="Area"
value={licensePlate} value={area}
name="licensePlate" name="area"
placeholder="License Plate" placeholder="Area"
onChangeEvent={(e) => setlicensePlate(e.target.value)} onChangeEvent={(e) => setArea(e.target.value)}
/>
<InputText
label="Color"
value={color}
name="color"
placeholder="Color"
onChangeEvent={(e) => setColor(e.target.value)}
/>
<InputSelect
label="Type"
value={type}
name="type"
onChangeEvent={(e) => setType(e.target.value)}
options={[
{ value: "1", text: "Sedan" },
{ value: "2", text: "SUV" },
{ value: "3", text: "Pickup" },
]}
/> />
</div> </div>
<Button <Button