Skip to content

Commit

Permalink
Merge pull request #197 from Arquisoft/Ignacio_BaseDeDatos
Browse files Browse the repository at this point in the history
Función para obtener el mas vendido
  • Loading branch information
valydumitru01 authored May 1, 2022
2 parents 867dc54 + a247e52 commit 765a464
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions restapi/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import express, { Router } from 'express';

import { addOrder, findOrdersByUserEmail, getDeliveryCosts } from './controllers/OrderController';
import express, { Request, Response, Router } from 'express';
import {check} from 'express-validator';
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';

Expand Down Expand Up @@ -45,4 +47,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 765a464

Please sign in to comment.