Skip to content

Commit

Permalink
Función para obtener el mas vendido
Browse files Browse the repository at this point in the history
  • Loading branch information
UO271548 committed Apr 30, 2022
1 parent 5810d3d commit 242f496
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion restapi/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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");
Expand Down Expand Up @@ -46,4 +46,6 @@ api.post("/orders/add", addOrder);

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

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

export default api;
23 changes: 23 additions & 0 deletions 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 Down

0 comments on commit 242f496

Please sign in to comment.