Created
September 5, 2022 06:49
-
-
Save GautamPanickar/0ab01c827422d4585e51948fedfefe18 to your computer and use it in GitHub Desktop.
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
import SomethingWrongException from '../utils/exceptions/base/somethingwrongexception'; | |
import { UserInfoDTO } from '../dtos/user-info-dto'; | |
import CustomException from '../utils/exceptions/customexception'; | |
import DatabaseService from './db-service'; | |
class UserService { | |
private dbService: DatabaseService; | |
constructor() { | |
this.dbService = new DatabaseService(); | |
} | |
public getUserInfo(): POSInfoDTO { | |
try { | |
const result: POSInfoDTO[] = this.dbService.readAllUserInfo(); | |
if (result?.length > 0 ) { | |
return result[0]; | |
} | |
return null; | |
} catch (err) { | |
throw new SomethingWrongException(err); | |
} | |
} | |
public saveUserInfo(info: POSInfoDTO): boolean { | |
try { | |
if (info.uid > 0) { | |
const existingInfo = this.getUserInfo(); | |
if (existingInfo) { | |
info.id = existingInfo.id; | |
this.dbService.updateUserInfo(info); | |
} else { | |
this.dbService.saveUserInfo(info); | |
} | |
return true; | |
} else { | |
throw new CustomException('User UID cannot be empty', 200); | |
} | |
} catch (err) { | |
throw new SomethingWrongException(err); | |
} | |
} | |
} | |
export default UserService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment