Advertisement
Przemyslaw8888

Untitled

Jan 20th, 2020
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Course, CourseType } from './course'
  3. import { AngularFireDatabase } from '@angular/fire/database';
  4. import { COURSES } from './mock-course'
  5. import { Rating } from './rating';
  6. import { of, Observable } from 'rxjs';
  7.  
  8.  
  9. @Injectable({
  10.   providedIn: 'root'
  11. })
  12. export class CourseService {
  13.  
  14.   private courses: Object;
  15.   private idToCourse :number;
  16.  
  17.   constructor(private db: AngularFireDatabase) {
  18.     this.getIdToCourse().subscribe(id => this.idToCourse = id);
  19.     this.getCourses().subscribe(courses => this.courses=courses);
  20.   }
  21.  
  22.   getIdToCourse(): Observable<any>{
  23.     return this.db.list('idToCourse').valueChanges();
  24.   }
  25.  
  26.   getCourses(): Observable<any>{
  27.     return this.db.list('courses').valueChanges();
  28.   }
  29.  
  30.   getCourse(id: number){
  31.     return Object.values(this.courses).filter(course => {return course.id == id})[0];
  32.   }
  33.  
  34.   deleteCourse(id: number){
  35.     this.db.list('courses/' + String(id)).remove();
  36.   }
  37.  
  38.   editCourse(course: Course){
  39.  
  40.     this.db.list('courses/' + course.id).set('name',  course.name);
  41.     this.db.list('courses/' + course.id).set('ECTS',  course.ECTS);
  42.     this.db.list('courses/' + course.id).set('semester',  course.semester);
  43.     this.db.list('courses/' + course.id).set('formCourse',  course.formCourse);
  44.     this.db.list('courses/' + course.id).set('maxStudents',  course.maxStudents);
  45.     this.db.list('courses/' + course.id).set('description',  course.description);
  46.     this.db.list('courses/' + course.id).set('image',  course.image);
  47.   }
  48.  
  49.   canEnrollForCourse(user: string, id: number){
  50.     const course = Object.values(this.courses).filter(course => {return course.id == id})[0];
  51.     if(course.students === undefined) return true;
  52.     if(!(course.students.length < course.maxStudents)) return false;
  53.     for(var i = 1; i < course.students.length; i++){
  54.       if(course.students[String(i)] === user) {
  55.         return false;
  56.       }
  57.     }
  58.     return true;
  59.   }
  60.  
  61.   studentIsEnrolled(user: string, id: number){
  62.     const course = Object.values(this.courses).filter(course => {return course.id == id})[0];
  63.     if(course.students === undefined) return false;
  64.     for(var i = 1; i < course.students.length; i++){
  65.       if(course.students[String(i)] === user) {
  66.         return true;
  67.       }
  68.     }
  69.     return false;
  70.   }
  71.  
  72.   enrollForCourse(user: string, id: number){
  73.     const course = Object.values(this.courses).filter(course => {return course.id == id})[0];
  74.     let students: number;
  75.     if(course.students === undefined) students = 1;
  76.     else students = course.students.length;
  77.  
  78.     this.db.list('courses/' + id + '/students').set(String(students),  user);
  79.   }
  80.  
  81.   canRate(user: string, id: number){
  82.     const course = Object.values(this.courses).filter(course => {return course.id == id})[0];
  83.  
  84.     if(course.ratings === undefined) return true;
  85.  
  86.     for(var i = 1; i < course.ratings.length; i++){
  87.       if(course.ratings[String(i)].studentID === user) return false;
  88.     }
  89.  
  90.     return true;
  91.   }
  92.  
  93.   getAvgRatingFromCourse(id: number){
  94.     const course = Object.values(this.courses).filter(course => {return course.id == id})[0];
  95.  
  96.     if(course.ratings === undefined) return 0;
  97.  
  98.     var sum = 0;
  99.     course.ratings.forEach(element => {
  100.       sum += element.rating;
  101.     });
  102.     return Math.round(100 * (sum / (course.ratings.length - 1))) / 100; // Najpierw mnoże przez 10 aby zaokrąglić do całośći a potem dzielę żeby mieć części dziesiętne
  103.   }
  104.  
  105.   addRateToCourse(id: number, rate: number, user: string){
  106.     const course = Object.values(this.courses).filter(course => {return course.id == id})[0];
  107.  
  108.     const Rating = {
  109.       rating: rate,
  110.       studentID: user
  111.     };
  112.  
  113.     let rates: number;
  114.     if(course.ratings === undefined) rates = 1;
  115.     else rates = course.ratings.length;
  116.  
  117.     this.db.list('courses/' + id + '/ratings').set(String(rates),  Rating);
  118.     return Rating.rating;
  119.   }
  120.  
  121.   getUserRate(user: string, id: number){
  122.     const course = Object.values(this.courses).filter(course => {return course.id == id})[0];
  123.  
  124.     if(course.ratings === undefined) return "";
  125.    
  126.     for(var i = 1; i < course.ratings.length; i++){
  127.       if(course.ratings[String(i)].studentID === user) return course.ratings[String(i)].rating.toString();
  128.     }
  129.  
  130.     return "";
  131.   }
  132.  
  133.   addCourse(course: Course){
  134.     let id: number;
  135.     if(this.idToCourse[0] === undefined) id = 1;
  136.     else id = this.idToCourse[0] + 1
  137.     course.id = id;
  138.  
  139.     this.db.list('courses').set(String(id), course);
  140.     this.db.list('idToCourse').set('idToCourse',id);
  141.     return COURSES.push(course);
  142.   }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement