updated task
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
FROM node:18-alpine
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package*.json ./
|
||||
|
||||
RUN npm install
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
CMD ["npm", "run", "dev"]
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* TODO:
|
||||
* - create basic express server
|
||||
* - connect to mysql database
|
||||
* - create proper docker compose file and dockerfile to setup mysql, express server
|
||||
* - make api to look like this:
|
||||
* /v1/api/rest/video/PAGINATE
|
||||
Method POST
|
||||
|
||||
body
|
||||
{
|
||||
"payload": {},
|
||||
"page": 1,
|
||||
"limit": 10
|
||||
}
|
||||
Response
|
||||
http code 200
|
||||
{
|
||||
"error": false,
|
||||
"list": [
|
||||
{
|
||||
"id": 1,
|
||||
"title": "Rune raises $100,000 for marketing through NFT butterflies sale",
|
||||
"photo": "https://picsum.photos/200/200",
|
||||
"user_id": 1,
|
||||
"username": "boss",
|
||||
"create_at": "2022-01-01",
|
||||
"update_at": "2022-01-01T04:00:00.000Z",
|
||||
"like": 10
|
||||
}
|
||||
],
|
||||
"page": 1,
|
||||
"limit": 10,
|
||||
"total": 112,
|
||||
"num_pages": 12
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
const express = require('express');
|
||||
const mysql = require('mysql2/promise');
|
||||
const app = express();
|
||||
|
||||
// Middleware to parse JSON bodies
|
||||
app.use(express.json());
|
||||
|
||||
// Database connection configuration
|
||||
const dbConfig = {
|
||||
host: process.env.DB_HOST || 'mysql',
|
||||
user: process.env.DB_USER || 'user',
|
||||
password: process.env.DB_PASSWORD || 'password',
|
||||
database: process.env.DB_NAME || 'videodb'
|
||||
};
|
||||
|
||||
// API endpoint for video pagination
|
||||
app.get('/', async (req, res) => {
|
||||
res.send('Hello World');
|
||||
});
|
||||
|
||||
app.post('/v1/api/rest/video/PAGINATE', async (req, res) => {
|
||||
try {
|
||||
const page = parseInt(req.body.page) || 1;
|
||||
const limit = parseInt(req.body.limit) || 10;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const connection = await mysql.createConnection(dbConfig);
|
||||
|
||||
// Get total count
|
||||
const [countResult] = await connection.execute(
|
||||
'SELECT COUNT(*) as total FROM videos'
|
||||
);
|
||||
const total = countResult[0].total;
|
||||
|
||||
// Fix: Use a different query format for LIMIT
|
||||
const [rows] = await connection.query(
|
||||
`SELECT
|
||||
v.id, v.title, v.photo, v.user_id,
|
||||
v.created_at, v.updated_at, v.likes
|
||||
FROM videos v
|
||||
LIMIT ${offset}, ${limit}`
|
||||
);
|
||||
|
||||
await connection.end();
|
||||
|
||||
res.json({
|
||||
error: false,
|
||||
list: rows,
|
||||
page: page,
|
||||
limit: limit,
|
||||
total,
|
||||
num_pages: Math.ceil(total / limit)
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
res.status(500).json({ error: true, message: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running on port ${PORT}`);
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
app:
|
||||
build: .
|
||||
ports:
|
||||
- "3000:3000"
|
||||
environment:
|
||||
- DB_HOST=mysql
|
||||
- DB_USER=user
|
||||
- DB_PASSWORD=password
|
||||
- DB_NAME=videodb
|
||||
volumes:
|
||||
- .:/app
|
||||
- /app/node_modules
|
||||
depends_on:
|
||||
- mysql
|
||||
command: npm run dev
|
||||
|
||||
mysql:
|
||||
image: mysql:8.0
|
||||
environment:
|
||||
MYSQL_DATABASE: videodb
|
||||
MYSQL_USER: user
|
||||
MYSQL_PASSWORD: password
|
||||
MYSQL_ROOT_PASSWORD: rootpassword
|
||||
ports:
|
||||
- "3306:3306"
|
||||
volumes:
|
||||
- mysql-data:/var/lib/mysql
|
||||
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||
|
||||
volumes:
|
||||
mysql-data:
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
CREATE TABLE users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE videos (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
photo VARCHAR(255),
|
||||
user_id INT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
likes INT DEFAULT 0,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- Insert some sample data
|
||||
INSERT INTO users (username) VALUES ('boss');
|
||||
|
||||
INSERT INTO videos (title, photo, user_id, likes) VALUES
|
||||
('Rune raises $100,000 for marketing through NFT butterflies sale', 'https://picsum.photos/200/200', 1, 10),
|
||||
('Exploring the future of NFTs in gaming', 'https://picsum.photos/200/201', 1, 15),
|
||||
('How to create a successful YouTube channel', 'https://picsum.photos/200/202', 1, 20),
|
||||
('The rise of remote work: Pros and cons', 'https://picsum.photos/200/203', 1, 5),
|
||||
('Top 10 programming languages to learn in 2023', 'https://picsum.photos/200/204', 1, 8),
|
||||
('Understanding blockchain technology', 'https://picsum.photos/200/205', 1, 12),
|
||||
('The impact of AI on the job market', 'https://picsum.photos/200/206', 1, 7),
|
||||
('How to invest in cryptocurrency safely', 'https://picsum.photos/200/207', 1, 9),
|
||||
('The benefits of meditation for mental health', 'https://picsum.photos/200/208', 1, 11),
|
||||
('Traveling on a budget: Tips and tricks', 'https://picsum.photos/200/209', 1, 6),
|
||||
('The importance of cybersecurity in 2023', 'https://picsum.photos/200/210', 1, 14),
|
||||
('How to build a personal brand online', 'https://picsum.photos/200/211', 1, 13),
|
||||
('The future of electric vehicles', 'https://picsum.photos/200/212', 1, 10),
|
||||
('Exploring the metaverse: What you need to know', 'https://picsum.photos/200/213', 1, 15),
|
||||
('The best productivity tools for remote teams', 'https://picsum.photos/200/214', 1, 20),
|
||||
('How to create engaging content for social media', 'https://picsum.photos/200/215', 1, 5),
|
||||
('The role of data science in business', 'https://picsum.photos/200/216', 1, 8),
|
||||
('Understanding the gig economy', 'https://picsum.photos/200/217', 1, 12),
|
||||
('The benefits of learning a second language', 'https://picsum.photos/200/218', 1, 7),
|
||||
('How to stay motivated while working from home', 'https://picsum.photos/200/219', 1, 9),
|
||||
('The impact of climate change on our planet', 'https://picsum.photos/200/220', 1, 11),
|
||||
('How to create a successful online course', 'https://picsum.photos/200/221', 1, 6),
|
||||
('The importance of emotional intelligence in leadership', 'https://picsum.photos/200/222', 1, 14),
|
||||
('How to network effectively in a digital world', 'https://picsum.photos/200/223', 1, 13),
|
||||
('The rise of plant-based diets', 'https://picsum.photos/200/224', 1, 10),
|
||||
('Exploring the world of virtual reality', 'https://picsum.photos/200/225', 1, 15),
|
||||
('The best practices for remote team management', 'https://picsum.photos/200/226', 1, 20),
|
||||
('How to create a successful blog', 'https://picsum.photos/200/227', 1, 5),
|
||||
('The impact of social media on mental health', 'https://picsum.photos/200/228', 1, 8),
|
||||
('Understanding the basics of SEO', 'https://picsum.photos/200/229', 1, 12),
|
||||
('The benefits of regular exercise', 'https://picsum.photos/200/230', 1, 7),
|
||||
('How to manage stress effectively', 'https://picsum.photos/200/231', 1, 9),
|
||||
('The future of work: Trends to watch', 'https://picsum.photos/200/232', 1, 11),
|
||||
('How to create a successful marketing strategy', 'https://picsum.photos/200/233', 1, 6),
|
||||
('The importance of financial literacy', 'https://picsum.photos/200/234', 1, 14),
|
||||
('How to improve your public speaking skills', 'https://picsum.photos/200/235', 1, 13),
|
||||
('The rise of e-commerce in 2023', 'https://picsum.photos/200/236', 1, 10),
|
||||
('Exploring the benefits of mindfulness', 'https://picsum.photos/200/237', 1, 15),
|
||||
('The best tools for online collaboration', 'https://picsum.photos/200/238', 1, 20),
|
||||
('How to create a successful YouTube channel', 'https://picsum.photos/200/239', 1, 5),
|
||||
('The impact of technology on education', 'https://picsum.photos/200/240', 1, 8),
|
||||
('Understanding the basics of digital marketing', 'https://picsum.photos/200/241', 1, 12),
|
||||
('The benefits of volunteering', 'https://picsum.photos/200/242', 1, 7),
|
||||
('How to build a strong online presence', 'https://picsum.photos/200/243', 1, 9),
|
||||
('The importance of work-life balance', 'https://picsum.photos/200/244', 1, 11),
|
||||
('How to create a successful podcast', 'https://picsum.photos/200/245', 1, 6),
|
||||
('The role of technology in healthcare', 'https://picsum.photos/200/246', 1, 14),
|
||||
('How to stay productive while working from home', 'https://picsum.photos/200/247', 1, 13),
|
||||
('The rise of remote learning', 'https://picsum.photos/200/248', 1, 10),
|
||||
('Exploring the world of cryptocurrency', 'https://picsum.photos/200/249', 1, 15),
|
||||
('The best practices for online security', 'https://picsum.photos/200/250', 1, 20),
|
||||
('How to create engaging email campaigns', 'https://picsum.photos/200/251', 1, 5),
|
||||
('The impact of artificial intelligence on society', 'https://picsum.photos/200/252', 1, 8),
|
||||
('Understanding the basics of web development', 'https://picsum.photos/200/253', 1, 12),
|
||||
('The benefits of a healthy diet', 'https://picsum.photos/200/254', 1, 7),
|
||||
('How to manage your time effectively', 'https://picsum.photos/200/255', 1, 9),
|
||||
('The future of renewable energy', 'https://picsum.photos/200/256', 1, 11),
|
||||
('How to create a successful business plan', 'https://picsum.photos/200/257', 1, 6),
|
||||
('The importance of customer service', 'https://picsum.photos/200/258', 1, 14),
|
||||
('How to improve your writing skills', 'https://picsum.photos/200/259', 1, 13),
|
||||
('The rise of mobile apps', 'https://picsum.photos/200/260', 1, 10),
|
||||
('Exploring the benefits of yoga', 'https://picsum.photos/200/261', 1, 15),
|
||||
('The best tools for project management', 'https://picsum.photos/200/262', 1, 20),
|
||||
('How to create a successful online store', 'https://picsum.photos/200/263', 1, 5),
|
||||
('The impact of social media on business', 'https://picsum.photos/200/264', 1, 8),
|
||||
('Understanding the basics of graphic design', 'https://picsum.photos/200/265', 1, 12),
|
||||
('The benefits of lifelong learning', 'https://picsum.photos/200/266', 1, 7),
|
||||
('How to build a strong professional network', 'https://picsum.photos/200/267', 1, 9),
|
||||
('The importance of adaptability in the workplace', 'https://picsum.photos/200/268', 1, 11),
|
||||
('How to create a successful fundraising campaign', 'https://picsum.photos/200/269', 1, 6),
|
||||
('The role of social media in modern marketing', 'https://picsum.photos/200/270', 1, 14),
|
||||
('How to stay ahead in a competitive job market', 'https://picsum.photos/200/271', 1, 13),
|
||||
('The rise of subscription services', 'https://picsum.photos/200/272', 1, 10),
|
||||
('Exploring the world of augmented reality', 'https://picsum.photos/200/273', 1, 15),
|
||||
('The best practices for content marketing', 'https://picsum.photos/200/274', 1, 20),
|
||||
('How to create a successful affiliate marketing strategy', 'https://picsum.photos/200/275', 1, 5),
|
||||
('The impact of globalization on local businesses', 'https://picsum.photos/200/276', 1, 8),
|
||||
('Understanding the basics of user experience design', 'https://picsum.photos/200/277', 1, 12),
|
||||
('The benefits of a positive mindset', 'https://picsum.photos/200/278', 1, 7),
|
||||
('How to manage your finances effectively', 'https://picsum.photos/200/279', 1, 9),
|
||||
('The future of smart home technology', 'https://picsum.photos/200/280', 1, 11);
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "video-api",
|
||||
"version": "1.0.0",
|
||||
"description": "Video API with pagination",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"start": "node app.js",
|
||||
"dev": "nodemon app.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"mysql2": "^3.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.0.1"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user