Skip to content
Snippets Groups Projects
Commit f10be592 authored by michael.minelli's avatar michael.minelli
Browse files

Remove old model classes

parent 8f87568e
Branches
Tags
No related merge requests found
import Exercice from '../models/Exercice';
class ExerciceManager {
private static _instance: ExerciceManager;
private constructor() { }
public static get instance(): ExerciceManager {
if ( !ExerciceManager._instance ) {
ExerciceManager._instance = new ExerciceManager();
}
return ExerciceManager._instance;
}
createObjectFromRawSql(raw: any): Exercice {
const exercice = Exercice.createFromSql(raw);
exercice.exerciceGitlabCreationInfo = raw.exerciceGitlabCreationInfo;
exercice.exerciceGitlabLastInfo = raw.exerciceGitlabLastInfo;
return exercice;
}
}
export default ExerciceManager.instance;
import Model from './Model';
import db from '../helpers/DatabaseHelper';
import GitlabRepository from '../shared/types/Gitlab/GitlabRepository';
import LazyVal from '../shared/helpers/LazyVal';
import UserManager from '../managers/UserManager';
import User from './User';
class Enonce extends Model {
static tableName: string = 'Enonce';
enonceName: string = '';
enonceGitlabId: number = null;
enonceGitlabLink: string = '';
private _enonceGitlabCreationInfo: string = '{}';
private _enonceGitlabLastInfo: string = '{}';
enonceGitlabLastInfoTs: number = null;
get enonceGitlabCreationInfo(): GitlabRepository {
return JSON.parse(this._enonceGitlabCreationInfo);
}
set enonceGitlabCreationInfo(value: any) {
if ( typeof value === 'string' ) {
this._enonceGitlabCreationInfo = value;
return;
}
this._enonceGitlabCreationInfo = JSON.stringify(value);
}
get enonceGitlabLastInfo(): GitlabRepository {
return JSON.parse(this._enonceGitlabLastInfo);
}
set enonceGitlabLastInfo(value: any) {
if ( typeof value === 'string' ) {
this._enonceGitlabLastInfo = value;
return;
}
this._enonceGitlabLastInfo = JSON.stringify(value);
}
staff = new LazyVal<Array<User>>(() => {
return UserManager.getStaffOfEnonce(this.enonceName);
});
public async toJsonObject(): Promise<Object> {
const result = {
'name' : this.enonceName,
'gitlabId' : this.enonceGitlabId,
'gitlabLink' : this.enonceGitlabLink,
'gitlabCreationInfo': this.enonceGitlabCreationInfo,
'gitlabLastInfo' : this.enonceGitlabLastInfo,
'gitlabLastInfoTs' : this.enonceGitlabLastInfoTs
};
return result;
};
public importFromJsonObject(jsonObject: any) {
this.enonceName = jsonObject.name;
this.enonceGitlabId = jsonObject.gitlabId;
this.enonceGitlabLink = jsonObject.gitlabLink;
this.enonceGitlabCreationInfo = jsonObject.gitlabCreationInfo;
this.enonceGitlabLastInfo = jsonObject.gitlabLastInfo;
this.enonceGitlabLastInfoTs = jsonObject.gitlabLastInfoTs;
}
public toDb(): any {
return {
enonceName : this.enonceName,
enonceGitlabId : this.enonceGitlabId,
enonceGitlabLink : this.enonceGitlabLink,
enonceGitlabCreationInfo: this._enonceGitlabCreationInfo,
enonceGitlabLastInfo : this._enonceGitlabLastInfo,
enonceGitlabLastInfoTs : this.enonceGitlabLastInfoTs
};
}
async create(): Promise<Enonce> {
await db(Enonce.tableName).insert(this.toDb());
return this;
}
update(): Promise<void> {
return db(Enonce.tableName).where('enonceName', this.enonceName).update(this.toDb());
}
del(): Promise<void> {
return db(Enonce.tableName).where('enonceName', this.enonceName).del();
}
}
export default Enonce;
import Model from './Model';
import db from '../helpers/DatabaseHelper';
class EnonceStaff extends Model {
static tableName: string = 'EnonceStaff';
enonceName: string = null;
userId: number = null;
public async toJsonObject(): Promise<Object> {
const result = {
'enonceName': this.enonceName,
'userId' : this.userId
};
return result;
};
public importFromJsonObject(jsonObject: any) {
this.enonceName = jsonObject.enonceName;
this.userId = jsonObject.userId;
}
public toDb(): any {
return {
enonceName: this.enonceName,
userId : this.userId
};
}
async create(): Promise<EnonceStaff> {
await db(EnonceStaff.tableName).insert(this.toDb());
return this;
}
del(): Promise<void> {
return db(EnonceStaff.tableName).where('enonceName', this.enonceName).andWhere('userId', this.userId).del();
}
}
export default EnonceStaff;
import Model from './Model';
import db from '../helpers/DatabaseHelper';
import GitlabRepository from '../shared/types/Gitlab/GitlabRepository';
class Exercice extends Model {
static tableName: string = 'Exercice';
exerciceId: string = '';
exerciceEnonceName: string = '';
exerciceName: string = '';
exerciceGitlabId: number = null;
exerciceGitlabLink: string = '';
private _exerciceGitlabCreationInfo: string = '{}';
private _exerciceGitlabLastInfo: string = '{}';
exerciceGitlabLastInfoTs: number = null;
get exerciceGitlabCreationInfo(): GitlabRepository {
return JSON.parse(this._exerciceGitlabCreationInfo);
}
set exerciceGitlabCreationInfo(value: any) {
if ( typeof value === 'string' ) {
this._exerciceGitlabCreationInfo = value;
return;
}
this._exerciceGitlabCreationInfo = JSON.stringify(value);
}
get exerciceGitlabLastInfo(): GitlabRepository {
return JSON.parse(this._exerciceGitlabLastInfo);
}
set exerciceGitlabLastInfo(value: any) {
if ( typeof value === 'string' ) {
this._exerciceGitlabLastInfo = value;
return;
}
this._exerciceGitlabLastInfo = JSON.stringify(value);
}
public async toJsonObject(): Promise<Object> {
const result = {
'id' : this.exerciceId,
'enonceName' : this.exerciceEnonceName,
'name' : this.exerciceName,
'gitlabId' : this.exerciceGitlabId,
'gitlabLink' : this.exerciceGitlabLink,
'gitlabCreationInfo': this.exerciceGitlabCreationInfo,
'gitlabLastInfo' : this.exerciceGitlabLastInfo,
'gitlabLastInfoTs' : this.exerciceGitlabLastInfoTs
};
return result;
};
public importFromJsonObject(jsonObject: any) {
this.exerciceId = jsonObject.id;
this.exerciceEnonceName = jsonObject.enonceName;
this.exerciceName = jsonObject.name;
this.exerciceGitlabId = jsonObject.gitlabId;
this.exerciceGitlabLink = jsonObject.gitlabLink;
this.exerciceGitlabCreationInfo = jsonObject.gitlabCreationInfo;
this.exerciceGitlabLastInfo = jsonObject.gitlabLastInfo;
this.exerciceGitlabLastInfoTs = jsonObject.gitlabLastInfoTs;
}
public toDb(): any {
return {
exerciceId : this.exerciceId,
exerciceEnonceName : this.exerciceEnonceName,
exerciceName : this.exerciceName,
exerciceGitlabId : this.exerciceGitlabId,
exerciceGitlabLink : this.exerciceGitlabLink,
exerciceGitlabCreationInfo: this._exerciceGitlabCreationInfo,
exerciceGitlabLastInfo : this._exerciceGitlabLastInfo,
exerciceGitlabLastInfoTs : this.exerciceGitlabLastInfoTs
};
}
async create(): Promise<Exercice> {
await db(Exercice.tableName).insert(this.toDb());
return this;
}
update(): Promise<void> {
return db(Exercice.tableName).where('exerciceId', this.exerciceId).update(this.toDb());
}
del(): Promise<void> {
return db(Exercice.tableName).where('exerciceId', this.exerciceId).del();
}
}
export default Exercice;
import Model from './Model';
import db from '../helpers/DatabaseHelper';
class ExerciceMember extends Model {
static tableName: string = 'ExerciceMember';
exerciceId: string = '';
userId: number = null;
public async toJsonObject(): Promise<Object> {
const result = {
'exerciceId': this.exerciceId,
'userId' : this.userId
};
return result;
};
public importFromJsonObject(jsonObject: any) {
this.exerciceId = jsonObject.exerciceId;
this.userId = jsonObject.userId;
}
public toDb(): any {
return {
exerciceId: this.exerciceId,
userId : this.userId
};
}
async create(): Promise<ExerciceMember> {
await db(ExerciceMember.tableName).insert(this.toDb());
return this;
}
del(): Promise<void> {
return db(ExerciceMember.tableName).where('exerciceId', this.exerciceId).andWhere('userId', this.userId).del();
}
}
export default ExerciceMember;
type Constructor<T> = new (...args: any[]) => T;
abstract class Model extends Object {
static tableName: string = null;
static createFromSql<T extends Object>(this: Constructor<T>, obj: any): T {
const result = new this();
Object.getOwnPropertyNames(obj).forEach(property => {
if ( result.hasOwnProperty(property) ) {
(result as any)[property] = obj[property];
}
});
return result;
}
public abstract toJsonObject(): Promise<Object>
}
export default Model;
import Session from '../controllers/Session';
import Config from '../config/Config';
import Toolbox from '../shared/helpers/Toolbox';
import * as bcrypt from 'bcryptjs';
import Model from './Model';
import db from '../helpers/DatabaseHelper';
import LazyVal from '../shared/helpers/LazyVal';
import GitlabUser from '../shared/types/Gitlab/GitlabUser';
import GitlabManager from '../managers/GitlabManager';
class User extends Model {
static tableName: string = 'User';
userId: number = null;
userFirstName: string = '';
userLastName: string = '';
userMail: string = '';
userGitlabId: number = -1;
userRole: string = 'student';
userDeleted: boolean = false;
userPassword: string = null;
unencryptedPassword: string = null; // This value is not set from the db. It's a value that is not null only if we have called createPassword function
currentSession: Session = null;
gitlabProfile = new LazyVal<GitlabUser>(() => {
return GitlabManager.getUserById(this.userGitlabId);
});
public async toJsonObject(): Promise<Object> {
const result = {
'id' : this.userId,
'firstName' : this.userFirstName,
'lastName' : this.userLastName,
'mail' : this.userMail,
'gitlabId' : this.userGitlabId,
'role' : this.userRole,
'isTeachingStaff': this.isTeachingStaff,
'deleted' : this.userDeleted
};
return result;
};
get fullName(): string {
return this.userLastName.toUpperCase() + ' ' + this.userFirstName;
}
get isTeachingStaff(): boolean {
return Config.permissions.teachingStaff.includes(this.userRole);
}
public importFromJsonObject(jsonObject: any) {
this.userId = jsonObject.id;
this.userFirstName = jsonObject.firstName;
this.userLastName = jsonObject.lastName;
this.userMail = jsonObject.mail;
this.userGitlabId = jsonObject.gitlabId;
this.userRole = jsonObject.role;
this.userDeleted = jsonObject.deleted;
}
public generateHashedPassword() {
this.userPassword = bcrypt.hashSync(this.unencryptedPassword, Config.userPasswordSaltRounds);
}
public replacePassword(password: string) {
this.unencryptedPassword = password;
this.generateHashedPassword();
}
public createPassword() {
this.unencryptedPassword = Toolbox.randomString(Config.userPasswordLength);
this.generateHashedPassword();
}
public toDb(): any {
return {
userFirstName: Toolbox.capitalizeName(this.userFirstName),
userLastName : Toolbox.capitalizeName(this.userLastName),
userRole : this.userRole,
userMail : this.userMail,
userGitlabId : this.userGitlabId,
userPassword : this.userPassword
};
}
async create(): Promise<User> {
const id = await db(User.tableName).insert(this.toDb());
this.userId = id[0];
return this;
}
update(): Promise<void> {
return db(User.tableName).where('userId', this.userId).update(this.toDb());
}
updatePassword(): Promise<void> {
return db(User.tableName).where('userId', this.userId).update({ 'userPassword': this.userPassword });
}
del(): Promise<void> {
return db(User.tableName).where('userId', this.userId).update({ 'userDeleted': true });
}
}
export default User;
import express from 'express'; import express from 'express';
import Session from '../controllers/Session'; import Session from '../controllers/Session';
import Enonce from './Enonce'; import { Enonce } from '../helpers/DatabaseHelper';
type ApiRequest = express.Request & { type ApiRequest = express.Request & {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment