feat: initial setup backend with default routes and db connection

This commit is contained in:
2024-12-30 01:36:41 +01:00
parent 478a53a0fb
commit 26e6cd0b7e
28 changed files with 3062 additions and 1 deletions

View File

@ -0,0 +1,17 @@
import dotenv from "dotenv";
dotenv.config();
console.log(process.env.NODE_ENV);
export const config = {
username: process.env.DB_USER!,
password: process.env.DB_PASSWORD!,
database: process.env.DB_NAME!,
host: process.env.DB_HOST!,
dialect: process.env.DB_DIALECT! as
| "mysql"
| "mariadb"
| "postgres"
| "sqlite"
| "mssql",
port: parseInt(process.env.DB_PORT as string, 10),
};

View File

@ -0,0 +1,68 @@
import express, { Request, Response } from "express";
import { container } from "tsyringe";
import { LocationService } from "../services/locationService";
const locationService = container.resolve(LocationService);
const router = express.Router();
router.get("/", async (req: Request, res: Response) => {
try {
const locations = await locationService.getAllLocations();
res.status(200).json(locations);
} catch (error) {
res.status(500).json({ error: "Error retrieving locations" });
}
});
router.get("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const location = await locationService.getLocationById(id);
if (!location) {
res.status(404).json({ error: "Location not found" });
return;
}
res.status(200).json(location);
} catch (error) {
res.status(500).json({ error: "Error retrieving location" });
}
});
router.post("/", async (req: Request, res: Response) => {
try {
const newLocation = await locationService.createLocation(req.body);
res.status(201).json(newLocation);
} catch (error) {
res.status(500).json({ error: "Error creating location" });
}
});
router.put("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const updatedLocation = await locationService.updateLocation(id, req.body);
if (!updatedLocation) {
res.status(404).json({ error: "Location not found" });
return;
}
res.status(200).json(updatedLocation);
} catch (error) {
res.status(500).json({ error: "Error updating location" });
}
});
router.delete("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const deleted = await locationService.deleteLocation(id);
if (!deleted) {
res.status(404).json({ error: "Location not found" });
return;
}
res.status(204).send();
} catch (error) {
res.status(500).json({ error: "Error deleting location" });
}
});
export default router;

View File

@ -0,0 +1,75 @@
import express, { Request, Response } from "express";
import { container } from "tsyringe";
import { LpTtnEndDeviceUplinksService } from "../services/lpTtnEndDeviceUplinksService";
const lpTtnEndDeviceUplinksService = container.resolve(
LpTtnEndDeviceUplinksService
);
const router = express.Router();
router.get("/", async (req: Request, res: Response) => {
try {
const uplinks = await lpTtnEndDeviceUplinksService.getAllUplinks();
res.status(200).json(uplinks);
} catch (error) {
console.log(error);
res.status(500).json({ error: "Error retrieving uplinks" });
}
});
router.get("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const uplink = await lpTtnEndDeviceUplinksService.getUplinkById(id);
if (!uplink) {
res.status(404).json({ error: "Uplink not found" });
return;
}
res.status(200).json(uplink);
} catch (error) {
res.status(500).json({ error: "Error retrieving uplink" });
}
});
router.post("/", async (req: Request, res: Response) => {
try {
const newUplink = await lpTtnEndDeviceUplinksService.createUplink(req.body);
res.status(201).json(newUplink);
} catch (error) {
console.log(error);
res.status(500).json({ error: "Error creating uplink" });
}
});
router.put("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const updatedUplink = await lpTtnEndDeviceUplinksService.updateUplink(
id,
req.body
);
if (!updatedUplink) {
res.status(404).json({ error: "Uplink not found" });
return;
}
res.status(200).json(updatedUplink);
} catch (error) {
res.status(500).json({ error: "Error updating uplink" });
}
});
router.delete("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const deleted = await lpTtnEndDeviceUplinksService.deleteUplink(id);
if (!deleted) {
res.status(404).json({ error: "Uplink not found" });
return;
}
res.status(204).send();
} catch (error) {
res.status(500).json({ error: "Error deleting uplink" });
}
});
export default router;

View File

@ -0,0 +1,74 @@
import express, { Request, Response } from "express";
import { TtnGatewayReceptionService } from "../services/ttnGatewayReceptionService";
import { container } from "tsyringe";
const ttnGatewayReceptionService = container.resolve(
TtnGatewayReceptionService
);
const router = express.Router();
router.get("/", async (req: Request, res: Response) => {
try {
const gatewayReceptions =
await ttnGatewayReceptionService.getAllGatewayReceptions();
res.status(200).json(gatewayReceptions);
} catch (error) {
res.status(500).json({ error: "Error retrieving gateway receptions" });
}
});
router.get("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const gatewayReception =
await ttnGatewayReceptionService.getGatewayReceptionById(id);
if (!gatewayReception) {
res.status(404).json({ error: "Gateway reception not found" });
return;
}
res.status(200).json(gatewayReception);
} catch (error) {
res.status(500).json({ error: "Error retrieving gateway reception" });
}
});
router.post("/", async (req: Request, res: Response) => {
try {
const newGatewayReception =
await ttnGatewayReceptionService.createGatewayReception(req.body);
res.status(201).json(newGatewayReception);
} catch (error) {
res.status(500).json({ error: "Error creating gateway reception" });
}
});
router.put("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const updatedGatewayReception =
await ttnGatewayReceptionService.updateGatewayReception(id, req.body);
if (!updatedGatewayReception) {
res.status(404).json({ error: "Gateway reception not found" });
return;
}
res.status(200).json(updatedGatewayReception);
} catch (error) {
res.status(500).json({ error: "Error updating gateway reception" });
}
});
router.delete("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const deleted = await ttnGatewayReceptionService.deleteGatewayReception(id);
if (!deleted) {
res.status(404).json({ error: "Gateway reception not found" });
return;
}
res.status(204).send();
} catch (error) {
res.status(500).json({ error: "Error deleting gateway reception" });
}
});
export default router;

View File

@ -0,0 +1,70 @@
import express, { Request, Response } from "express";
import { container } from "tsyringe";
import { WifiScanService } from "../services/wifiScanService";
const wifiScanService = container.resolve(WifiScanService);
const router = express.Router();
router.get("/", async (req: Request, res: Response) => {
try {
const wifiScans = await wifiScanService.getAllWifiScans();
res.status(200).json(wifiScans);
} catch (error) {
res.status(500).json({ error: "Error retrieving wifi scans" });
}
});
router.get("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const wifiScan = await wifiScanService.getWifiScanById(id);
if (!wifiScan) {
res.status(404).json({ error: "Wifi scan not found" });
return;
}
res.status(200).json(wifiScan);
} catch (error) {
res.status(500).json({ error: "Error retrieving wifi scan" });
}
});
router.post("/", async (req: Request, res: Response) => {
try {
const newWifiScan = await wifiScanService.createWifiScan(req.body);
res.status(201).json(newWifiScan);
} catch (error) {
res.status(500).json({ error: "Error creating wifi scan" });
}
});
router.put("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const updatedWifiScan = await wifiScanService.updateWifiScan(id, req.body);
if (!updatedWifiScan) {
res.status(404).json({ error: "Wifi scan not found" });
return;
}
res.status(200).json(updatedWifiScan);
} catch (error) {
res.status(500).json({ error: "Error updating wifi scan" });
}
});
router.delete("/:id", async (req: Request, res: Response) => {
try {
const { id } = req.params;
const deleted = await wifiScanService.deleteWifiScan(id);
if (!deleted) {
res.status(404).json({ error: "Wifi scan not found" });
return;
}
res.status(204).send();
} catch (error) {
res.status(500).json({ error: "Error deleting wifi scan" });
}
});
router.delete("/:id", async (req: Request, res: Response) => {});
export default router;

View File

@ -0,0 +1,14 @@
import { Sequelize } from "sequelize";
import { config } from "../config/config";
export const sequelize = new Sequelize(
config.database,
config.username,
config.password,
{
host: config.host,
dialect: config.dialect,
port: config.port,
logging: false,
}
);

26
server/src/index.ts Normal file
View File

@ -0,0 +1,26 @@
import dotenv from "dotenv";
import express from "express";
import "reflect-metadata";
const cors = require("cors");
import locationRoutes from "./controller/locationController";
import lpTtnEndDeviceUplinksRoutes from "./controller/lpTtnEndDeviceUplinksController";
import ttnGatewayReceptionRoutes from "./controller/ttnGatewayReceptionController";
import wifiScanRoutes from "./controller/wifiScanController";
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
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/locations", locationRoutes);
app.listen(PORT, () => {
console.log(`🚀 Server läuft auf http://localhost:${PORT}`);
});

View File

@ -0,0 +1,60 @@
import { DataTypes, Model } from "sequelize";
import { sequelize } from "../database/database";
export class Location extends Model {
public location_id!: string;
public lp_ttn_end_device_uplinks_id!: string;
public wifi_latitude!: number;
public wifi_longitude!: number;
public gnss_latitude!: number;
public gnss_longitude!: number;
public created_at_utc!: Date;
public updated_at_utc!: Date;
}
Location.init(
{
location_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
lp_ttn_end_device_uplinks_id: {
type: DataTypes.UUID,
allowNull: false,
},
wifi_latitude: {
type: DataTypes.NUMBER,
allowNull: true,
},
wifi_longitude: {
type: DataTypes.NUMBER,
allowNull: true,
},
gnss_latitude: {
type: DataTypes.NUMBER,
allowNull: true,
},
gnss_longitude: {
type: DataTypes.NUMBER,
allowNull: true,
},
created_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
updated_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
},
{
sequelize,
modelName: "Location",
tableName: "location",
timestamps: false,
}
);

View File

@ -0,0 +1,80 @@
import { DataTypes, Model } from "sequelize";
import { sequelize } from "../database/database";
export class LpTtnEndDeviceUplinks extends Model {
public lp_ttn_end_device_uplinks_id!: string;
public device_id!: string;
public application_ids!: string;
public dev_eui!: string;
public join_eui!: string;
public dev_addr!: string;
public received_at_utc!: Date;
public battery!: number;
public latitude!: number;
public longitude!: number;
public created_at_utc!: Date;
public updated_at_utc!: Date;
}
LpTtnEndDeviceUplinks.init(
{
lp_ttn_end_device_uplinks_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
device_id: {
type: DataTypes.STRING,
allowNull: false,
},
application_ids: {
type: DataTypes.STRING,
allowNull: true,
},
dev_eui: {
type: DataTypes.STRING,
allowNull: true,
},
join_eui: {
type: DataTypes.STRING,
allowNull: true,
},
dev_addr: {
type: DataTypes.STRING,
allowNull: true,
},
received_at_utc: {
type: DataTypes.DATE,
allowNull: true,
},
battery: {
type: DataTypes.NUMBER,
allowNull: true,
},
latitude: {
type: DataTypes.NUMBER,
allowNull: true,
},
longitude: {
type: DataTypes.NUMBER,
allowNull: true,
},
created_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
updated_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
},
{
sequelize,
modelName: "LpTtnEndDeviceUplinks",
tableName: "lp_ttn_end_device_uplinks",
timestamps: false,
}
);

View File

@ -0,0 +1,70 @@
import { DataTypes, Model } from "sequelize";
import { sequelize } from "../database/database";
export class TtnGatewayReception extends Model {
public ttn_gateway_reception_id!: string;
public lp_ttn_end_device_uplinks_id!: string;
public gateway_id!: string;
public eui!: string;
public rssi!: number;
public latitude!: number;
public longitude!: number;
public altitude!: number;
public created_at_utc!: Date;
public updated_at_utc!: Date;
}
TtnGatewayReception.init(
{
ttn_gateway_reception_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
lp_ttn_end_device_uplinks_id: {
type: DataTypes.UUID,
allowNull: false,
},
gateway_id: {
type: DataTypes.STRING,
allowNull: false,
},
eui: {
type: DataTypes.STRING,
allowNull: false,
},
rssi: {
type: DataTypes.NUMBER,
allowNull: true,
},
latitude: {
type: DataTypes.NUMBER,
allowNull: true,
},
longitude: {
type: DataTypes.NUMBER,
allowNull: true,
},
altitude: {
type: DataTypes.NUMBER,
allowNull: true,
},
created_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
updated_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
},
{
sequelize,
modelName: "TtnGatewayReception",
tableName: "ttn_gateway_reception",
timestamps: false,
}
);

View File

@ -0,0 +1,50 @@
import { DataTypes, Model } from "sequelize";
import { sequelize } from "../database/database";
export class WifiScan extends Model {
public wifi_scan_id!: string;
public lp_ttn_end_device_uplinks_id!: string;
public mac!: string;
public rssi!: number;
public created_at_utc!: Date;
public updated_at_utc!: Date;
}
WifiScan.init(
{
wifi_scan_id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
allowNull: false,
},
lp_ttn_end_device_uplinks_id: {
type: DataTypes.UUID,
allowNull: false,
},
mac: {
type: DataTypes.STRING,
allowNull: false,
},
rssi: {
type: DataTypes.NUMBER,
allowNull: true,
},
created_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
updated_at_utc: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
},
{
sequelize,
modelName: "WifiScan",
tableName: "wifi_scan",
timestamps: false,
}
);

View File

@ -0,0 +1,34 @@
import { injectable } from "tsyringe";
import { Location } from "../models/location";
@injectable()
export class LocationRepository {
public async findAll() {
return await Location.findAll();
}
public async findById(id: string) {
return await Location.findByPk(id);
}
public async create(data: Partial<Location>) {
return await Location.create(data);
}
public async update(id: string, data: Partial<Location>) {
const location = await this.findById(id);
if (location) {
return await location.update(data);
}
return null;
}
public async delete(id: string) {
const location = await this.findById(id);
if (location) {
await location.destroy();
return true;
}
return false;
}
}

View File

@ -0,0 +1,34 @@
import { injectable } from "tsyringe";
import { LpTtnEndDeviceUplinks } from "../models/lpTtnEndDeviceUplinks";
@injectable()
export class LpTtnEndDeviceUplinksRepository {
public async findAll() {
return await LpTtnEndDeviceUplinks.findAll();
}
public async findById(id: string) {
return await LpTtnEndDeviceUplinks.findByPk(id);
}
public async create(data: Partial<LpTtnEndDeviceUplinks>) {
return await LpTtnEndDeviceUplinks.create(data);
}
public async update(id: string, data: Partial<LpTtnEndDeviceUplinks>) {
const uplink = await this.findById(id);
if (uplink) {
return await uplink.update(data);
}
return null;
}
public async delete(id: string) {
const uplink = await this.findById(id);
if (uplink) {
await uplink.destroy();
return true;
}
return false;
}
}

View File

@ -0,0 +1,34 @@
import { injectable } from "tsyringe";
import { TtnGatewayReception } from "../models/ttnGatewayReception";
@injectable()
export class TtnGatewayReceptionRepository {
public async findAll() {
return await TtnGatewayReception.findAll();
}
public async findById(id: string) {
return await TtnGatewayReception.findByPk(id);
}
public async create(data: Partial<TtnGatewayReception>) {
return await TtnGatewayReception.create(data);
}
public async update(id: string, data: Partial<TtnGatewayReception>) {
const gatewayReception = await this.findById(id);
if (gatewayReception) {
return await gatewayReception.update(data);
}
return null;
}
public async delete(id: string) {
const gatewayReception = await this.findById(id);
if (gatewayReception) {
await gatewayReception.destroy();
return true;
}
return false;
}
}

View File

@ -0,0 +1,34 @@
import { injectable } from "tsyringe";
import { WifiScan } from "../models/wifiScan";
@injectable()
export class WifiScanRepository {
public async findAll() {
return await WifiScan.findAll();
}
public async findById(id: string) {
return await WifiScan.findByPk(id);
}
public async create(data: Partial<WifiScan>) {
return await WifiScan.create(data);
}
public async update(id: string, data: Partial<WifiScan>) {
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;
}
}

View File

@ -0,0 +1,31 @@
import { inject, injectable } from "tsyringe";
import { Location } from "../models/location";
import { LocationRepository } from "../repositories/locationRepository";
@injectable()
export class LocationService {
constructor(
@inject(LocationRepository)
private repository: LocationRepository
) {}
public async getAllLocations() {
return this.repository.findAll();
}
public async getLocationById(id: string) {
return this.repository.findById(id);
}
public async createLocation(data: Partial<Location>) {
return this.repository.create(data);
}
public async updateLocation(id: string, data: Partial<Location>) {
return this.repository.update(id, data);
}
public async deleteLocation(id: string) {
return this.repository.delete(id);
}
}

View File

@ -0,0 +1,31 @@
import { inject, injectable } from "tsyringe";
import { LpTtnEndDeviceUplinks } from "../models/lpTtnEndDeviceUplinks";
import { LpTtnEndDeviceUplinksRepository } from "../repositories/lpTtnEndDeviceUplinksRepository";
@injectable()
export class LpTtnEndDeviceUplinksService {
constructor(
@inject(LpTtnEndDeviceUplinksRepository)
private repository: LpTtnEndDeviceUplinksRepository
) {}
public async getAllUplinks() {
return this.repository.findAll();
}
public async getUplinkById(id: string) {
return this.repository.findById(id);
}
public async createUplink(data: Partial<LpTtnEndDeviceUplinks>) {
return this.repository.create(data);
}
public async updateUplink(id: string, data: Partial<LpTtnEndDeviceUplinks>) {
return this.repository.update(id, data);
}
public async deleteUplink(id: string) {
return this.repository.delete(id);
}
}

View File

@ -0,0 +1,34 @@
import { inject, injectable } from "tsyringe";
import { TtnGatewayReception } from "../models/ttnGatewayReception";
import { TtnGatewayReceptionRepository } from "../repositories/ttnGatewayReceptionRepository";
@injectable()
export class TtnGatewayReceptionService {
constructor(
@inject(TtnGatewayReceptionRepository)
private repository: TtnGatewayReceptionRepository
) {}
public async getAllGatewayReceptions() {
return this.repository.findAll();
}
public async getGatewayReceptionById(id: string) {
return this.repository.findById(id);
}
public async createGatewayReception(data: Partial<TtnGatewayReception>) {
return this.repository.create(data);
}
public async updateGatewayReception(
id: string,
data: Partial<TtnGatewayReception>
) {
return this.repository.update(id, data);
}
public async deleteGatewayReception(id: string) {
return this.repository.delete(id);
}
}

View File

@ -0,0 +1,30 @@
import { inject, injectable } from "tsyringe";
import { WifiScan } from "../models/wifiScan";
import { WifiScanRepository } from "../repositories/wifiScanRepository";
@injectable()
export class WifiScanService {
constructor(
@inject(WifiScanRepository) private repository: WifiScanRepository
) {}
public async getAllWifiScans() {
return this.repository.findAll();
}
public async getWifiScanById(id: string) {
return this.repository.findById(id);
}
public async createWifiScan(data: Partial<WifiScan>) {
return this.repository.create(data);
}
public async updateWifiScan(id: string, data: Partial<WifiScan>) {
return this.repository.update(id, data);
}
public async deleteWifiScan(id: string) {
return this.repository.delete(id);
}
}