Explorar o código

Working on a query to get supply teachers

Andrea Fazzi %!s(int64=6) %!d(string=hai) anos
pai
achega
9078dddd5d
Modificáronse 3 ficheiros con 25 adicións e 2 borrados
  1. 6 0
      orm/orm_test.go
  2. 17 0
      orm/teacher.go
  3. 2 2
      templates/teachers_show.html.tpl

+ 6 - 0
orm/orm_test.go

@@ -170,6 +170,12 @@ func (t *testSuite) TestGetTeachersAll() {
 	t.Equal("MATEMATICA", teachers.([]*Teacher)[0].Subjects[0].Name)
 }
 
+func (t *testSuite) TestGetSupplyTeachers() {
+	teachers, err := GetSupplyTeachers()
+	t.Nil(err)
+	t.Equal(11, len(teachers))
+}
+
 func (t *testSuite) TestGetTeacherActByClass() {
 	teacher, err := GetTeacher(map[string]string{"id": "1"})
 	t.Nil(err)

+ 17 - 0
orm/teacher.go

@@ -12,6 +12,8 @@ import (
 type Teacher struct {
 	gorm.Model
 	Credential
+
+	Supply      bool `gorm:"-"`
 	Classes     []*Class
 	Students    []*Student
 	Subjects    []*Subject
@@ -40,6 +42,12 @@ WHERE (activities.teacher_id=? OR activities.supply_teacher_id=?)`
 SELECT subjects.* FROM activities 
 INNER JOIN subjects on subjects.id=activities.subject_id 
 WHERE (activities.teacher_id=? OR activities.supply_teacher_id=?) AND (activities.class_id=?)
+`
+
+	selectSupplyTeachers = `
+SELECT teachers.*,1 as supply FROM activities 
+INNER JOIN teachers on teachers.id=activities.teacher_id 
+WHERE supply_teacher_id <> 0 ORDER BY surname,name
 `
 )
 
@@ -120,6 +128,15 @@ func GetTeachersFast(args map[string]string) (interface{}, error) {
 	for _, teacher := range teachers {
 		teacher.GetSubjects()
 	}
+
+	return teachers, nil
+}
+
+func GetSupplyTeachers() ([]*Teacher, error) {
+	var teachers []*Teacher
+	if err := DB().Raw(selectSupplyTeachers).Scan(&teachers).Error; err != nil {
+		return nil, err
+	}
 	return teachers, nil
 }
 

+ 2 - 2
templates/teachers_show.html.tpl

@@ -100,13 +100,13 @@
   <div class="row">
     <div class="col-md-12">
    
-      <h2 class="karmen-relation-header">Cattedra</h2>
+      <h2 class="karmen-relation-header">Attività del docente</h2>
       {{if .Data.Activities}}
       <div class="list-group" id="classes_list_group">
 	{{range $activity := .Data.Activities}}
 	<a href="/activities/{{$activity.ID}}?{{query "tpl_layout" "base" "tpl_content" "activities_show"}}" class="list-group-item list-group-item-action">
 	  <span class="fa fa-business-time"></span>
-	  {{$activity.Subject.Name}} {{$activity.Class.Name}} {{$activity.Hours}}h
+	  {{$activity.Subject.Name}} {{$activity.Class.Name}} {{$activity.Hours}}h{{if $activity.SupplyTeacher}}{{if ne $.Data.Surname $activity.SupplyTeacher.Surname}} (sostituito da {{$activity.SupplyTeacher.Surname}}){{else}} (sostituisce {{$activity.Teacher.Surname}}){{end}}{{end}}
 	  {{end}}
 	</a>
       </div>