diff --git a/ExpressAPI/src/managers/ExerciceManager.ts b/ExpressAPI/src/managers/ExerciceManager.ts deleted file mode 100644 index 4938f726894a17deca54f837fa080b51096e653d..0000000000000000000000000000000000000000 --- a/ExpressAPI/src/managers/ExerciceManager.ts +++ /dev/null @@ -1,28 +0,0 @@ -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; diff --git a/ExpressAPI/src/models/Enonce.ts b/ExpressAPI/src/models/Enonce.ts deleted file mode 100644 index 07272be07630de4762071f60ebacfbc63f770f0c..0000000000000000000000000000000000000000 --- a/ExpressAPI/src/models/Enonce.ts +++ /dev/null @@ -1,97 +0,0 @@ -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; diff --git a/ExpressAPI/src/models/EnonceStaff.ts b/ExpressAPI/src/models/EnonceStaff.ts deleted file mode 100644 index 7452394558dcaf5d34005d84b1383458183e4e1f..0000000000000000000000000000000000000000 --- a/ExpressAPI/src/models/EnonceStaff.ts +++ /dev/null @@ -1,43 +0,0 @@ -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; diff --git a/ExpressAPI/src/models/Exercice.ts b/ExpressAPI/src/models/Exercice.ts deleted file mode 100644 index d81b868538cbd6fe30c5164618f330d51218966a..0000000000000000000000000000000000000000 --- a/ExpressAPI/src/models/Exercice.ts +++ /dev/null @@ -1,98 +0,0 @@ -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; diff --git a/ExpressAPI/src/models/ExerciceMember.ts b/ExpressAPI/src/models/ExerciceMember.ts deleted file mode 100644 index cf2a2c8ee71d85c060b4e420e3b436917403e6b4..0000000000000000000000000000000000000000 --- a/ExpressAPI/src/models/ExerciceMember.ts +++ /dev/null @@ -1,43 +0,0 @@ -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; diff --git a/ExpressAPI/src/models/Model.ts b/ExpressAPI/src/models/Model.ts deleted file mode 100644 index 96a3a96e07e012420e411cb0c44674b4d79e1f39..0000000000000000000000000000000000000000 --- a/ExpressAPI/src/models/Model.ts +++ /dev/null @@ -1,22 +0,0 @@ -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; diff --git a/ExpressAPI/src/models/User.ts b/ExpressAPI/src/models/User.ts deleted file mode 100644 index 9955b3e7469597118c5fc8e0b2793e12082cad03..0000000000000000000000000000000000000000 --- a/ExpressAPI/src/models/User.ts +++ /dev/null @@ -1,112 +0,0 @@ -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; diff --git a/ExpressAPI/src/models/ApiRequest.ts b/ExpressAPI/src/types/ApiRequest.ts similarity index 50% rename from ExpressAPI/src/models/ApiRequest.ts rename to ExpressAPI/src/types/ApiRequest.ts index a0a3238578ca63775ef9f40e6ce6591f5077ee1f..2c80102e861f4e16695929b43e44d309435feb7d 100644 --- a/ExpressAPI/src/models/ApiRequest.ts +++ b/ExpressAPI/src/types/ApiRequest.ts @@ -1,6 +1,6 @@ -import express from 'express'; -import Session from '../controllers/Session'; -import Enonce from './Enonce'; +import express from 'express'; +import Session from '../controllers/Session'; +import { Enonce } from '../helpers/DatabaseHelper'; type ApiRequest = express.Request & {