103 lines
2.7 KiB
JavaScript
103 lines
2.7 KiB
JavaScript
const { v4: uuidv4 } = require('uuid');
|
|
const database = require('../config/database');
|
|
|
|
class CommitFilesModel {
|
|
constructor() {
|
|
this.db = database;
|
|
}
|
|
|
|
async createCommitFile(fileData) {
|
|
const {
|
|
commit_id,
|
|
change_type,
|
|
file_path
|
|
} = fileData;
|
|
|
|
const query = `
|
|
INSERT INTO repository_commit_files (
|
|
id, commit_id, change_type, file_path, created_at
|
|
) VALUES ($1, $2, $3, $4, NOW())
|
|
RETURNING *
|
|
`;
|
|
|
|
const values = [
|
|
uuidv4(),
|
|
commit_id,
|
|
change_type,
|
|
file_path
|
|
];
|
|
|
|
const result = await this.db.query(query, values);
|
|
return result.rows[0];
|
|
}
|
|
|
|
async createMultipleCommitFiles(commit_id, files) {
|
|
if (!files || files.length === 0) return [];
|
|
|
|
const values = [];
|
|
const placeholders = [];
|
|
|
|
files.forEach((file, index) => {
|
|
const baseIndex = index * 4;
|
|
placeholders.push(`($${baseIndex + 1}, $${baseIndex + 2}, $${baseIndex + 3}, $${baseIndex + 4}, NOW())`);
|
|
values.push(uuidv4(), commit_id, file.change_type, file.file_path);
|
|
});
|
|
|
|
const query = `
|
|
INSERT INTO repository_commit_files (
|
|
id, commit_id, change_type, file_path, created_at
|
|
) VALUES ${placeholders.join(', ')}
|
|
RETURNING *
|
|
`;
|
|
|
|
const result = await this.db.query(query, values);
|
|
return result.rows;
|
|
}
|
|
|
|
async getFilesByCommit(commit_id) {
|
|
const query = `
|
|
SELECT * FROM repository_commit_files
|
|
WHERE commit_id = $1
|
|
ORDER BY file_path
|
|
`;
|
|
|
|
const result = await this.db.query(query, [commit_id]);
|
|
return result.rows;
|
|
}
|
|
|
|
async getFilesByRepository(repository_id, limit = 100, offset = 0) {
|
|
const query = `
|
|
SELECT rcf.*, rcd.commit_sha, rcd.committed_at
|
|
FROM repository_commit_files rcf
|
|
JOIN repository_commit_details rcd ON rcf.commit_id = rcd.id
|
|
WHERE rcd.repository_id = $1
|
|
ORDER BY rcd.committed_at DESC, rcf.file_path
|
|
LIMIT $2 OFFSET $3
|
|
`;
|
|
|
|
const result = await this.db.query(query, [repository_id, limit, offset]);
|
|
return result.rows;
|
|
}
|
|
|
|
async getFileChangesByPath(repository_id, file_path) {
|
|
const query = `
|
|
SELECT rcf.*, rcd.commit_sha, rcd.committed_at, rcd.author_name
|
|
FROM repository_commit_files rcf
|
|
JOIN repository_commit_details rcd ON rcf.commit_id = rcd.id
|
|
WHERE rcd.repository_id = $1 AND rcf.file_path = $2
|
|
ORDER BY rcd.committed_at DESC
|
|
`;
|
|
|
|
const result = await this.db.query(query, [repository_id, file_path]);
|
|
return result.rows;
|
|
}
|
|
|
|
async deleteFilesByCommit(commit_id) {
|
|
const query = `DELETE FROM repository_commit_files WHERE commit_id = $1`;
|
|
const result = await this.db.query(query, [commit_id]);
|
|
return result.rowCount;
|
|
}
|
|
}
|
|
|
|
module.exports = CommitFilesModel;
|