All files / src/api/controllers fingerprintController.ts

0% Statements 0/33
0% Branches 0/9
0% Functions 0/3
0% Lines 0/33

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                                                                                                                                                 
import { Request, Response } from "express";
import { FingerprintService } from "../services/fingerprintService";
import { MongooseFingerprintRepository } from "../repositories/mongoose/fingerprintRepository";
 
const fingerprintRepository = new MongooseFingerprintRepository();
const fingerprintService = new FingerprintService(fingerprintRepository);
 
export class FingerprintController {
  async getFingerprint(req: Request, res: Response): Promise<void> {
    try {
      const { macAddress } = req.params;
 
      const result = await fingerprintService.getFingerprint(macAddress);
 
      Iif (!result) {
        res.status(404).json({ message: "Fingerprint not found" });
        return;
      }
 
      res.status(200).json({
        macAddress: result.macAddress,
        timestamp: result.timestamp,
        location: result.location,
        beacons: result.beacons
      });
    } catch (error) {
      res.status(500).json({ error: "Failed, internal server error" });
    }
  }
 
  async updateFingerprint(req: Request, res: Response): Promise<void> {
    try {
      const { macAddress } = req.params;
      const data = req.body;
 
      Iif (!data || !data.macAddress || !data.timestamp || !data.location || !data.beacons) {
        res.status(400).json({
          message: "Invalid request body"
        });
        return;
      }
 
      Iif (data.macAddress !== macAddress) {
        res.status(400).json({
          message: "MAC address in body does not match URL"
        });
        return;
      }
 
      const result = await fingerprintService.updateFingerprint(macAddress, data);
 
      res.status(200).json(result);
    } catch (error) {
      res.status(500).json({ error: "Failed, internal server error" });
    }
  }
 
  async deleteFingerprint(req: Request, res: Response): Promise<void> {
    try {
      const { macAddress } = req.params;
      const result = await fingerprintService.deleteFingerprint(macAddress);
 
      Iif (!result) {
        res.status(404).json({ message: "Fingerprint not found" });
        return;
      }
 
      res.status(200).json({ message: "Fingerprint deleted" });
    } catch (error) {
      res.status(500).json({ error: "Failed, internal server error" });
    }
  }
}