All files / src/api/controllers thingyController.ts

0% Statements 0/51
0% Branches 0/7
0% Functions 0/4
0% Lines 0/51

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 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                                                                                                                                                                                     
import { Request, Response } from "express";
import { ThingyService } from "../services/thingyService";
import { MongooseThingyRepository } from "../repositories/mongoose/thingyRepository";
import { buildThingyFilters } from "../utils/filterBuilder";
 
const thingyRepository = new MongooseThingyRepository();
const thingyService = new ThingyService(thingyRepository);
 
export class ThingyController {
 
  // Get all devices
  async getThingysData(req: Request, res: Response) : Promise<void> {
    try {
      const filters = buildThingyFilters(req.query);
      const thingys = await thingyService.getThingysData(filters);
      res.status(200).json(thingys);
    } catch (error) {
      console.error("Error fetching devices:", error);
      res.status(500).json({ error: "An unexpected error occurred while fetching devices" });
    }
  };
 
  // Get a specific device by ID
  async getThingyDataByMacAddress(req: Request, res: Response): Promise<void> {
    try {
      const { macAddress } = req.params;
      console.log("macAddress", macAddress);
      Iif (!macAddress) {
        res.status(400).json({ message: "Mac address is required" });
        return;
      }
      const thingy = await thingyService.getThingyDataByMacAddress(macAddress);
      Iif (!thingy) {
        res.status(404).json({ message: "Thingy not found" });
        return;
      }
      res.status(200).json(thingy);
    } catch (error) {
      console.error("Error fetching device:", error);
      res.status(500).json({ error: "An unexpected error occurred while fetching devices" });
    }
  };
 
  async createThingyData(req: Request, res: Response): Promise<void> {
    try {
      const thingyDataArray = req.body; // Expecting an array of thingy data
      console.log("thingyDataArray", thingyDataArray);
  
      Iif (!Array.isArray(thingyDataArray) || thingyDataArray.length === 0) {
        res.status(400).json({ message: "Invalid request body or empty data array" });
        return;
      }
  
      // Validate each entry in the array
      for (const thingyData of thingyDataArray) {
        Iif (!thingyData.macAddress) {
          res.status(400).json({ message: "Each thingy data must include a macAddress" });
          return;
        }
      }
  
      // Save the array of thingy data
      const createdThingys = [];
      for (const thingyData of thingyDataArray) {
        const createdThingy = await thingyService.createThingyData(thingyData);
        createdThingys.push(createdThingy);
      }
      res.status(200).json(createdThingys);
    } catch (error) {
      console.error("Error creating device:", error);
      res.status(500).json({ error: "An unexpected error occurred while creating devices" });
    }
  }
 
  async deleteThingyById(req: Request, res: Response): Promise<void> {
    try {
      const { id } = req.params;
 
      const result = await thingyService.deleteThingy(id);
      Iif (!result) {
        res.status(404).json({ message: "Thingy not found" });
        return;
      }
 
      res.status(200).json({ message: "Thingy deleted successfully" });
    } catch (error) {
      console.error("Error deleting Thingy:", error);
      res.status(500).json({ error: "An unexpected error occurred while deleting the Thingy" });
    }
  };
};