- Python (Flask Web 框架)paztt.cn
app.py
python
from flask import Flask, request, jsonify
app = Flask(name)
假设的购物车数据
cart = {‘product1’: 2, ‘product2’: 1}
@app.route(‘/add_to_cart’, methods=[‘POST’])
def add_to_cart():
product = request.json.get(‘product’)
quantity = request.json.get(‘quantity’, 1)
cart[product] = cart.get(product, 0) + quantity
return jsonify({‘message’: f’{product} added to cart’, ‘cart’: cart})
… 其他路由和函数 …
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js 和 Express 框架)
server.js
javascript
const express = require(‘express’);
const app = express();
const bodyParser = require(‘body-parser’);
app.use(bodyParser.json());
// 假设的购物车数据
let cart = {‘product1’: 2, ‘product2’: 1};
app.post(‘/add_to_cart’, (req, res) => {
const product = req.body.product;
const quantity = req.body.quantity || 1;
cart[product] = (cart[product] || 0) + quantity;
res.json({message: ${product} added to cart
, cart: cart});
});
// … 其他路由和中间件 …
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (Spring Boot)
Controller.java
java
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
public class ShoppingCartController {
// 假设的购物车数据
private Map<String, Integer> cart = new HashMap<>();
@PostMapping("/add_to_cart")
public Map<String, Object> addToCart(@RequestBody Map<String, Integer> productQty) {
String product = productQty.keySet().iterator().next();
int quantity = productQty.getOrDefault(product, 1);
cart.put(product, cart.getOrDefault(product, 0) + quantity);
return Map.of("message", product + " added to cart", "cart", cart);
}
// ... 其他控制器方法和类 ...
}
4. Ruby (Ruby on Rails)
controllers/cart_controller.rb
ruby
class CartController < ApplicationController
假设的购物车数据
@@cart = {‘product1’ => 2, ‘product2’ => 1}
def add_to_cart
product = params[:product]
quantity = params[:quantity] || 1
@@cart[product] = (@@cart[product] || 0) + quantity.to_i
render json: {message: “#{product} added to cart”, cart: @@cart}
end
… 其他动作和私有方法 …
end
请注意,这些代码片段只是为了展示如何在不同的编程语言中实现购物车的基本添加功能。一个完整的购物商城会涉及更多的功能,如用户认证、库存管理、支付处理、数据库交互等。由于编写一个完整的购物商城代码超出了简短回答的范围,我将为您提供一个简化的购物商城的核心功能的伪代码或示例代码片段,使用几种不同的编程语言。
- Python (Flask Web 框架)
app.py
python
from flask import Flask, request, jsonify
app = Flask(name)
假设的购物车数据
cart = {‘product1’: 2, ‘product2’: 1}
@app.route(‘/add_to_cart’, methods=[‘POST’])
def add_to_cart():
product = request.json.get(‘product’)
quantity = request.json.get(‘quantity’, 1)
cart[product] = cart.get(product, 0) + quantity
return jsonify({‘message’: f’{product} added to cart’, ‘cart’: cart})
… 其他路由和函数 …
if name == ‘main’:
app.run(debug=True)
2. JavaScript (Node.js 和 Express 框架)
server.js
javascript
const express = require(‘express’);
const app = express();
const bodyParser = require(‘body-parser’);
app.use(bodyParser.json());
// 假设的购物车数据
let cart = {‘product1’: 2, ‘product2’: 1};
app.post(‘/add_to_cart’, (req, res) => {
const product = req.body.product;
const quantity = req.body.quantity || 1;
cart[product] = (cart[product] || 0) + quantity;
res.json({message: ${product} added to cart
, cart: cart});
});
// … 其他路由和中间件 …
app.listen(3000, () => console.log(‘Server started on port 3000’));
3. Java (Spring Boot)
Controller.java
java
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
public class ShoppingCartController {
// 假设的购物车数据
private Map<String, Integer> cart = new HashMap<>();
@PostMapping("/add_to_cart")
public Map<String, Object> addToCart(@RequestBody Map<String, Integer> productQty) {
String product = productQty.keySet().iterator().next();
int quantity = productQty.getOrDefault(product, 1);
cart.put(product, cart.getOrDefault(product, 0) + quantity);
return Map.of("message", product + " added to cart", "cart", cart);
}
// ... 其他控制器方法和类 ...
}
4. Ruby (Ruby on Rails)
controllers/cart_controller.rb
ruby
class CartController < ApplicationController
假设的购物车数据
@@cart = {‘product1’ => 2, ‘product2’ => 1}
def add_to_cart
product = params[:product]
quantity = params[:quantity] || 1
@@cart[product] = (@@cart[product] || 0) + quantity.to_i
render json: {message: “#{product} added to cart”, cart: @@cart}
end
… 其他动作和私有方法 …
end
请注意,这些代码片段只是为了展示如何在不同的编程语言中实现购物车的基本添加功能。一个完整的购物商城会涉及更多的功能,如用户认证、库存管理、支付处理、数据库交互等。