-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 refactor FacilityService.ts and UserService.ts
- Loading branch information
Showing
7 changed files
with
233 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import DBService from '../services/DBService'; | ||
import { Level } from 'level'; | ||
|
||
export class MainRepository { | ||
private dbService: DBService; | ||
private db: Level<string, string | string[]>; | ||
|
||
constructor() { | ||
this.dbService = DBService.getInstance(); | ||
this.db = DBService.getInstance().getDB(); | ||
} | ||
|
||
public async getId(): Promise<number> { | ||
try { | ||
return (Number(await this.db.get('user_db_increment'))) + 1; | ||
} catch (e) { | ||
if (e.status === 404) { | ||
return 1; | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
public async setUserDBIncrement(id: string) { | ||
await this.db.put('user_db_increment', id); | ||
} | ||
} | ||
|
||
export default new MainRepository(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { AppRole, User } from '../types'; | ||
import DBService, { DBLevel, LevelDefaultTyping } from '../services/DBService'; | ||
import { Level } from 'level'; | ||
import { AbstractSublevel } from 'abstract-level'; | ||
|
||
export class UserRepository { | ||
private dbService: DBService; | ||
private db: Level<string, string | string[]>; | ||
private userDB: AbstractSublevel<DBLevel, LevelDefaultTyping, string, User>; | ||
private loginDB: AbstractSublevel<DBLevel, LevelDefaultTyping, string, string>; | ||
|
||
constructor() { | ||
this.dbService = DBService.getInstance(); | ||
this.db = DBService.getInstance().getDB(); | ||
this.userDB = DBService.getInstance().getUserDB(); | ||
this.loginDB = this.dbService.getLoginDB(); | ||
} | ||
|
||
|
||
public async getAllUsers(): Promise<User[]> { | ||
return await this.userDB.values().all(); | ||
} | ||
|
||
public async getUserIdByLogin(login: string): Promise<number | null> { | ||
try { | ||
const userId = await this.loginDB.get(login); | ||
return Number(userId); | ||
} catch (e) { | ||
if (e.status === 404) { | ||
return null; | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
public async getUserById(id: number): Promise<User> { | ||
return await this.userDB.get(String(id)); | ||
} | ||
|
||
public async createUser( | ||
id: number, | ||
login: string, | ||
password: string, | ||
roles: AppRole[] | ||
): Promise<void> { | ||
await this.userDB.put(String(id), { | ||
id, | ||
login, | ||
password, | ||
roles | ||
}); | ||
|
||
await this.loginDB.put(login, String(id)); | ||
} | ||
|
||
public async deleteUser(userId: string, login: string): Promise<void> { | ||
await this.db.del(String(userId)); | ||
await this.loginDB.del(login); | ||
} | ||
|
||
public async updateUser(userId: string, user: User): Promise<void> { | ||
await this.userDB.put(String(userId), user); | ||
} | ||
} | ||
|
||
export default new UserRepository(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.