import { Injectable } from '@angular/core' ; import { BackendService } from './backend.service' ; import { Dependency } from '../../about/licenses/dependency.model' ; import { ILecture } from '../../../../../../shared/models/ILecture' ; import { IUnit } from '../../../../../../shared/models/units/IUnit' ; import { IUser } from '../../../../../../shared/models/IUser' ; import { ICourse } from '../../../../../../shared/models/ICourse' ; import { IDownload } from '../../../../../../shared/models/IDownload' ; import { IDirectory } from '../../../../../../shared/models/mediaManager/IDirectory' ; import { IFile } from '../../../../../../shared/models/mediaManager/IFile' ; import { IDuplicationResponse } from '../../../../../../shared/models/IDuplicationResponse' ; import { IUserSearchMeta } from '../../../../../../shared/models/IUserSearchMeta' ; import { IConfig } from '../../../../../../shared/models/IConfig' ; import { IAssignment } from '../../../../../../shared/models/assignment/IAssignment' ; import { INotificationView } from '../../../../../../shared/models/INotificationView' ; import { INotificationSettingsView } from '../../../../../../shared/models/INotificationSettingsView' ; export abstract class DataService { static changeStringProp2DateProp ( item , prop ) { if ( item [ prop ] !== null ) { item [ prop ] = ( typeof item [ prop ] === 'string' ? new Date ( item [ prop ] ) : null ) ; } } constructor ( public apiPath , public backendService , public changeProps2Date ? , public dependentID ? ) { } createItem < T , RT = T > ( createItem ) { return this . backendService . post ( this . apiPath , createItem ) . toPromise ( ) ; } readItems < T > ( ) { return new Promise ( ( resolve , reject ) => { this . backendService . get ( this . apiPath ) . subscribe ( ( responseItems ) => { if ( this . changeProps2Date ) { responseItems . forEach ( item => { this . changeProps2Date . forEach ( prop => { DataService . changeStringProp2DateProp ( item , prop ) ; } ) ; } ) ; } resolve ( responseItems ) ; } , ( error ) => reject ( error ) ) ; } ) ; } updateItem < T extends any > ( updateItem ) { return this . backendService . put ( this . apiPath + updateItem . _id , JSON . stringify ( updateItem ) ) . toPromise ( ) ; } deleteItem < T extends any > ( deleteItem ) { return this . backendService . delete ( this . apiPath + deleteItem . _id ) . toPromise ( ) ; } readSingleItem < T > ( id , apiPath = this . apiPath ) { return new Promise ( ( resolve , reject ) => { this . backendService . get ( apiPath + id ) . subscribe ( ( responseItems ) => { if ( this . changeProps2Date ) { responseItems . forEach ( item => { this . changeProps2Date . forEach ( prop => { DataService . changeStringProp2DateProp ( item , prop ) ; } ) ; } ) ; } resolve ( responseItems ) ; } , error => reject ( error ) ) ; } ) ; } } @ Injectable ( ) export class ExportService extends DataService { constructor ( public backendService ) { super ( 'export/' , backendService ) ; } exportCourse ( course ) { return this . backendService . get ( this . apiPath + 'course/' + course . _id ) . toPromise ( ) ; } exportLecture ( lecture ) { return this . backendService . get ( this . apiPath + 'lecture/' + lecture . _id ) . toPromise ( ) ; } exportUnit ( unit ) { return this . backendService . get ( this . apiPath + 'unit/' + unit . _id ) . toPromise ( ) ; } } @ Injectable ( ) export class ImportService extends DataService { constructor ( public backendService ) { super ( 'import/' , backendService ) ; } importCourse ( course ) { return this . backendService . post ( this . apiPath + 'course/' , JSON . stringify ( course ) ) . toPromise ( ) ; } importLecture ( lecture , course ) { return this . backendService . post ( this . apiPath + 'course/' + course . _id , JSON . stringify ( lecture ) ) . toPromise ( ) ; } importUnit ( unit , lecture , course ) { return this . backendService . post ( this . apiPath + 'unit/' + course . _id + '/' + lecture . _id , JSON . stringify ( unit ) ) . toPromise ( ) ; } } @ Injectable ( ) export class DuplicationService extends DataService { constructor ( public backendService ) { super ( 'duplicate/' , backendService ) ; } duplicateCourse ( course , courseAdmin ) { return this . backendService . post ( this . apiPath + 'course/' + course . _id , JSON . stringify ( { courseAdmin } ) ) . toPromise ( ) ; } duplicateLecture ( lecture , courseId ) { return this . backendService . post ( this . apiPath + 'lecture/' + lecture . _id , JSON . stringify ( { courseId } ) ) . toPromise ( ) ; } duplicateUnit ( unit , lectureId ) { return this . backendService . post ( this . apiPath + 'unit/' + unit . _id , JSON . stringify ( { lectureId } ) ) . toPromise ( ) ; } } @ Injectable ( ) export class MediaService extends DataService { constructor ( public backendService ) { super ( 'media/' , backendService ) ; } createRootDir ( course , rootDirName ) { return this . backendService . post ( this . apiPath + 'directory' , JSON . stringify ( { _course : course . _id , name : rootDirName } ) ) . toPromise ( ) ; } createDirectory ( newDirName , parentDir ) { return this . backendService . post ( this . apiPath + 'directory/' + parentDir . _id , JSON . stringify ( { name : newDirName } ) ) . toPromise ( ) ; } addFile ( directory ) { return this . backendService . post ( this . apiPath + 'file/' + directory . _id , JSON . stringify ( { } ) ) . toPromise ( ) ; } getDirectory ( dirId , lazy = false ) { const path = this . apiPath + 'directory/' + dirId + ( lazy ? '/lazy' : '' ) ; return this . backendService . get ( path ) . toPromise ( ) ; } getFile ( fileId ) { return this . backendService . get ( this . apiPath + 'file/' + fileId ) . toPromise ( ) ; } updateDirectory ( dir ) { return this . backendService . put ( this . apiPath + 'directory/' + dir . _id , JSON . stringify ( dir ) ) . toPromise ( ) ; } updateFile ( file ) { return this . backendService . put ( this . apiPath + 'file/' + file . _id , JSON . stringify ( file ) ) . toPromise ( ) ; } deleteDirectory ( dir ) { return this . backendService . delete ( this . apiPath + 'directory/' + dir . _id ) . toPromise ( ) ; } deleteFile ( file ) { return this . backendService . delete ( this . apiPath + 'file/' + file . _id ) . toPromise ( ) ; } } @ Injectable ( ) export class CourseService extends DataService { constructor ( public backendService ) { super ( 'courses/' , backendService ) ; } enrollStudent ( courseId , data ) { const accessKey = data . accessKey ; return this . backendService . post ( this . apiPath + courseId + '/enroll' , JSON . stringify ( { accessKey } ) ) . toPromise ( ) ; } sendMailToSelectedUsers ( data ) { return this . backendService . post ( this . apiPath + 'mail' , JSON . stringify ( data ) ) . toPromise ( ) ; } async removePicture ( courseId ) { return this . backendService . delete ( this . apiPath + 'picture/' + courseId ) . toPromise ( ) ; } async leaveStudent ( courseId ) { return await this . backendService . post ( this . apiPath + courseId + '/leave' , { } ) . toPromise ( ) ; } readCourseToView ( id ) { return this . readSingleItem < ICourse > ( id ) ; } readCourseToEdit ( id ) { return this . readSingleItem < ICourse > ( id + '/edit' ) ; } setWhitelistUsers ( courseId , whitelistUsers ) { const originalApiPath = this . apiPath ; this . apiPath += courseId + '/whitelist' ; const promise = this . createItem ( JSON . stringify ( whitelistUsers ) ) ; this . apiPath = originalApiPath ; return promise ; } } @ Injectable ( ) export class TaskService extends DataService { constructor ( public backendService ) { super ( 'tasks/' , backendService ) ; } getTasksForCourse ( id ) { return new Promise ( ( resolve , reject ) => { this . backendService . get ( this . apiPath + 'course/' + id ) . subscribe ( ( responseItems ) => { if ( this . changeProps2Date ) { responseItems . forEach ( item => { this . changeProps2Date . forEach ( prop => { DataService . changeStringProp2DateProp ( item , prop ) ; } ) ; } ) ; } resolve ( responseItems ) ; } , error => reject ( error ) ) ; } ) ; } } @ Injectable ( ) export class AssignmentService extends DataService { constructor ( public backendService ) { super ( 'units/' , backendService ) ; } createAssignment ( unitId ) { return this . backendService . post ( this . apiPath + unitId + '/assignment' , null ) . toPromise ( ) ; } updateAssignment ( assignment , unitId ) { return new Promise ( ( resolve , reject ) => { this . backendService . put ( this . apiPath + unitId + '/assignment' , assignment ) . subscribe ( ( responseItem ) => { resolve ( responseItem ) ; } , error => reject ( error ) ) ; } ) ; } deleteAssignment ( unitId ) { return new Promise ( ( resolve , reject ) => { this . backendService . delete ( this . apiPath + unitId + '/assignment' ) . subscribe ( ( responseItem ) => { resolve ( responseItem ) ; } , error => reject ( error ) ) ; } ) ; } downloadAllAssignments ( unitId ) { return this . backendService . getDownload ( this . apiPath + unitId + '/assignments/files' ) . toPromise ( ) ; } downloadSingleAssignment ( unitId , userId ) { return this . backendService . getDownload ( this . apiPath + unitId + '/assignments/' + userId + '/files' ) . toPromise ( ) ; } } @ Injectable ( ) export class LectureService extends DataService { constructor ( public backendService ) { super ( 'lecture/' , backendService ) ; } } @ Injectable ( ) export class UnitService extends DataService { constructor ( public backendService ) { super ( 'units/' , backendService ) ; } getUnitForCourse ( courseId ) { const originalApiPath = this . apiPath ; this . apiPath += 'progressable/course/' ; const promise = this . readSingleItem ( courseId ) ; this . apiPath = originalApiPath ; return promise ; } getProgressableUnits ( courseId ) { const originalApiPath = this . apiPath ; this . apiPath += 'course/progressable/' ; const promise = this . readSingleItem ( courseId ) ; this . apiPath = originalApiPath ; return promise ; } } @ Injectable ( ) export class CodeKataUnitService extends DataService { constructor ( public backendService ) { super ( 'units/code-katas/' , backendService ) ; } } @ Injectable ( ) export class FreeTextUnitService extends DataService { constructor ( public backendService ) { super ( 'units/free-texts/' , backendService ) ; } } @ Injectable ( ) export class NotificationSettingsService extends DataService { constructor ( public backendService ) { super ( 'notificationSettings/' , backendService ) ; } getNotificationSettings ( ) { return this . backendService . get ( this . apiPath ) . toPromise ( ) ; } async setNotificationSettings ( settings ) { await this . backendService . put ( this . apiPath , settings ) . toPromise ( ) ; } } @ Injectable ( ) export class NotificationService extends DataService { constructor ( public backendService ) { super ( 'notification/' , backendService ) ; } createNotification ( user , data ) { return this . backendService . post ( this . apiPath + 'user/' + user . _id , JSON . stringify ( data ) ) . toPromise ( ) ; } getNotifications ( ) { return this . backendService . get ( this . apiPath ) . toPromise ( ) ; } } @ Injectable ( ) export class UserDataService extends DataService { constructor ( public backendService ) { super ( 'users/' , backendService ) ; } searchUsers ( role , query , limit ? ) { const originalApiPath = this . apiPath ; this . apiPath += 'members/search/' ; this . apiPath += '?role=' + role ; this . apiPath += '&query=' + query ; if ( limit ) { this . apiPath += '&limit=' + limit ; } const promise = this . readSingleItem ( '' ) as Promise < IUserSearchMeta > ; this . apiPath = originalApiPath ; return promise ; } getRoles ( ) { const originalApiPath = this . apiPath ; this . apiPath += 'roles/' ; const promise = this . readItems < string > ( ) ; this . apiPath = originalApiPath ; return promise ; } addPicture ( data ) { const originalApiPath = this . apiPath ; this . apiPath += 'picture/' ; const promise = this . updateItem ( data ) ; this . apiPath = originalApiPath ; return promise ; } exportData ( ) { const url = 'export/user' ; return this . backendService . getDownload ( url ) . toPromise ( ) ; } } @ Injectable ( ) export class WhitelistUserService extends DataService { constructor ( public backendService ) { super ( 'whitelist/' , backendService ) ; } checkWhitelistUsers ( whitelistUsers ) { const originalApiPath = this . apiPath ; this . apiPath += 'check/' + encodeURIComponent ( JSON . stringify ( whitelistUsers ) ) ; const promise = this . readItems ( ) ; this . apiPath = originalApiPath ; return promise ; } } @ Injectable ( ) export class APIInfoService extends DataService { private apiInfo : any ; constructor ( public backendService ) { super ( '' , backendService ) ; } async readAPIInfo ( ) { if ( ! this . apiInfo ) { this . apiInfo = await this . readItems ( ) ; } return this . apiInfo ; } } @ Injectable ( ) export class AboutDataService extends DataService { constructor ( public backendService ) { super ( 'about/' , backendService ) ; } getApiDependencies ( ) { return new Promise ( ( resolve , reject ) => { this . backendService . get ( this . apiPath + 'dependencies' ) . subscribe ( ( responseItems ) => { if ( responseItems . httpCode >= 500 ) { return resolve ( [ ] ) ; } const out = [ ] ; responseItems . data . forEach ( item => { out . push ( new Dependency ( item . name , item . version , item . repository , item . license , item . devDependency ) ) ; } ) ; resolve ( out ) ; } , error => reject ( error ) ) ; } ) ; } } @ Injectable ( ) export class DownloadFileService extends DataService { constructor ( public backendService ) { super ( 'download/' , backendService ) ; } postDownloadReqForCoursePDFIndividual ( idl ) { return this . backendService . post ( this . apiPath + 'pdf/individual' , idl ) . toPromise ( ) ; } postDownloadReqForCoursePDFSingle ( idl ) { return this . backendService . post ( this . apiPath + 'pdf/single' , idl ) . toPromise ( ) ; } getFile ( id ) { return this . backendService . getDownload ( this . apiPath + id ) . toPromise ( ) ; } } @ Injectable ( ) export class ConfigService extends DataService { downloadMaxFileSize : number ; constructor ( public backendService ) { super ( 'config/' , backendService ) ; } async getDownloadMaxFileSize ( ) { const res = < IConfig > < any > await this . readSingleItem ( 'public/downloadMaxFileSize' ) ; const _value = Number . parseInt ( res . value ) ; const value = isNaN ( _value ) ? 51200 : _value ; this . downloadMaxFileSize = value ; return value ; } }