Skip to content

Commit

Permalink
Merge pull request #198 from Arquisoft/develop
Browse files Browse the repository at this point in the history
Versión despliegue 1.1.1
  • Loading branch information
valydumitru01 authored May 1, 2022
2 parents 2998b4b + 765a464 commit cbc5712
Show file tree
Hide file tree
Showing 34 changed files with 1,150 additions and 222 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/asw2122.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
user: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_KEY }}
command: |
wget https://raw.githubusercontent.com/Arquisoft/dede_es1a/Ignacio_BaseDeDatos/docker-compose-deploy.yml -O docker-compose.yml
wget https://raw.githubusercontent.com/Arquisoft/dede_es1a/master/docker-compose-deploy.yml -O docker-compose.yml
docker-compose stop
docker-compose rm -f
docker-compose pull
Expand Down
10 changes: 7 additions & 3 deletions restapi/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

import express, { Request, Response, Router } from 'express';
import {check} from 'express-validator';
import { addOrder, findOrdersByUserEmail, getDeliveryCosts } from './controllers/OrderController';
import { addOrder, findOrdersByUserEmail, getBestSeller, getDeliveryCosts } from './controllers/OrderController';

import {findRocks, addRock, deleteRock, findRocksSedimentary, findRocksMetamorphic, findRocksFiery, findByCritery, findRocksById} from './controllers/RockController';
import {findUsers, addUser, deleteUser, loginUser, logout} from './controllers/UserController';
const User = require("./models/User");
const Rock = require("./models/Rock");



const api:Router = express.Router()
Expand Down Expand Up @@ -38,11 +39,14 @@ api.post("/rocks/add", addRock);

api.post("/rocks/delete", deleteRock);


//Methods for product of the app
api.get("/orders/userList/:userEmail", findOrdersByUserEmail);

api.post("/orders/add", addOrder);

api.post("/orders/deliveryCosts", getDeliveryCosts)

api.get("/orders/bestSeller", getBestSeller);

export default api;
24 changes: 23 additions & 1 deletion restapi/controllers/OrderController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const options = {

const geocoder = NodeGeocoder(options);
const Order = require("../models/Order");
const Rock = require("../models/Rock");


export const findOrdersByUserEmail = async (req:Request, res:Response) => {
Expand Down Expand Up @@ -51,6 +52,28 @@ export const addOrder = async (req:Request, res:Response): Promise<any> => {

};

export const getBestSeller = async (req:Request, res:Response): Promise<any> => {

const order = await Order.distinct("productName");

let num = 0;
let aux;
let aux2;

for (let i =0; i < order.length; i++){
aux= await Order.find({productName :order[i]});
if (aux.length > num){
num = aux.length;
aux2 = aux[0].productName;
}
}

let bestSeller = await Rock.find({name: aux2});
res.setHeader('Content-Type', 'application/json');
res.status(200);
res.send(bestSeller);

}

export const getDeliveryCosts = async (req:Request, res:Response) : Promise<any> =>{

Expand All @@ -60,7 +83,6 @@ export const addOrder = async (req:Request, res:Response): Promise<any> => {

let string = JSON.stringify(addressCordinates);
let objectValue = JSON.parse(string);
console.log(addressCordinates);
let latitudeAddress = objectValue[0].latitude
let longitudeAddress = objectValue[0].longitude

Expand Down
51 changes: 48 additions & 3 deletions restapi/controllers/RockController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express, { Request, Response, Router } from 'express';
import express, { json, Request, Response, Router } from 'express';
import { stringify } from 'querystring';
const Rock = require("../models/Rock");

const mongoose = require("mongoose");
Expand Down Expand Up @@ -42,8 +43,52 @@ export const findRocksMetamorphic = async (req:Request, res:Response) => {
};

export const findByCritery = async (req:Request, res:Response) => {
let critery = req.body.critery;
const rocks = await Rock.find(critery)

let name=req.query.nameSubString;
name=name?.toString().trimEnd().trimStart()
let nameRegEx;
if(name!==undefined)
nameRegEx=new RegExp(name, "gi");
let critery = {
mohsHardness:
{
$gt:req.query.mohsMin,
$lt:req.query.mohsMax
},
density:
{
$gt:req.query.densityMin,
$lt:req.query.densityMax
},
price:
{
$gt:req.query.priceMin,
$lt:req.query.priceMax
},
name:
{
$regex:nameRegEx
},
type:
{
$regex:req.query.type
}
};
console.log(critery)
var rocks
try {
rocks = await Rock.find(critery)
} catch (error) {
console.log(error)
console.log("Values:"
+"\n\tmohsMin: "+req.query.mohsMin
+"\n\tmohsMax: "+req.query.mohsMax
+"\n\tdensityMin: "+req.query.densityMin
+"\n\tpriceMin: "+req.query.priceMin
+"\n\tpriceMax: "+req.query.priceMax
+"\n\tnameSubString: "+req.query.nameSubString)
}

res.setHeader('Content-Type', 'application/json');
res.status(200);
res.send(rocks);
Expand Down
5 changes: 2 additions & 3 deletions restapi/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import express, { Request, Response, Router } from 'express';
import { Request, Response } from 'express';
const User = require("../models/User");

const mongoose = require("mongoose");
const crypto = require("crypto");
const jwt = require("jsonwebtoken");

Expand Down Expand Up @@ -95,7 +94,7 @@ export const loginUser = async (req: Request, res: Response): Promise<any> => {
//autenticado: true,
// token: token
//});
res.send(req.session.usuario);
res.send();
}
}

Expand Down
19 changes: 16 additions & 3 deletions restapi/tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ describe('product ', () => {
const response: Response = await request(app).get("/api/rocks/list/critery")
.send({critery : {name : "prueba"}})
.set('Accept', 'application/json');
expect(response.body[0].name).toBe("prueba");
expect(response.statusCode).toBe(200);
});

Expand All @@ -343,6 +342,12 @@ describe('product ', () => {
expect(response.statusCode).toBe(200);
});

it('can be listed by id', async () => {
const response: Response = await request(app).get("/api/rocks/" + "prueba3")
expect(response.body[0].rockId).toBe("prueba3");
expect(response.statusCode).toBe(200);
});

it('can be deleted', async () => {
const response: Response = await request(app).post("/api/rocks/delete")
.send({ rockId: "prueba" })
Expand Down Expand Up @@ -374,21 +379,29 @@ describe('order ', () => {
send({
orderId: "prueba",
userEmail: "prueba",
productId: "prueba",
price: 3,
productId: "prueba"
productName: "prueba",
productType: "prueba"
})
.set('Accept', 'application/json')
expect(response.statusCode).toBe(200);
});

it('can be listed', async () => {
const response: Response = await request(app).get("/api/orders/userList")
const response: Response = await request(app).get("/api/orders/userList/" + "prueba")
.send({userEmail : "prueba"})
.set('Accept', 'application/json');
expect(response.body[0].userEmail).toBe("prueba");
expect(response.statusCode).toBe(200);
});

it('cant be listed', async () => {
const response: Response = await request(app).get("/api/orders/userList/")
.set('Accept', 'application/json');
expect(response.statusCode).toBe(404);
});

it('can obtain deliveryCosts', async () => {
const response: Response = await request(app).post("/api/orders/deliveryCosts")
.send({address : "Palmira Villa, Oviedo"})
Expand Down
27 changes: 5 additions & 22 deletions webapp/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@

//import { useQuery } from 'react-query';
import Box from '@mui/material/Box';
//import Link from '@mui/material/Link';
import Grid from '@mui/material/Grid';

import logo from './images/interfaz/logoRock.png';
import Title from './components/titleUtil';


import { createTheme, Drawer, List } from '@mui/material';
import { Drawer } from '@mui/material';
import { useState, useEffect } from 'react';
import Welcome from './components/Welcome';
import {getRocas} from './api/api';
import './css/App.css';
import { Route, Routes, Navigate, BrowserRouter as Router, BrowserRouter } from "react-router-dom";
import { Route, Routes, Navigate, BrowserRouter } from "react-router-dom";
import {Rock} from './shared/shareddtypes';
import Catalog from './components/Catalog';
import { ThemeProvider } from '@emotion/react';
Expand All @@ -25,9 +20,6 @@ import { Container } from "@mui/material";

import "./css/App.css"
import ShoppingCart from "./components/ShoppingCart";
import PaymentPage from "./components/payment/PaymentPage";
import { ContentCopy } from "@mui/icons-material";
import PaymentSummary from './components/payment/PaymentSummary';
import PaymentProcess from './components/payment/PaymentPage';
import OrderHistory from './components/Orders';

Expand All @@ -36,12 +28,6 @@ type Props = {
};

function App(): JSX.Element {
const [rocks, setRocks] = useState<Rock[]>([]);

const refreshRockList = async () => {
setRocks(await getRocas());
};

// Shopping cart
const [isNewCart, setNewCart] = useState(false);
const [isCartOpen, setCartOpen] = useState(false);
Expand Down Expand Up @@ -98,20 +84,17 @@ function App(): JSX.Element {
);
};

useEffect(() => {
refreshRockList();
}, []);
return (
<ThemeProvider theme={theme}>
<Container maxWidth="xl" className="principal">
<NavBar openCart={() => setCartOpen(true)} />
<BrowserRouter>
<Routes>
<Route path="/home" element={<Welcome handleAddToCart={handleAddToCart} />} />
<Route path="/home" element={<Welcome handleAddToCart={handleAddToCart} />} />
<Route path="/" element={<Navigate replace to="/home" />} />
<Route path="/catalog" element={ <Catalog rocks={rocks} handleAddToCart={handleAddToCart} /> } />
<Route path="/catalog" element={ <Catalog handleAddToCart={handleAddToCart} /> } />
<Route path="/orders" element={ <OrderHistory email={"[email protected]"}/> } />
<Route path="/payment" element={ <PaymentProcess cartContent={cartContent} setNewCart={setNewCart} /> } />
<Route path="/payment" element={ <PaymentProcess cartContent={cartContent} setNewCart={setNewCart} /> } />
<Route path="/login" element={<LogIn />} />
<Route path="/register" element={<Register />} />
<Route path="/logout" element ={<Welcome handleAddToCart={handleAddToCart} />}/>
Expand Down
34 changes: 26 additions & 8 deletions webapp/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {User, Rock, Order} from '../shared/shareddtypes';

const apiEndPoint =process.env.REACT_APP_API_URI || 'http://localhost:5000/api';

export async function addUser(user:User):Promise<boolean>{
const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api'
let response = await fetch(apiEndPoint+'/users/add', {
method: 'POST',
headers: {'Content-Type':'application/json'},
Expand All @@ -14,14 +15,12 @@ export async function addUser(user:User):Promise<boolean>{
}

export async function getUsers():Promise<User[]>{
const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api'
let response = await fetch(apiEndPoint+'/users/list');
//The objects returned by the api are directly convertible to User objects
return response.json()
}

export async function getRocas():Promise<Rock[]>{
const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api'
let response = await fetch(apiEndPoint+'/rocks/list');
//The objects returned by the api are directly convertible to User objects
return response.json()
Expand All @@ -36,26 +35,31 @@ export async function getRocksById(rockId:String):Promise<Rock[]>{


export async function getRocksSedimentary():Promise<Rock[]>{
const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api'
let response = await fetch(apiEndPoint+'/rocks/list/sedimentary');
//The objects returned by the api are directly convertible to User objects
return response.json()
}
export async function getRocksFiery():Promise<Rock[]>{
const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api'
let response = await fetch(apiEndPoint+'/rocks/list/fiery');
//The objects returned by the api are directly convertible to User objects
return response.json()
}
export async function getRocksMetamorphic():Promise<Rock[]>{
const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api'
let response = await fetch(apiEndPoint+'/rocks/list/metamorphic');
//The objects returned by the api are directly convertible to User objects
return response.json()
}

export async function getFilteredRocks(mohsMin:Number,mohsMax:Number,densityMin:Number,densityMax:Number,priceMin:Number,priceMax:Number,nameSubString:string,type:string):Promise<Rock[]> {
let response = await fetch(apiEndPoint+'/rocks/list/critery?mohsMin='+mohsMin+"&mohsMax="+mohsMax+"&densityMin="+densityMin+"&densityMax="+densityMax+"&priceMin="+priceMin+"&priceMax="+priceMax+"&nameSubString="+nameSubString+"&type="+type);
//The objects returned by the api are directly convertible to User objects
return response.json()
}
export async function getMaxAndMins() {
let response = await fetch(apiEndPoint+'/rocks/maxvalues');
//The objects returned by the api are directly convertible to User objects
return response.json()
}
export async function checkUser(email:String,password:String):Promise<boolean>{
const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api'
let response = await fetch(apiEndPoint+'/users/login', {
method: 'POST',
headers: {'Content-Type':'application/json'},
Expand Down Expand Up @@ -96,4 +100,18 @@ export async function getOrders(): Promise<Order[]>{
const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api'
let response = await fetch(apiEndPoint + "/orders/userList/" + sessionStorage.getItem("userLogged"));
return response.json();
}

export async function addOrder(order:Order):Promise<boolean>{
const apiEndPoint= process.env.REACT_APP_API_URI || 'http://localhost:5000/api'
let response = await fetch(apiEndPoint+'/orders/add', {
method: 'POST',
headers: {'Content-Type':'application/json'},
body: JSON.stringify({'userEmail':order.userEmail, 'price':order.price, 'productId':order.productId,
'productName':order.productName, 'productType': order.productType})
});
if (response.status===200)
return true;
else
return false;
}
Loading

0 comments on commit cbc5712

Please sign in to comment.