locCodeController.js 2.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import db from "../db";

const DB_QUERY_ERROR_MSG = "DB Query Error.";
const DB_CONNECTION_ERROR_MSG = "DB Connection Error.";
const SERVER_ERROR_MSG = "The server encountered an error.";
const QUERY_SUCCESS_MSG = "Query Success.";

const STATUS_OK_CODE = 200;
const STATUS_SERVER_ERROR_CODE = 500;

export const getDo = async (req, res) => {
  const query = "SELECT CODE, DONAME FROM LOCDO";

  db((connErr, connection) => {
    if (connErr) {
      console.log(DB_CONNECTION_ERROR_MSG);
      res.status(STATUS_SERVER_ERROR_CODE).json({ error: SERVER_ERROR_MSG });
    } else {
      connection.query(query, (queryErr, result) => {
        if (queryErr) {
          console.log(DB_QUERY_ERROR_MSG);
          res
            .status(STATUS_SERVER_ERROR_CODE)
            .json({ error: SERVER_ERROR_MSG });
        } else {
          console.log(QUERY_SUCCESS_MSG);
          res.status(STATUS_OK_CODE).json({ info: result });
        }
      });

      connection.release();
    }
  });
};

export const getSGG = (req, res) => {
  const {
    params: { id },
  } = req;

  const query = `SELECT CODE, SGGNAME FROM LOCSIGUNGU WHERE DOCODE = ${id}`;

  db((connErr, connection) => {
    if (connErr) {
      console.log(DB_CONNECTION_ERROR_MSG);
      res.status(STATUS_SERVER_ERROR_CODE).json({ error: SERVER_ERROR_MSG });
    } else {
      connection.query(query, (queryErr, result) => {
        if (queryErr) {
          console.log(DB_QUERY_ERROR_MSG);
          res
            .status(STATUS_SERVER_ERROR_CODE)
            .json({ error: SERVER_ERROR_MSG });
        } else {
          console.log(QUERY_SUCCESS_MSG);
          res.status(STATUS_OK_CODE).json({ info: result });
        }
      });

      connection.release();
    }
  });
};

export const getEMD = (req, res) => {
  const {
    params: { id },
  } = req;

  const query = `SELECT CODE, EMDNAME FROM LOCINFO WHERE SGGCODE = ${id}`;

  db((connErr, connection) => {
    if (connErr) {
      console.log(DB_CONNECTION_ERROR_MSG);
      res.status(STATUS_SERVER_ERROR_CODE).json({ error: SERVER_ERROR_MSG });
    } else {
      connection.query(query, (queryErr, result) => {
        if (queryErr) {
          console.log(DB_QUERY_ERROR_MSG);
          res
            .status(STATUS_SERVER_ERROR_CODE)
            .json({ error: SERVER_ERROR_MSG });
        } else {
          console.log(QUERY_SUCCESS_MSG);
          res.status(STATUS_OK_CODE).json({ info: result });
        }
      });

      connection.release();
    }
  });
};