Skip to content

Instantly share code, notes, and snippets.

@GautamPanickar
Created September 5, 2022 06:49
Show Gist options
  • Save GautamPanickar/0ab01c827422d4585e51948fedfefe18 to your computer and use it in GitHub Desktop.
Save GautamPanickar/0ab01c827422d4585e51948fedfefe18 to your computer and use it in GitHub Desktop.
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