feat: added controller for new services
This commit is contained in:
parent
5a5dcb6334
commit
7b9c6ae5b8
@ -1,5 +1,7 @@
|
|||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
|
import { StatusCodes } from "http-status-codes";
|
||||||
import { container } from "tsyringe";
|
import { container } from "tsyringe";
|
||||||
|
import { authenticateHeader } from "../middleware/authentificationMiddleware";
|
||||||
import { validateData } from "../middleware/validationMiddleware";
|
import { validateData } from "../middleware/validationMiddleware";
|
||||||
import { TtnMessage } from "../models/ttnMessage";
|
import { TtnMessage } from "../models/ttnMessage";
|
||||||
import { LocationService } from "../services/locationService";
|
import { LocationService } from "../services/locationService";
|
||||||
@ -7,8 +9,6 @@ import { LpTtnEndDeviceUplinksService } from "../services/lpTtnEndDeviceUplinksS
|
|||||||
import { TtnGatewayReceptionService } from "../services/ttnGatewayReceptionService";
|
import { TtnGatewayReceptionService } from "../services/ttnGatewayReceptionService";
|
||||||
import { WifiScanService } from "../services/wifiScanService";
|
import { WifiScanService } from "../services/wifiScanService";
|
||||||
import { ttnMessageValidator } from "../validation/ttn/ttnMessageValidation";
|
import { ttnMessageValidator } from "../validation/ttn/ttnMessageValidation";
|
||||||
import { authenticateHeader } from "../middleware/authentificationMiddleware";
|
|
||||||
import { StatusCodes } from "http-status-codes";
|
|
||||||
|
|
||||||
const lpTtnEndDeviceUplinksService = container.resolve(
|
const lpTtnEndDeviceUplinksService = container.resolve(
|
||||||
LpTtnEndDeviceUplinksService
|
LpTtnEndDeviceUplinksService
|
||||||
@ -94,9 +94,9 @@ router.post(
|
|||||||
gnss:
|
gnss:
|
||||||
gnnsLocation.latitude && gnnsLocation.longitude
|
gnnsLocation.latitude && gnnsLocation.longitude
|
||||||
? {
|
? {
|
||||||
latitude: gnnsLocation.latitude,
|
latitude: gnnsLocation.latitude,
|
||||||
longitude: gnnsLocation.longitude,
|
longitude: gnnsLocation.longitude,
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -104,7 +104,9 @@ router.post(
|
|||||||
res.status(StatusCodes.OK).send();
|
res.status(StatusCodes.OK).send();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: "Error creating uplink" });
|
res
|
||||||
|
.status(StatusCodes.INTERNAL_SERVER_ERROR)
|
||||||
|
.json({ error: "Error creating uplink" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
32
server/src/controller/wifiLocationController.ts
Normal file
32
server/src/controller/wifiLocationController.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import express, { Request, Response } from "express";
|
||||||
|
import { container } from "tsyringe";
|
||||||
|
import { WifiLocationService } from "../services/wifiLocationService";
|
||||||
|
|
||||||
|
const wifiLocationService = container.resolve(WifiLocationService);
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const wifiLocations = await wifiLocationService.getAllWifiLocations();
|
||||||
|
res.status(200).json(wifiLocations);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
res.status(500).json({ error: "Error retrieving wifi location" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.delete("/:id", async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
const deleted = await wifiLocationService.deleteWifiLocation(id);
|
||||||
|
if (!deleted) {
|
||||||
|
res.status(404).json({ error: "Wifi Location not found" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.status(204).send();
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: "Error deleting wifi location" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
35
server/src/controller/wifiLocationHistoryController.ts
Normal file
35
server/src/controller/wifiLocationHistoryController.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import express, { Request, Response } from "express";
|
||||||
|
import { container } from "tsyringe";
|
||||||
|
import { WifiLocationHistoryService } from "../services/wifiLocationHistoryService";
|
||||||
|
|
||||||
|
const wifiLocationHistoryService = container.resolve(
|
||||||
|
WifiLocationHistoryService
|
||||||
|
);
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const wifiLocationHistory =
|
||||||
|
await wifiLocationHistoryService.getAllWifiLocationHistories();
|
||||||
|
res.status(200).json(wifiLocationHistory);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: "Error retrieving wifi location history" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/:id", async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
const wifiLocationHistory =
|
||||||
|
await wifiLocationHistoryService.getWifiLocationHistoryById(id);
|
||||||
|
if (!wifiLocationHistory) {
|
||||||
|
res.status(404).json({ error: "Wifi location history not found" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.status(200).json(wifiLocationHistory);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: "Error retrieving wifi location history" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
@ -7,6 +7,8 @@ import locationRoutes from "./controller/locationController";
|
|||||||
import lpTtnEndDeviceUplinksRoutes from "./controller/lpTtnEndDeviceUplinksController";
|
import lpTtnEndDeviceUplinksRoutes from "./controller/lpTtnEndDeviceUplinksController";
|
||||||
import ttnRoutes from "./controller/ttnController";
|
import ttnRoutes from "./controller/ttnController";
|
||||||
import ttnGatewayReceptionRoutes from "./controller/ttnGatewayReceptionController";
|
import ttnGatewayReceptionRoutes from "./controller/ttnGatewayReceptionController";
|
||||||
|
import wifiLocationRoutes from "./controller/wifiLocationController";
|
||||||
|
import wifiLocationHistoryRoutes from "./controller/wifiLocationHistoryController";
|
||||||
import wifiScanRoutes from "./controller/wifiScanController";
|
import wifiScanRoutes from "./controller/wifiScanController";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
@ -18,8 +20,10 @@ app.use(cors());
|
|||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.use("/api/lp-ttn-end-device-uplinks", lpTtnEndDeviceUplinksRoutes);
|
app.use("/api/lp-ttn-end-device-uplinks", lpTtnEndDeviceUplinksRoutes);
|
||||||
app.use("/api/wifi-scans", wifiScanRoutes);
|
|
||||||
app.use("/api/ttn-gateway-receptions", ttnGatewayReceptionRoutes);
|
app.use("/api/ttn-gateway-receptions", ttnGatewayReceptionRoutes);
|
||||||
|
app.use("/api/wifi-location-history", wifiLocationHistoryRoutes);
|
||||||
|
app.use("/api/wifi-location", wifiLocationRoutes);
|
||||||
|
app.use("/api/wifi-scans", wifiScanRoutes);
|
||||||
app.use("/api/locations", locationRoutes);
|
app.use("/api/locations", locationRoutes);
|
||||||
app.use("/api/ttn", ttnRoutes);
|
app.use("/api/ttn", ttnRoutes);
|
||||||
|
|
||||||
|
@ -23,21 +23,4 @@ export class WifiLocationHistoryRepository {
|
|||||||
public async createMany(data: Partial<WifiLocationHistory>[]) {
|
public async createMany(data: Partial<WifiLocationHistory>[]) {
|
||||||
return await WifiLocationHistory.bulkCreate(data);
|
return await WifiLocationHistory.bulkCreate(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async update(id: string, data: Partial<WifiLocationHistory>) {
|
|
||||||
const wifiScan = await this.findById(id);
|
|
||||||
if (wifiScan) {
|
|
||||||
return await wifiScan.update(data);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async delete(id: string) {
|
|
||||||
const wifiScan = await this.findById(id);
|
|
||||||
if (wifiScan) {
|
|
||||||
await wifiScan.destroy();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ export class WifiLocationHistoryService {
|
|||||||
constructor(
|
constructor(
|
||||||
@inject(WifiLocationHistoryRepository)
|
@inject(WifiLocationHistoryRepository)
|
||||||
private repository: WifiLocationHistoryRepository
|
private repository: WifiLocationHistoryRepository
|
||||||
) { }
|
) {}
|
||||||
|
|
||||||
public async getAllWifiLocationHistories() {
|
public async getAllWifiLocationHistories() {
|
||||||
return this.repository.findAll();
|
return this.repository.findAll();
|
||||||
@ -38,20 +38,14 @@ export class WifiLocationHistoryService {
|
|||||||
order: [["updated_at_utc", "DESC"]],
|
order: [["updated_at_utc", "DESC"]],
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!existingEntry || existingEntry.latitude !== data.latitude || existingEntry.longitude !== data.longitude) {
|
if (
|
||||||
|
!existingEntry ||
|
||||||
|
existingEntry.latitude !== data.latitude ||
|
||||||
|
existingEntry.longitude !== data.longitude
|
||||||
|
) {
|
||||||
return await this.repository.create(data);
|
return await this.repository.create(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return existingEntry;
|
return existingEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateWifiLocationHistory(
|
|
||||||
data: UpdateWifiLocationHistoryParams
|
|
||||||
) {
|
|
||||||
return this.repository.update(data.mac, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async deleteWifiLocationHistory(id: string) {
|
|
||||||
return this.repository.delete(id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user