import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
@Injectable()
export class RawTasksService {
constructor(private readonly dataSource: DataSource) {}
// 1. Thêm mới dữ liệu (INSERT)
async createTask(title: string, description: string) {
// MSSQL sử dụng từ khóa OUTPUT để lấy về giá trị của dòng vừa được chèn vào
const sql = `
INSERT INTO tasks (title, description)
OUTPUT inserted.id, inserted.title
VALUES (?, ?);
`;
const result = await this.dataSource.query(sql, [title, description]);
return result[0];
}
// 2. Cập nhật dữ liệu (UPDATE)
async updateTaskStatus(id: number, status: string) {
const sql = `
UPDATE tasks
SET status = ?
WHERE id = ?;
`;
await this.dataSource.query(sql, [status, id]);
return { success: true, message: `Đã cập nhật trạng thái tác vụ #${id}` };
}
// 3. Xóa dữ liệu (DELETE)
async deleteTask(id: number) {
const sql = `
DELETE FROM tasks
WHERE id = ?;
`;
await this.dataSource.query(sql, [id]);
return { success: true, message: `Đã xóa tác vụ #${id} thành công` };
}
}