Skip to content

SamstyleGhost/one-stop-shop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Prerequisite to run this

  • Require node,npm and Go installed
  • My versions:
    • node: v22.11.0
    • npm: v10.9.0
    • go: v1.22.5

Attention:

The app is on the heavier side of things due to detailed 3d models and images contained in the repo itself. In actual deployments, these would be in a separately hosted storage system and compressed

  • File sizes for the client directory

Setup

  1. git clone
  2. cd server
  3. go run main.go
  4. cd ..
  5. cd client
  6. npm install
  7. npm run dev

Testing

To run unit tests, it is essential to change the filepaths at the top of the helpers file:

  • itemsPath : data/items.json -> ../data/items.json
  • usersPath : data/users.json -> ../data/users.json
  • ordersPath : data/orders.json -> ../data/orders.json

Need to add additional test cases to test direct API endpoints. Current tests only check the helper functions

Features

  • The backend of the project is built with Go
    • Used fiber as the backend framework
  • The frontend is built using React with Vite
  • There is no external database, however I have added persistency through json files as well as some use of browser localStorage

Endpoints

API endpoints are available for:

  • GET /items

    • Sends all items from the items.json file
    • Response:
      {
      "items": [
          {
              "item_id": 1,
              "name": "Office Chair",
              "vendor": "OfficeCo Supplies",
              "description": "A comfortable and ergonomic office chair with adjustable height and wheels for mobility, ideal for long hours at the desk.",
              "images": [
                  "chair1",
                  "chair2"
              ],
              "three_link": "https://fab.com/s/2cf83e2ac5e8",
              "price": 49.99,
              "quantity": 25
          },
          ...
        ]
      }
      
  • POST /items

    • Sends only the selected item
    • Request:
    {
      "item_id": 3
    }
    
    • Response:
    {
      "item": {
          "item_id": 3,
          "name": "Clock",
          "vendor": "Timekeepers Ltd.",
          "description": "A sleek and modern wall clock with a silent mechanism, suitable for homes or offices.",
          "images": [
              "clock1",
              "clock2",
              "clock3"
          ],
          "three_link": "https://fab.com/s/7534f812496b",
          "price": 19.99,
          "quantity": 100
      }
    }
    
  • POST /user/get-cart

    • Sends the user's cart
    • Request :
    {
      "user_id": "85090ac9-9066-4979-8a64-91b4a6016bbc"
    }
    
    • Response:
    {
      "user": {
          "user_id": "85090ac9-9066-4979-8a64-91b4a6016bbc",
          "cart": [
              {
                  "item_id": 2,
                  "quantity": 6
              },
              {
                  "item_id": 2,
                  "quantity": 6
              }
          ],
          "total_cart_value": 1079.88
      }
    }
    
  • POST /user

    • Adds the current user to the users file
    • Request: user_id
  • POST /user/add-to-cart

    • Adds current item to the user's cart
    • Request:
    {
      "user_id": "39490ad6-7cc2-42f6-8887-3f29749dbb42",
      "item_id": 5,
      "quantity": 6
    }
    
    • Response:
    {
      "cart": [
          {
              "item_id": 5,
              "quantity": 6
          }
      ]
    }
    
  • POST /user/checkout

    • Request1:
    {
      "user_id": "39490ad6-7cc2-42f6-8887-3f29749dbb42"
    }
    
    • Response1:
    {
      "cart": [
          {
              "item_id": 5,
              "quantity": 6
          }
      ],
      "coupon": "00000000-0000-0000-0000-000000000000",
      "discount": 0,
      "original_price": 599.94,
      "total_price": 599.94
    }
    
    • Request2:
    {
      "user_id": "8d27c3f7-bc80-4994-bab7-e27db5f102c5",
      "coupon_code": "1972fcab-3b1c-4cae-b3db-31704b931973"
    }
    
    • Response2:
    {
      "cart": [
          {
              "item_id": 3,
              "quantity": 2
          },
          {
              "item_id": 4,
              "quantity": 5
          }
      ],
      "coupon": "00000000-0000-0000-0000-000000000000",
      "discount": 78.993004,
      "original_price": 789.93,
      "total_price": 710.937
    }
    
    • The nil uuid present in the coupon field signifies that the current transaction did not generate any coupon
    • Every nth transaction generates a discount coupon, which can be redeemed as shown in the above request
  • POST /user/coupons

    • Request:
    {
      "user_id": "847f38fa-bf27-43b1-a871-93c1810098c8"
    }
    
    • Response:
    [
      {
          "user_id": "847f38fa-bf27-43b1-a871-93c1810098c8",
          "coupon_code": "fc1d6f3e-91ff-4fd3-a809-a9d702485595"
      },
      {
          "user_id": "847f38fa-bf27-43b1-a871-93c1810098c8",
          "coupon_code": "7259d3fc-5e7f-4e23-8aae-1c5b81e03663"
      }
    ]
    

Additional Info

  • Adding user info to data
    • Users get a uuid assigned to them when they open the website. This id is stored in the localStorage in the frontend and is used to save user selected items in their respective carts
    • Checkout and discount functionalities also make use of this id
    • As long as the id field is not removed from the browser's local storage, every time a user visits the website, same id will be persisted
    • This is just a simulation for user auth
  • Adding item to user cart
    • Only item-id is taken from user. This would help in maintaining consistency in the data
  • Checkout
    • Calculates total price of all cart items
    • For every nth order (I am using n = 3), generates a coupon code (uuid) that would be sent to the user. It is also simultaneously added to the orders.json file where the coupon code is stored in conjuction with the user that received it. Even if a coupon code is a valid one, but originates from the incorrect user, discount will not be applied
    • If the user has a valid coupon code, then the endpoint would bestow a discount of 10% on the current cart total, and the coupon code is deleted from the coupons list. This ensures that a coupon will only be used once.
    • After checkout is complete, the cart is emptied out, and the value resets to 0
    • Also checkout cannot be done if a cart is empty

Needed optimizations

  • In hindsight, having a seperate file for coupons was a small mistake, just an additional field in the users file would have done the job better
  • On the client side, havent really compressed the images and models much. Also, right now everything resides on the server itself, but using a storage system would ease the burden and also make working with the assets better.
  • The frontend doesnt have feedbacks for when an API works or doesnt work

Photo credits:

3d model credits:

About

E-commerce store built with Go, React & Three

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors