All files / src/api/controllers deviceController.ts

0% Statements 0/34
0% Branches 0/2
0% Functions 0/4
0% Lines 0/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                 
import { Request, Response } from "express";
import { DeviceService } from "../services/deviceService";
import { MongooseDeviceRepository } from "../repositories/mongoose/deviceRepository";
import { buildDeviceFilters } from "../utils/filterBuilder";
 
 
const deviceRepository = new MongooseDeviceRepository();
const deviceService = new DeviceService(deviceRepository);
 
export class DeviceController {
  async getDevices(req: Request, res: Response): Promise<void> {
    try {
      const filters = buildDeviceFilters(req.query);
      const devices = await deviceService.filterDevices(filters);
      res.status(200).json(devices);
    } catch (error) {
      console.error('Error fetching devices:', error);
      res.status(500).json({ message: 'Failed to fetch devices', error });
    }
  }
 
  async updateDevice(req: Request, res: Response): Promise<void> {
    try {
      const newDevice = await deviceService.updateDevice(req.body);
      res.status(200).json(newDevice);
    } catch (error) {
      console.error("Error updating device:", error);
      res.status(400).json({ message: "Error updating device", error });
    }
  };
 
    async createDevice(req: Request, res: Response): Promise<void> {
    try {
      const newDevice = await deviceService.addDevice(req.body);
      res.status(200).json(newDevice);
    } catch (error) {
      console.error("Error updating device:", error);
      res.status(400).json({ message: "Error updating device", error });
    }
  };
 
  async deleteDevice(req: Request, res: Response): Promise<void> {
    try {
      const { macAddress } = req.params;
 
      Iif (!macAddress) {
        res.status(400).json({ message: "Mac address is required" });
        return;
      }
 
      const result = await deviceService.deleteDevice(macAddress);
      Iif (!result) {
        res.status(404).json({ message: "Device not found" });
        return;
      }
 
      res.status(200).json({ message: "Device deleted successfully" });
    } catch (error) {
      console.error("Error deleting Device:", error);
      res.status(500).json({ error: "An unexpected error occurred while deleting the Device" });
    }
  };
};