[AI] Use Cases và Ví Dụ Thực Tế với GitHub Copilot

Posted by : on

Category : ai   github-copilot


Use Cases và Ví Dụ Thực Tế

1. Web Development

Use Case 1: Tạo REST API với Express

Scenario: Bạn cần tạo một REST API cho quản lý sản phẩm

Prompt:

/*
Create Express REST API for Product Management
Endpoints:
- GET /api/products - Get all products (with pagination)
- GET /api/products/:id - Get product by ID
- POST /api/products - Create new product
- PUT /api/products/:id - Update product
- DELETE /api/products/:id - Delete product

Middleware: authentication, validation, error handling
Database: MongoDB with Mongoose
Response format: JSON with status codes
*/

const express = require('express');
const router = express.Router();

// Copilot generates complete API

Kết quả:

const express = require('express');
const router = express.Router();
const Product = require('../models/Product');
const auth = require('../middleware/auth');
const { validateProduct } = require('../middleware/validation');

// GET all products with pagination
router.get('/products', async (req, res) => {
    try {
        const page = parseInt(req.query.page) || 1;
        const limit = parseInt(req.query.limit) || 10;
        const skip = (page - 1) * limit;

        const products = await Product.find()
            .skip(skip)
            .limit(limit)
            .sort({ createdAt: -1 });

        const total = await Product.countDocuments();

        res.json({
            success: true,
            data: products,
            pagination: {
                page,
                limit,
                total,
                pages: Math.ceil(total / limit)
            }
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: 'Server error',
            error: error.message
        });
    }
});

// GET product by ID
router.get('/products/:id', async (req, res) => {
    try {
        const product = await Product.findById(req.params.id);
        
        if (!product) {
            return res.status(404).json({
                success: false,
                message: 'Product not found'
            });
        }

        res.json({
            success: true,
            data: product
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: 'Server error',
            error: error.message
        });
    }
});

// POST create product (protected route)
router.post('/products', auth, validateProduct, async (req, res) => {
    try {
        const product = new Product(req.body);
        await product.save();

        res.status(201).json({
            success: true,
            data: product,
            message: 'Product created successfully'
        });
    } catch (error) {
        res.status(400).json({
            success: false,
            message: 'Failed to create product',
            error: error.message
        });
    }
});

// PUT update product
router.put('/products/:id', auth, validateProduct, async (req, res) => {
    try {
        const product = await Product.findByIdAndUpdate(
            req.params.id,
            req.body,
            { new: true, runValidators: true }
        );

        if (!product) {
            return res.status(404).json({
                success: false,
                message: 'Product not found'
            });
        }

        res.json({
            success: true,
            data: product,
            message: 'Product updated successfully'
        });
    } catch (error) {
        res.status(400).json({
            success: false,
            message: 'Failed to update product',
            error: error.message
        });
    }
});

// DELETE product
router.delete('/products/:id', auth, async (req, res) => {
    try {
        const product = await Product.findByIdAndDelete(req.params.id);

        if (!product) {
            return res.status(404).json({
                success: false,
                message: 'Product not found'
            });
        }

        res.json({
            success: true,
            message: 'Product deleted successfully'
        });
    } catch (error) {
        res.status(500).json({
            success: false,
            message: 'Failed to delete product',
            error: error.message
        });
    }
});

module.exports = router;

Use Case 2: React Component với Hooks

Scenario: Tạo form đăng ký với validation

Prompt:

/*
Create React registration form component
Features:
- Fields: username, email, password, confirm password
- Validation: real-time validation, show errors
- Submit: call API, show loading state, handle errors
- Success: show message, redirect to login
Use: React hooks (useState, useEffect), custom validation hook
*/

import React from 'react';

function RegistrationForm() {
    // Copilot generates component
}

Kết quả:

import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';

function RegistrationForm() {
    const navigate = useNavigate();
    const [formData, setFormData] = useState({
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
    });
    const [errors, setErrors] = useState({});
    const [loading, setLoading] = useState(false);
    const [success, setSuccess] = useState(false);

    // Validation function
    const validateForm = () => {
        const newErrors = {};

        if (formData.username.length < 3) {
            newErrors.username = 'Username must be at least 3 characters';
        }

        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(formData.email)) {
            newErrors.email = 'Invalid email format';
        }

        if (formData.password.length < 8) {
            newErrors.password = 'Password must be at least 8 characters';
        }

        if (formData.password !== formData.confirmPassword) {
            newErrors.confirmPassword = 'Passwords do not match';
        }

        setErrors(newErrors);
        return Object.keys(newErrors).length === 0;
    };

    // Handle input change
    const handleChange = (e) => {
        const { name, value } = e.target;
        setFormData(prev => ({
            ...prev,
            [name]: value
        }));
        
        // Clear error for this field
        if (errors[name]) {
            setErrors(prev => ({
                ...prev,
                [name]: ''
            }));
        }
    };

    // Handle form submit
    const handleSubmit = async (e) => {
        e.preventDefault();

        if (!validateForm()) {
            return;
        }

        setLoading(true);

        try {
            const response = await fetch('/api/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: formData.username,
                    email: formData.email,
                    password: formData.password
                })
            });

            const data = await response.json();

            if (response.ok) {
                setSuccess(true);
                setTimeout(() => {
                    navigate('/login');
                }, 2000);
            } else {
                setErrors({ submit: data.message || 'Registration failed' });
            }
        } catch (error) {
            setErrors({ submit: 'Network error. Please try again.' });
        } finally {
            setLoading(false);
        }
    };

    if (success) {
        return (
            <div className="success-message">
                <h2>Registration Successful!</h2>
                <p>Redirecting to login...</p>
            </div>
        );
    }

    return (
        <div className="registration-form">
            <h2>Create Account</h2>
            <form onSubmit={handleSubmit}>
                <div className="form-group">
                    <label htmlFor="username">Username</label>
                    <input
                        type="text"
                        id="username"
                        name="username"
                        value={formData.username}
                        onChange={handleChange}
                        className={errors.username ? 'error' : ''}
                    />
                    {errors.username && (
                        <span className="error-message">{errors.username}</span>
                    )}
                </div>

                <div className="form-group">
                    <label htmlFor="email">Email</label>
                    <input
                        type="email"
                        id="email"
                        name="email"
                        value={formData.email}
                        onChange={handleChange}
                        className={errors.email ? 'error' : ''}
                    />
                    {errors.email && (
                        <span className="error-message">{errors.email}</span>
                    )}
                </div>

                <div className="form-group">
                    <label htmlFor="password">Password</label>
                    <input
                        type="password"
                        id="password"
                        name="password"
                        value={formData.password}
                        onChange={handleChange}
                        className={errors.password ? 'error' : ''}
                    />
                    {errors.password && (
                        <span className="error-message">{errors.password}</span>
                    )}
                </div>

                <div className="form-group">
                    <label htmlFor="confirmPassword">Confirm Password</label>
                    <input
                        type="password"
                        id="confirmPassword"
                        name="confirmPassword"
                        value={formData.confirmPassword}
                        onChange={handleChange}
                        className={errors.confirmPassword ? 'error' : ''}
                    />
                    {errors.confirmPassword && (
                        <span className="error-message">{errors.confirmPassword}</span>
                    )}
                </div>

                {errors.submit && (
                    <div className="error-message submit-error">
                        {errors.submit}
                    </div>
                )}

                <button 
                    type="submit" 
                    disabled={loading}
                    className="submit-button"
                >
                    {loading ? 'Creating Account...' : 'Register'}
                </button>
            </form>
        </div>
    );
}

export default RegistrationForm;

2. Data Processing

Use Case 3: ETL Pipeline

Scenario: Xử lý và transform data từ CSV

Prompt:

"""
ETL Pipeline for Sales Data
Input: CSV file with columns: date, product_id, quantity, price, customer_id
Process:
1. Read CSV file
2. Clean data (remove nulls, fix formats)
3. Calculate total_amount = quantity * price
4. Aggregate by product_id (total sales, avg price)
5. Add date dimensions (year, month, quarter)
6. Save to database and export summary to JSON
Error handling: log errors, continue processing
"""

import pandas as pd
import logging

def process_sales_data(csv_file):
    # Copilot generates ETL pipeline

Use Case 4: Data Validation

Scenario: Validate user input data

Prompt:

/*
Create comprehensive data validator
Validate:
- Email format
- Phone number (international format)
- Credit card number (Luhn algorithm)
- Date ranges
- URL format
- Password strength (min 8 chars, uppercase, lowercase, number, special char)
Return: { valid: boolean, errors: array }
*/

class DataValidator {
    // Copilot generates validators
}

3. Database Operations

Use Case 5: Complex SQL Query

Prompt:

-- Query: Get top 10 customers by total purchase amount in last 6 months
-- Include: customer name, email, total orders, total amount, avg order value
-- Join: customers, orders, order_items tables
-- Filter: orders from last 6 months, status = 'completed'
-- Sort: by total amount descending
-- Format: round amounts to 2 decimals

SELECT 
    -- Copilot generates complex query

Use Case 6: Database Migration

Prompt:

/*
Create database migration: Add user roles and permissions
Changes:
1. Create roles table (id, name, description)
2. Create permissions table (id, name, resource, action)
3. Create role_permissions junction table
4. Add role_id to users table
5. Insert default roles: admin, user, moderator
6. Create indexes for performance
Rollback: drop tables and column
*/

exports.up = async function(knex) {
    // Copilot generates migration
};

exports.down = async function(knex) {
    // Copilot generates rollback
};

4. Testing

Use Case 7: Unit Tests với Jest

Prompt:

// Function to test
function calculateDiscount(price, customerType, quantity) {
    let discount = 0;
    
    if (customerType === 'premium') {
        discount = 0.2;
    } else if (customerType === 'regular') {
        discount = 0.1;
    }
    
    if (quantity >= 10) {
        discount += 0.05;
    }
    
    return price * (1 - discount);
}

/*
Write comprehensive unit tests
Test cases:
- Premium customer, quantity < 10
- Premium customer, quantity >= 10
- Regular customer, quantity < 10
- Regular customer, quantity >= 10
- New customer (no discount)
- Edge cases: zero price, negative quantity, invalid customer type
Use: Jest, describe/it blocks, expect assertions
*/

describe('calculateDiscount', () => {
    // Copilot generates tests
});

Use Case 8: Integration Tests

Prompt:

/*
Integration test: User registration flow
Test:
1. POST /api/register with valid data
2. Verify user created in database
3. Verify email sent
4. Verify password is hashed
5. Verify JWT token returned
6. Test duplicate email (should fail)
7. Test invalid email format (should fail)
Use: supertest, Jest, test database
*/

describe('User Registration Integration Tests', () => {
    // Copilot generates integration tests
});

5. DevOps và Automation

Use Case 9: Docker Configuration

Prompt:

# Dockerfile for Node.js application
# Base: node:18-alpine
# Features:
# - Multi-stage build (build and production)
# - Install dependencies
# - Copy source code
# - Build TypeScript
# - Run as non-root user
# - Health check
# - Optimize for production (small image size)
# Expose port 3000

# Copilot generates Dockerfile

Use Case 10: CI/CD Pipeline

Prompt:

# GitHub Actions workflow for Node.js app
# Trigger: push to main, pull requests
# Jobs:
# 1. Lint and format check
# 2. Run unit tests
# 3. Run integration tests
# 4. Build Docker image
# 5. Push to registry
# 6. Deploy to staging (if main branch)
# Matrix: test on Node 16, 18, 20
# Cache: npm dependencies

name: CI/CD Pipeline
# Copilot generates workflow

6. Algorithms và Data Structures

Use Case 11: Implement Binary Search Tree

Prompt:

/*
Implement Binary Search Tree
Methods:
- insert(value): add node
- search(value): find node
- delete(value): remove node
- inorder(): traverse in order
- preorder(): traverse pre order
- postorder(): traverse post order
- findMin(): find minimum value
- findMax(): find maximum value
- height(): calculate tree height
Include: proper error handling, edge cases
*/

class BinarySearchTree {
    // Copilot generates BST implementation
}

Use Case 12: Sorting Algorithm

Prompt:

"""
Implement Quick Sort algorithm
Features:
- In-place sorting
- Handle duplicates
- Optimize for small arrays (use insertion sort)
- Add logging for educational purposes
- Time complexity: O(n log n) average
- Space complexity: O(log n)
Include: docstring, type hints, examples
"""

def quick_sort(arr: list[int]) -> list[int]:
    # Copilot generates quick sort

7. Security

Use Case 13: Authentication Middleware

Prompt:

/*
Create JWT authentication middleware for Express
Features:
- Verify JWT token from Authorization header
- Check token expiration
- Validate token signature
- Extract user info from token
- Handle errors: missing token, invalid token, expired token
- Add user object to request
- Support refresh tokens
*/

const jwt = require('jsonwebtoken');

function authMiddleware(req, res, next) {
    // Copilot generates auth middleware
}

Use Case 14: Input Sanitization

Prompt:

/*
Create input sanitization utility
Sanitize:
- HTML (prevent XSS)
- SQL (prevent injection)
- NoSQL (prevent injection)
- File paths (prevent traversal)
- Email addresses
- URLs
Return sanitized string
*/

class InputSanitizer {
    // Copilot generates sanitization methods
}

8. Real-world Scenarios

Scenario 1: E-commerce Checkout

Prompt:

/*
Implement checkout process
Steps:
1. Validate cart items (stock availability)
2. Calculate totals (subtotal, tax, shipping, discount)
3. Validate payment information
4. Process payment (integrate with payment gateway)
5. Create order in database
6. Update inventory
7. Send confirmation email
8. Return order details
Error handling: rollback on failure, log errors
*/

async function processCheckout(cart, paymentInfo, shippingInfo) {
    // Copilot generates checkout logic
}

Scenario 2: Real-time Chat

Prompt:

/*
WebSocket chat server with Socket.io
Features:
- User authentication
- Join/leave rooms
- Send/receive messages
- Typing indicators
- Online users list
- Message history
- Private messages
- Broadcast to room
Error handling and reconnection
*/

const io = require('socket.io')(server);

io.on('connection', (socket) => {
    // Copilot generates chat server
});

Scenario 3: File Upload Service

Prompt:

/*
File upload service with validation
Features:
- Accept multiple files
- Validate file type (images only: jpg, png, gif)
- Validate file size (max 5MB)
- Generate unique filename
- Resize images (thumbnail, medium, large)
- Upload to cloud storage (AWS S3)
- Save metadata to database
- Return URLs
Error handling: cleanup on failure
*/

async function uploadFiles(files, userId) {
    // Copilot generates upload service
}

9. Refactoring Examples

Before Refactoring:

// Legacy code with code smells
function processOrder(order) {
    if (order.items.length > 0) {
        var total = 0;
        for (var i = 0; i < order.items.length; i++) {
            total = total + order.items[i].price * order.items[i].quantity;
        }
        if (order.customer.type == 'premium') {
            total = total * 0.9;
        }
        if (total > 100) {
            var shipping = 0;
        } else {
            var shipping = 10;
        }
        return total + shipping;
    }
}

Prompt:

// Refactor this code:
// 1. Use modern JavaScript (const/let, arrow functions, array methods)
// 2. Extract calculation logic to separate functions
// 3. Add TypeScript types
// 4. Improve naming
// 5. Add error handling
// 6. Follow SOLID principles

// Copilot generates refactored code

After Refactoring:

interface OrderItem {
    price: number;
    quantity: number;
}

interface Customer {
    type: 'premium' | 'regular';
}

interface Order {
    items: OrderItem[];
    customer: Customer;
}

const PREMIUM_DISCOUNT = 0.1;
const FREE_SHIPPING_THRESHOLD = 100;
const STANDARD_SHIPPING_COST = 10;

function calculateSubtotal(items: OrderItem[]): number {
    return items.reduce((sum, item) => sum + (item.price * item.quantity), 0);
}

function applyDiscount(amount: number, customerType: Customer['type']): number {
    return customerType === 'premium' 
        ? amount * (1 - PREMIUM_DISCOUNT)
        : amount;
}

function calculateShipping(subtotal: number): number {
    return subtotal >= FREE_SHIPPING_THRESHOLD 
        ? 0 
        : STANDARD_SHIPPING_COST;
}

function processOrder(order: Order): number {
    if (!order?.items?.length) {
        throw new Error('Order must contain at least one item');
    }

    const subtotal = calculateSubtotal(order.items);
    const discountedTotal = applyDiscount(subtotal, order.customer.type);
    const shipping = calculateShipping(discountedTotal);

    return discountedTotal + shipping;
}

10. Tips cho Use Cases phức tạp

Tip 1: Chia nhỏ vấn đề

// Thay vì một prompt lớn, chia thành nhiều bước
// Step 1: Define interfaces
// Step 2: Implement core logic
// Step 3: Add error handling
// Step 4: Add tests

Tip 2: Cung cấp examples

// Example input and output giúp Copilot hiểu rõ hơn
// Input: { items: [...], customer: {...} }
// Output: { total: 150.50, breakdown: {...} }

Tip 3: Reference existing code

// Follow the pattern from UserService
// Use similar error handling as in AuthController

Tip 4: Specify constraints

// Constraints:
// - Must complete in < 100ms
// - Memory usage < 50MB
// - Support 1000 concurrent users

Kết luận

Các use cases này cho thấy GitHub Copilot có thể hỗ trợ trong nhiều tình huống:

Web Development - APIs, Components, Forms
Data Processing - ETL, Validation, Transformation
Database - Queries, Migrations, ORMs
Testing - Unit, Integration, E2E
DevOps - Docker, CI/CD, Automation
Algorithms - Data Structures, Sorting, Searching
Security - Auth, Sanitization, Validation
Real-world - E-commerce, Chat, File Upload

Key Takeaways:

  • Prompts rõ ràng = Kết quả tốt hơn
  • Chia nhỏ problems phức tạp
  • Cung cấp examples và context
  • Luôn review và test code
  • Học từ patterns được generate

Bài viết được viết bằng AI 🚀


About Nguyen Chung
Nguyen Chung

Hi I am Nguyen Chung, an Automation Tester.

Email : ndchungict@gmail.com

Website : https://ndchungict.github.io

About Nguyen Chung

Hi, my name is Nguyen Duc Chung. Nice to see you!