Skip to content

Commit

Permalink
circular db references fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
kimamov committed Jan 31, 2021
1 parent 5a3f7db commit 211598a
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 41 deletions.
2 changes: 1 addition & 1 deletion client/.eslintcache

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions client/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
content="Room Access Dashboard"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
Expand All @@ -24,7 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>Zugang</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
8 changes: 7 additions & 1 deletion client/src/components/apartment/ApartmentList.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// in posts.js
import * as React from "react";
import { List, Datagrid, TextField, EditButton} from 'react-admin';
import { List, Datagrid, TextField, EditButton, ReferenceField} from 'react-admin';

export const ApartmentList = (props) => (
<List
{...props}
>
<Datagrid rowClick="show">
<TextField source="name"/>
<ReferenceField label="Building" reference="building" source="buildingId" link="show">
<TextField source="name"/>
</ReferenceField>
<ReferenceField label="Aparment Lock" reference="lock" source="apartmentLockId" link="show">
<TextField source="name"/>
</ReferenceField>
<EditButton/>
</Datagrid>
</List>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const BuildingRosenstrasse = (props) => {
const [building, setBuilding]=useState({});
/* get data for building rosenstrasse */
useEffect(() => {
fetch(`http:https://localhost:5000/api/building/1`)
fetch(`http:https://localhost:5000/api/building/3`)
.then(data=>data.json())
.then(json=>setBuilding(json))
.catch(error=>console.log(error))
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/lock/LockCreate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const LockCreate = (props) => {
{ id: 'Wohnungsschloss', name: 'Wohnungsschloss' },
{ id: 'Gebäudeschloss', name: 'Gebäudeschloss' },
]} required/>

<NumberInput label="Slot (1-6)" source="slot" min={1} max={6} required/>
<NumberInput source="number" label="Lock Number"/>

<ReferenceInput reference="building" source="buildingId" allowEmpty {...props}>
<SelectInput optionText="name" />
Expand Down
3 changes: 3 additions & 0 deletions client/src/components/lock/LockEdit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { Edit, SimpleForm, TextInput, NumberInput, ReferenceInput, SelectInput,
const LockEdit = (props) => (
<Edit {...props}>
<SimpleForm>
<TextInput disabled source="id" />
<TextInput source="name" required/>
<SelectInput source="type" choices={[
{ id: 'Tresorschloss', name: 'Tresorschloss' },
{ id: 'Wohnungsschloss', name: 'Wohnungsschloss' },
{ id: 'Gebäudeschloss', name: 'Gebäudeschloss' },
]} required/>
<NumberInput source="number" />

<NumberInput label="Slot (1-6)" source="slot" min={1} max={6} required/>

<ReferenceInput reference="building" source="buildingId" allowEmpty {...props}>
Expand Down
9 changes: 8 additions & 1 deletion src/controllers/apartmentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ export async function editBuilding(req: Request, res: Response) {




export async function deleteApartment(req: Request, res: Response){
try {
const result=await getRepository(Apartment).delete(req.params.id)
return res.send(result);
} catch (error) {
res.status(500).send(error)
}
}



9 changes: 8 additions & 1 deletion src/controllers/buildingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ export async function editBuilding(req: Request, res: Response) {
}
}


export async function deleteBuilding(req: Request, res: Response){
try {
const result=await getRepository(Building).delete(req.params.id)
return res.send(result);
} catch (error) {
res.status(500).send(error)
}
}



Expand Down
4 changes: 2 additions & 2 deletions src/controllers/keyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ export async function deleteKey(req: Request, res: Response){
}
}

export async function getKeysByDoor(req: Request, res: Response) {
/* export async function getKeysByDoor(req: Request, res: Response) {
}
} */
13 changes: 11 additions & 2 deletions src/controllers/lockController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export async function createLock(req: Request, res: Response) {
lock.apartmentLock=await getRepository(Apartment).findOne(lock.apartmentId)
lock.apartmentId=null;
}else if(lock.type==="Gebäudeschloss" && lock.buildingId){
console.log("lock")
lock.buildingLock=await getRepository(Building).findOne(lock.buildingId)
}
const result=await repo.save(lock);
Expand All @@ -58,13 +57,14 @@ export async function createLock(req: Request, res: Response) {
export async function editLock(req: Request, res: Response) {
// function creates a connection between the reader and key inside the database and sends it over to the reader
try {
const updateObject: Lock={...this.body}
const {building, apartment,reader,apartmentLock,buildingLock,...updateObject}=req.body

if(updateObject.type==="Wohnungsschloss" && updateObject.apartmentId){
updateObject.apartmentLock=await getRepository(Apartment).findOne(updateObject.apartmentId)
updateObject.apartmentId=null;
}else if(updateObject.type==="Gebäudeschloss" && updateObject.buildingId){
updateObject.buildingLock=await getRepository(Building).findOne(updateObject.buildingId)
//updateObject.buildingId=null;
}
const result=await getRepository(Lock).save(updateObject);
res.send(result)
Expand All @@ -78,6 +78,15 @@ export async function editLock(req: Request, res: Response) {
}


export async function deleteLock(req: Request, res: Response){
try {
const result=await getRepository(Lock).delete(req.params.id)
return res.send(result);
} catch (error) {
res.status(500).send(error)
}
}




Expand Down
8 changes: 4 additions & 4 deletions src/entity/Apartment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ export class Apartment {
@Column({ type: "int", nullable: true })
apartmentLockId: number;

@ManyToOne(()=> Lock, lock=>lock.apartmentLock, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@ManyToOne(()=> Lock, lock=>lock.apartmentLock, { onDelete: "SET NULL"})
@JoinColumn({name: "apartmentLockId"})
public apartmentLock: Lock;


@Column({ type: "int", nullable: true })
buildingId: number;

@ManyToOne(()=> Building, building=> building.apartments, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@ManyToOne(()=> Building, building=> building.apartments)
@JoinColumn({name: "buildingId"})
public building: Building;

@OneToMany(()=>Lock, lock=>lock.apartment, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@OneToMany(()=>Lock, lock=>lock.apartment/* , { cascade: true, onDelete: 'SET NULL', onUpdate: "CASCADE" } */)
locks: Lock[];

@OneToMany(()=>Reader, reader=>reader.apartment, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@OneToMany(()=>Reader, reader=>reader.apartment, { onDelete: 'SET NULL' })
readers: Reader[];

}
10 changes: 5 additions & 5 deletions src/entity/Building.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ export class Building {
@Column("varchar", { unique: true, length: 100, nullable: true })
thumbnailSrc: string;

@Column({ type: "int", nullable: true })
@Column({ type: "int", nullable: true})
buildingLockId: number;

@ManyToOne(()=> Lock)
@JoinColumn({name: "buildingLockId"})
@ManyToOne(()=> Lock, {onDelete: 'SET NULL'})
@JoinColumn({name: "buildingLockId",})
public buildingLock: Lock;

@OneToMany(()=>Lock, lock=>lock.building, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@OneToMany(()=>Lock, lock=>lock.building/* , { onDelete: 'CASCADE', onUpdate: 'CASCADE' } */)
locks: Lock[];

@OneToMany(()=>Apartment, apartment=>apartment.building, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@OneToMany(()=>Apartment, apartment=>apartment.building/* , { onDelete: 'CASCADE', onUpdate: 'CASCADE' } */)
apartments: Apartment[];


Expand Down
4 changes: 2 additions & 2 deletions src/entity/Key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export class Key {
@OneToMany(()=>ReaderKey, readerKey=>readerKey.key, { /* onDelete: 'CASCADE', onUpdate: 'CASCADE', */ cascade: true })
readerKeys: ReaderKey[];

@ManyToOne(()=>User, user=>user.keys, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@ManyToOne(()=>User, user=>user.keys)
user: User;

@ManyToOne(()=>Lock, lock=>lock.keys, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@ManyToOne(()=>Lock, lock=>lock.keys)
lock: Lock;

}
12 changes: 6 additions & 6 deletions src/entity/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ export class Lock {
@Column({ type: "int", nullable: true })
buildingId: number;

@ManyToOne(()=> Building, building=> building.locks, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@ManyToOne(()=> Building, building=> building.locks)
@JoinColumn({name: "buildingId"})
public building: Building;

@Column({ type: "int", nullable: true })
apartmentId: number;

@ManyToOne(()=> Apartment, apartment=> apartment.locks, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@ManyToOne(()=> Apartment, apartment=> apartment.locks, {onDelete: "SET NULL"})
@JoinColumn({name: "apartmentId"})
public apartment: Apartment;



@OneToOne(()=> Apartment, apartment=> apartment.apartmentLock, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@OneToOne(()=> Apartment, apartment=> apartment.apartmentLock, {cascade: true, onDelete: "SET NULL"})
public apartmentLock: Apartment;

@OneToOne(()=> Building, building=> building.buildingLock, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@OneToOne(()=> Building, building=> building.buildingLock, {cascade: true})
public buildingLock: Building;


@Column({ type: "int", nullable: true })
readerId: number;

@ManyToOne(()=> Reader, reader=> reader, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@ManyToOne(()=> Reader, reader=> reader)
@JoinColumn({name: "readerId"})
public reader: Reader;

Expand All @@ -67,6 +67,6 @@ export class Lock {
@JoinTable()
public previousLocks: Lock[]; */

@OneToMany(type=> Key, key => key.lock, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@OneToMany(type=> Key, key => key.lock/* , { onDelete: 'CASCADE', onUpdate: 'CASCADE' } */)
public keys: Key[];
}
2 changes: 1 addition & 1 deletion src/entity/NewKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class NewKey {
@Column("varchar", { nullable: false, length: 20 })
door: string;

@ManyToOne(()=> Reader, reader=> reader.newKeys, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@ManyToOne(()=> Reader, reader=> reader.newKeys)
reader: Reader;


Expand Down
4 changes: 2 additions & 2 deletions src/entity/Reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ export class Reader {
newKeys: NewKey[];


@OneToMany(()=>ReaderKey, readerKey=>readerKey.reader, { /* onDelete: 'CASCADE', onUpdate: 'CASCADE', */cascade: true })
@OneToMany(()=>ReaderKey, readerKey=>readerKey.reader, { onDelete: 'CASCADE', onUpdate: 'CASCADE', cascade: true })
readerKeys: ReaderKey[];

@Column({ type: "int", nullable: true })
apartmentId: number;

@ManyToOne(()=> Apartment, apartment=> apartment.readers, { onDelete: 'CASCADE', onUpdate: 'CASCADE' })
@ManyToOne(()=> Apartment, apartment=> apartment.readers ,{onDelete: 'SET NULL'}/* , { cascade: true, onDelete: 'CASCADE', onUpdate: 'CASCADE' } */)
@JoinColumn({name: "apartmentId"})
public apartment: Apartment;

Expand Down
6 changes: 3 additions & 3 deletions src/entity/ReaderKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, Column, PrimaryColumn, OneToMany, JoinColumn, ManyToOne } from "typeorm";
import { Entity, Column, PrimaryColumn, JoinColumn, ManyToOne } from "typeorm";
import { Key } from "./Key";
import {Reader} from "./Reader"

Expand All @@ -9,13 +9,13 @@ export class ReaderKey {

@PrimaryColumn()
readerId: number;
@ManyToOne(() => Reader, reader => reader.readerKeys, {primary: true, persistence: false})
@ManyToOne(() => Reader, reader => reader.readerKeys, {primary: true, persistence: false/* , onDelete: 'CASCADE', onUpdate: 'CASCADE' */})
@JoinColumn({name: "readerId", referencedColumnName: "id"})
reader: Reader;

@PrimaryColumn()
keyId: number;
@ManyToOne(() => Key, key => key.readerKeys, {primary: true})
@ManyToOne(() => Key, key => key.readerKeys, {primary: true, onDelete: "CASCADE"})
@JoinColumn({name: "keyId", referencedColumnName: "id"})
key: Key;

Expand Down
13 changes: 8 additions & 5 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { getReaders, addReaderKeys, getReaderWithKeys, editReaderKeys } from '..
import {signUp, editUser, getUsers, getUserById } from '../controllers/userController';
import { checkAuth } from '../middlewares/middlewares';
import { client } from '../mqtt/connection';
import { createLock, editLock, getLock, getLocks } from '../controllers/lockController';
import { getBuildings, getBuilding, createBuilding } from '../controllers/buildingController';
import { createApartment, getApartment, getApartments } from '../controllers/apartmentController';
import { createLock, deleteLock, editLock, getLock, getLocks } from '../controllers/lockController';
import { getBuildings, getBuilding, createBuilding, deleteBuilding } from '../controllers/buildingController';
import { createApartment, deleteApartment, getApartment, getApartments } from '../controllers/apartmentController';
/* import { client } from '../mqtt/connection';
*/

Expand Down Expand Up @@ -63,13 +63,17 @@ router.get("/building/:id", getBuilding)

router.post("/building"/* , checkAuth */, createBuilding)

router.delete("/building/:id", deleteBuilding)


router.get("/apartment"/* , checkAuth */, getApartments)

router.get("/apartment/:id", getApartment)

router.post("/apartment"/* , checkAuth */, createApartment)

router.delete("/apartment/:id", deleteApartment)


/* router.get("/openlock/:id", (req, res)=>{
console.log(req.params);
Expand All @@ -86,9 +90,8 @@ router.get("/lock/:id", getLock)

router.put("/lock/:id", editLock)

/* router.put("/key/:id", editKey)
router.delete("/lock/:id", deleteLock)

router.delete("/key/:id", deleteKey) */


// TODO: split the routes into different files
Expand Down
5 changes: 3 additions & 2 deletions src/util/getList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Repository, FindManyOptions, In } from "typeorm";

export async function getList<T>(repo: Repository<T>, req: Request, res: Response, extraOptions?: FindManyOptions){
try {
//console.log("getList was called")
const options:FindManyOptions={}
if(req.query.sort){
const [sortField="id", sortDirection="DESC"]=JSON.parse(req.query.sort as string);
Expand Down Expand Up @@ -36,8 +37,8 @@ export async function getList<T>(repo: Repository<T>, req: Request, res: Respons

}
//console.log("filter applied to getList");
} catch (_error) {
//console.log(error)
} catch (error) {
console.log(error)
}

}
Expand Down

0 comments on commit 211598a

Please sign in to comment.