@@ 154-198 (lines=45) @@ | ||
151 | def on_options(req, resp, id_): |
|
152 | resp.status = falcon.HTTP_200 |
|
153 | ||
154 | @staticmethod |
|
155 | def on_get(req, resp, id_): |
|
156 | if not id_.isdigit() or int(id_) <= 0: |
|
157 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
158 | description='API.INVALID_COMBINED_EQUIPMENT_ID') |
|
159 | ||
160 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
161 | cursor = cnx.cursor(dictionary=True) |
|
162 | ||
163 | query = (" SELECT id, name, uuid " |
|
164 | " FROM tbl_cost_centers ") |
|
165 | cursor.execute(query) |
|
166 | rows_cost_centers = cursor.fetchall() |
|
167 | ||
168 | cost_center_dict = dict() |
|
169 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
|
170 | for row in rows_cost_centers: |
|
171 | cost_center_dict[row['id']] = {"id": row['id'], |
|
172 | "name": row['name'], |
|
173 | "uuid": row['uuid']} |
|
174 | ||
175 | query = (" SELECT id, name, uuid, " |
|
176 | " is_input_counted, is_output_counted, " |
|
177 | " cost_center_id, description " |
|
178 | " FROM tbl_combined_equipments " |
|
179 | " WHERE id = %s ") |
|
180 | cursor.execute(query, (id_,)) |
|
181 | row = cursor.fetchone() |
|
182 | cursor.close() |
|
183 | cnx.disconnect() |
|
184 | ||
185 | if row is None: |
|
186 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
187 | description='API.COMBINED_EQUIPMENT_NOT_FOUND') |
|
188 | else: |
|
189 | cost_center = cost_center_dict.get(row['cost_center_id'], None) |
|
190 | meta_result = {"id": row['id'], |
|
191 | "name": row['name'], |
|
192 | "uuid": row['uuid'], |
|
193 | "is_input_counted": bool(row['is_input_counted']), |
|
194 | "is_output_counted": bool(row['is_output_counted']), |
|
195 | "cost_center": cost_center, |
|
196 | "description": row['description']} |
|
197 | ||
198 | resp.body = json.dumps(meta_result) |
|
199 | ||
200 | @staticmethod |
|
201 | def on_delete(req, resp, id_): |
@@ 154-198 (lines=45) @@ | ||
151 | def on_options(req, resp, id_): |
|
152 | resp.status = falcon.HTTP_200 |
|
153 | ||
154 | @staticmethod |
|
155 | def on_get(req, resp, id_): |
|
156 | if not id_.isdigit() or int(id_) <= 0: |
|
157 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
|
158 | description='API.INVALID_EQUIPMENT_ID') |
|
159 | ||
160 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
161 | cursor = cnx.cursor(dictionary=True) |
|
162 | ||
163 | query = (" SELECT id, name, uuid " |
|
164 | " FROM tbl_cost_centers ") |
|
165 | cursor.execute(query) |
|
166 | rows_cost_centers = cursor.fetchall() |
|
167 | ||
168 | cost_center_dict = dict() |
|
169 | if rows_cost_centers is not None and len(rows_cost_centers) > 0: |
|
170 | for row in rows_cost_centers: |
|
171 | cost_center_dict[row['id']] = {"id": row['id'], |
|
172 | "name": row['name'], |
|
173 | "uuid": row['uuid']} |
|
174 | ||
175 | query = (" SELECT id, name, uuid, " |
|
176 | " is_input_counted, is_output_counted, " |
|
177 | " cost_center_id, description " |
|
178 | " FROM tbl_equipments " |
|
179 | " WHERE id = %s ") |
|
180 | cursor.execute(query, (id_,)) |
|
181 | row = cursor.fetchone() |
|
182 | cursor.close() |
|
183 | cnx.disconnect() |
|
184 | ||
185 | if row is None: |
|
186 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND', |
|
187 | description='API.EQUIPMENT_NOT_FOUND') |
|
188 | else: |
|
189 | cost_center = cost_center_dict.get(row['cost_center_id'], None) |
|
190 | meta_result = {"id": row['id'], |
|
191 | "name": row['name'], |
|
192 | "uuid": row['uuid'], |
|
193 | "is_input_counted": bool(row['is_input_counted']), |
|
194 | "is_output_counted": bool(row['is_output_counted']), |
|
195 | "cost_center": cost_center, |
|
196 | "description": row['description']} |
|
197 | ||
198 | resp.body = json.dumps(meta_result) |
|
199 | ||
200 | @staticmethod |
|
201 | def on_delete(req, resp, id_): |