| 
					
				 | 
			
			
				@@ -1,10 +1,8 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 package orm 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	"errors" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"fmt" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"net/http" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	"strings" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	"github.com/jinzhu/gorm" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	_ "github.com/jinzhu/gorm/dialects/mysql" 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -147,7 +145,7 @@ var ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		/// JSON 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		"/api/teachers/add": AddTeacher, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		"/teachers/add": AddTeacher, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		// Classes 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		"/classes/{id}/update": UpdateClass, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -166,139 +164,6 @@ var ( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-func (b *TheBoss) Create(db *gorm.DB, record map[string]interface{}) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if record["role"] == "boss" { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result := new(TheBoss) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Name = record["name"].(string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Surname = record["surname"].(string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Username = result.GenerateUsername() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Password = result.GenerateSaltedPassword(result.Username) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		db.Create(result) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		boss := TheBoss{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		teacher := Teacher{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		db.Model(TheBoss{}).First(&boss) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		db.Where("username=?", record["username"]).First(&teacher) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		db.Model(&boss).Association("Teachers").Append(&teacher) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-func (a *Administrative) Create(db *gorm.DB, record map[string]interface{}) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result := Administrative{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result.Name = record["name"].(string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result.Surname = record["surname"].(string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result.Username = result.GenerateUsername() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result.Password = result.GenerateSaltedPassword(result.Username) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	switch record["role"] { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	case "personal_administrator": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Role = ADMINISTRATIVE_ROLE_PERSONAL 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	case "teaching_administrator": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Role = ADMINISTRATIVE_ROLE_TEACHING 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	case "school_caretaker": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Role = ADMINISTRATIVE_ROLE_SCHOOL_CARETAKER 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	case "technical_assistant": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Role = ADMINISTRATIVE_ROLE_TECHNICAL_ASSISTANT 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	case "librarian": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Role = ADMINISTRATIVE_ROLE_LIBRARIAN 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	case "dsga": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Role = ADMINISTRATIVE_ROLE_DSGA 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	default: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Role = ADMINISTRATIVE_ROLE_UNDEFINED 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	db.Create(&result) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-func (d *Director) Create(db *gorm.DB, record map[string]interface{}) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result := new(Director) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if name := record["name"]; name == nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		return errors.New("Error in creating director: field name is empty") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Name = name.(string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if surname := record["surname"]; surname == nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		return errors.New("Error in creating director: field surname is empty") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Surname = surname.(string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	// Generate username and initial password 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result.Username = result.GenerateUsername() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result.Password = result.GenerateSaltedPassword(result.Username) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	db.Set("gorm:save_associations", false).Create(result) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-func (t *Student) Create(db *gorm.DB, record map[string]interface{}) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result := new(Student) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if name := record["name"]; name == nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		return errors.New("Error in creating student: field name is empty") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Name = name.(string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if surname := record["surname"]; surname == nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		return errors.New("Error in creating student: field surname is empty") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Surname = surname.(string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	// Generate username and initial password 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result.Username = result.GenerateUsername() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result.Password = result.GenerateSaltedPassword(result.Username) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	db.Set("gorm:save_associations", false).Create(result) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-func (d *Department) Create(db *gorm.DB, record map[string]interface{}) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	result := new(Department) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	if name := record["name"]; name == nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		return errors.New("Error in updating subject: field name is empty") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		result.Name = name.(string) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	db.Set("gorm:save_associations", false).Create(result) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	// Handle department-subjects relationship 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	for _, s := range record["subjects"].([]interface{}) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		subject := Subject{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		db.Where("name=?", strings.TrimSpace(s.(string))).Find(&subject) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		db.Model(result).Association("Subjects").Append(subject) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	// Handle department-teachers relationship 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	allTeachers := make([]Teacher, 0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	db.Find(&allTeachers) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	for _, t := range allTeachers { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		db.Model(&t).Related(&t.Subjects, "Subjects") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		for _, s := range t.Subjects { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			for _, depS := range result.Subjects { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				if s.Name == depS.Name { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-					db.Model(result).Association("Teachers").Append(t) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-				} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 func New(connection string) (*gorm.DB, error) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	db, err := gorm.Open("mysql", connection) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	if err != nil { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -315,22 +180,6 @@ func DB() *gorm.DB { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return currDB 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-func Reset() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	currDB.DropTableIfExists( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&School{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&Subject{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&Teacher{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&Class{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&Activity{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&Student{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&Department{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&TheBoss{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&Administrative{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&Director{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-		&Issue{}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 func AutoMigrate() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	if err := currDB.AutoMigrate( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 		&School{}, 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -350,8 +199,20 @@ func AutoMigrate() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-func GetTeacherByUsername(username string) (*Teacher, error) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	teacher := Teacher{} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	currDB.Where(fmt.Sprintf("username='%s'", username)).Find(&teacher) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-	return &teacher, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func GetResultFunc(path string) (GetFn, error) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	fn, ok := Get[path] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if !ok { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return nil, fmt.Errorf("Can't map %s!", path) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	return fn, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func PostFunc(path string) (PostFn, error) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	fn, ok := Post[path] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if !ok { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return nil, fmt.Errorf("Can't map %s!", path) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	return fn, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |