Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

1. Develop a simple RESTFul API for following scenario. ABC is a cake (cupcakes)…

  1. User
    1. Develop a simple RESTFul API for following scenario.

    ABC is a cake (cupcakes) ordering & selling company. They have their own customer base & they have different types of cupcakes to sell. You’re supposed to store different types of cupcakes, their customer details & order details.

    Please create endpoints for the following.

    a.) Get all cupcake items b.) Add new cupcake item c.) Edit new cupcake item d.) Delete new cupcake item e.) Get all customers f.) Create new customer g.) Create an order for new customer h.) Get all orders for selected customer i.) Filter all orders which has more than 10 quantity in an order. (10 can be a variable) j.) Filter top 5 customers which have higher sales k.) Find the top order (highest value order) of a selected customer

    Hint: Create 3 arrays to store Cupcakes, Customers, and Orders.

  2. Assistant

    To develop a simple RESTful API for the given scenario, we'll be using Node.js with Express.js as our framework. Below are the necessary API endpoints and their implementations:

    Dependencies and Setup

    First, install the required dependencies and set up the Express server:

    npm init -y
    npm install express body-parser
    

    Create a file called app.js:

    const express = require('express');
    const bodyParser = require('body-parser');
    
    const app = express();
    app.use(bodyParser.json());
    
    const cupcakes = [];
    const customers = [];
    const orders = [];
    
    app.listen(3000, () => {
      console.log("Server is running on port 3000");
    });
    

    API Endpoints

    a.) Get all cupcake items

    app.get('/cupcakes', (req, res) => {
      res.json(cupcakes);
    });
    

    b.) Add new cupcake item

    app.post('/cupcakes', (req, res) => {
      const newCupcake = req.body;
      cupcakes.push(newCupcake);
      res.status(201).json(newCupcake);
    });
    

    c.) Edit cupcake item

    app.put('/cupcakes/:id', (req, res) => {
      const { id } = req.params;
      const updatedCupcake = req.body;
    
      const index = cupcakes.findIndex(c => c.id === id);
      if (index !== -1) {
        cupcakes[index] = updatedCupcake;
        res.json(updatedCupcake);
      } else {
        res.status(404).json({ message: "Cupcake not found" });
      }
    });
    

    d.) Delete cupcake item

    app.delete('/cupcakes/:id', (req, res) => {
      const { id } = req.params;
      const index = cupcakes.findIndex(c => c.id === id);
      if (index !== -1) {
        const deletedCupcake = cupcakes.splice(index, 1);
        res.json(deletedCupcake);
      } else {
        res.status(404).json({ message: "Cupcake not found" });
      }
    });
    

    e.) Get all customers

    app.get('/customers', (req, res) => {
      res.json(customers);
    });
    

    f.) Create new customer

    app.post('/customers', (req, res) => {
      const newCustomer = req.body;
      customers.push(newCustomer);
      res.status(201).json(newCustomer);
    });
    

    g.) Create an order for new customer

    app.post('/orders', (req, res) => {
      const newOrder = req.body;
      orders.push(newOrder);
      res.status(201).json(newOrder);
    });
    

    h.) Get all orders for selected customer

    app.get('/orders/customer/:customerId', (req, res) => {
      const { customerId } = req.params;
      const customerOrders = orders.filter(o => o.customerId === customerId);
      res.json(customerOrders);
    });
    

    i.) Filter all orders which have more than 10 quantity in an order (10 can be a variable)

    app.get('/orders/quantity/:minQuantity', (req, res) => {
      const { minQuantity } = req.params;
      const filteredOrders = orders.filter(order => order.quantity > parseInt(minQuantity));
      res.json(filteredOrders);
    });
    

    j.) Filter top 5 customers which have higher sales

    app.get('/customers/top5', (req, res) => {
      const customerSales = customers.map(customer => {
        const customerOrders = orders.filter(order => order.customerId === customer.id);
        const totalSales = customerOrders.reduce((sum, order) => sum + order.totalValue, 0);
        return { ...customer, totalSales };
      });
    
      const topCustomers = customerSales.sort((a, b) => b.totalSales - a.totalSales).slice(0, 5);
      res.json(topCustomers);
    });
    

    k.) Find the top order (highest value order) of a selected customer

    app.get('/orders/highest/:customerId', (req, res) => {
      const { customerId } = req.params;
      const customerOrders = orders.filter(order => order.customerId === customerId);
      const highestOrder = customerOrders.reduce((max, order) => order.totalValue > max.totalValue ? order : max, {
        totalValue: 0
      });
      
      if (highestOrder.totalValue === 0) {
        res.status(404).json({ message: "No orders found for this customer" });
      } else {
        res.json(highestOrder);
      }
    });
    

    Full app.js file

    Here's the complete app.js file incorporating all the endpoints:

    const express = require('express');
    const bodyParser = require('body-parser');
    
    const app = express();
    app.use(bodyParser.json());
    
    const cupcakes = [];
    const customers = [];
    const orders = [];
    
    app.get('/cupcakes', (req, res) => {
      res.json(cupcakes);
    });
    
    app.post('/cupcakes', (req, res) => {
      const newCupcake = req.body;
      cupcakes.push(newCupcake);
      res.status(201).json(newCupcake);
    });
    
    app.put('/cupcakes/:id', (req, res) => {
      const { id } = req.params;
      const updatedCupcake = req.body;
    
      const index = cupcakes.findIndex(c => c.id === id);
      if (index !== -1) {
        cupcakes[index] = updatedCupcake;
        res.json(updatedCupcake);
      } else {
        res.status(404).json({ message: "Cupcake not found" });
      }
    });
    
    app.delete('/cupcakes/:id', (req, res) => {
      const { id } = req.params;
      const index = cupcakes.findIndex(c => c.id === id);
      if (index !== -1) {
        const deletedCupcake = cupcakes.splice(index, 1);
        res.json(deletedCupcake);
      } else {
        res.status(404).json({ message: "Cupcake not found" });
      }
    });
    
    app.get('/customers', (req, res) => {
      res.json(customers);
    });
    
    app.post('/customers', (req, res) => {
      const newCustomer = req.body;
      customers.push(newCustomer);
      res.status(201).json(newCustomer);
    });
    
    app.post('/orders', (req, res) => {
      const newOrder = req.body;
      orders.push(newOrder);
      res.status(201).json(newOrder);
    });
    
    app.get('/orders/customer/:customerId', (req, res) => {
      const { customerId } = req.params;
      const customerOrders = orders.filter(o => o.customerId === customerId);
      res.json(customerOrders);
    });
    
    app.get('/orders/quantity/:minQuantity', (req, res) => {
      const { minQuantity } = req.params;
      const filteredOrders = orders.filter(order => order.quantity > parseInt(minQuantity));
      res.json(filteredOrders);
    });
    
    app.get('/customers/top5', (req, res) => {
      const customerSales = customers.map(customer => {
        const customerOrders = orders.filter(order => order.customerId === customer.id);
        const totalSales = customerOrders.reduce((sum, order) => sum + order.totalValue, 0);
        return { ...customer, totalSales };
      });
    
      const topCustomers = customerSales.sort((a, b) => b.totalSales - a.totalSales).slice(0, 5);
      res.json(topCustomers);
    });
    
    app.get('/orders/highest/:customerId', (req, res) => {
      const { customerId } = req.params;
      const customerOrders = orders.filter(order => order.customerId === customerId);
      const highestOrder = customerOrders.reduce((max, order) => order.totalValue > max.totalValue ? order : max, {
        totalValue: 0
      });
      
      if (highestOrder.totalValue === 0) {
        res.status(404).json({ message: "No orders found for this customer" });
      } else {
        res.json(highestOrder);
      }
    });
    
    app.listen(3000, () => {
      console.log("Server is running on port 3000");
    });
    

    Run your server with:

    node app.js
    

    Now, your RESTful API for the ABC cupcakes ordering & selling company should be up and running on port 3000. You can use tools like Postman or cURL to test each endpoint.