Dev-FE-1 / team2-intranet-project-

토이프로젝트I - 2조 기업용 사내 인트라넷 개발
https://organic-meggy-toyproject-group2-intranet-solution-b9fd064a.koyeb.app/
0 stars 1 forks source link

백엔드 데이터베이스 구축, 직원 CRUD API 구현 #24

Closed nakyeonko3 closed 3 months ago

nakyeonko3 commented 4 months ago

Tasks

예상 기간

토요일 23시 59분까지 완료 예정

당장 필요한 API 위주로 개발할 예정. 필요한 API 가 있으면 댓글로 적어주세요

nakyeonko3 commented 4 months ago

인메모리 데이터 베이스 구현완료

import sqlite3 from 'sqlite3';

class ConnectionMaker {
  constructor() {}

  makeConnection() {
    const db = new sqlite3.Database(`:memory:`, (err) => {
      if (err) {
        console.error('Error connecting to database:', err);
      } else {
        console.log('Connected to the in-memory SQlite database.');
      }
    });

    return db;
  }
}

export default class InMemoDatabase {
  constructor() {
    this.db = new ConnectionMaker().makeConnection();
    this.createTable();
  }

  createTable() {
    this.db.serialize(() => {
      this.db.run(`
        CREATE TABLE IF NOT EXISTS Employees (
          id INTEGER PRIMARY KEY AUTOINCREMENT,
          name TEXT NOT NULL,
          email TEXT,
          phone TEXT,
          position TEXT,
          profileImg TEXT,
          password TEXT
        )`);
    });
  }

  insertEmployees(employees) {
    (employees || []).forEach((employee) => {
      this.insertEmployee(employee);
    });
  }

  insertEmployee({ name, email, phone, position, profileImg }) {
    const sql = `INSERT INTO Employees (name, email, phone, position,  profileImg) VALUES (?, ?, ?, ?, ?)`;
    this.db.run(sql, [name, email, phone, position, profileImg], (err) => {
      if (err) {
        console.error('Error inserting Employee:', err);
      }
    });
  }

  getAllEmployees(callback) {
    const sql = 'SELECT * FROM Employees';
    this.db.all(sql, [], (err, rows) => {
      if (err) {
        console.error('Error selecting all Employees:', err);
      }
      callback(rows);
    });
  }
}