Explorar o código

Add Operator MVC

Andrea Fazzi %!s(int64=4) %!d(string=hai) anos
pai
achega
c3e822b16f

+ 1 - 0
main.go

@@ -44,6 +44,7 @@ var (
 		&orm.Document{},
 		&orm.Job{},
 		&orm.File{},
+		&orm.Operator{},
 	}
 )
 

+ 122 - 0
orm/operator.go

@@ -0,0 +1,122 @@
+package orm
+
+import (
+	"fmt"
+	"net/http"
+	"strings"
+
+	"github.com/jinzhu/gorm"
+	"gogs.carduccidante.edu.it/karmen/core/renderer"
+)
+
+type Operator struct {
+	gorm.Model
+
+	Lastname  string
+	Firstname string
+	Email     string
+
+	Description string
+}
+
+func (o *Operator) GetID() uint { return o.ID }
+func (o *Operator) String() string {
+	return fmt.Sprintf("%s %s (%s)", strings.ToUpper(o.Lastname), strings.Title(o.Firstname), o.Description)
+}
+
+func (o *Operator) Create(args map[string]string, r *http.Request) (interface{}, error) {
+	if r.Method == "GET" {
+		operator := new(Operator)
+		return operator, nil
+	} else {
+		operator := new(Operator)
+		err := renderer.Decode(operator, r)
+		if err != nil {
+			return nil, err
+		}
+		operator, err = CreateOperator(operator)
+		if err != nil {
+			return nil, err
+		}
+		return operator, nil
+	}
+}
+
+func (o *Operator) Read(args map[string]string, r *http.Request) (interface{}, error) {
+	var operator Operator
+
+	id := args["id"]
+
+	if err := DB().Where("id = ?", id).Find(&operator).Error; err != nil {
+		return nil, err
+	}
+
+	return &operator, nil
+}
+
+func (o *Operator) ReadAll(args map[string]string, r *http.Request) (interface{}, error) {
+	var operators []*Operator
+
+	if err := DB().Find(&operators).Error; err != nil {
+		return nil, err
+	}
+	return operators, nil
+}
+
+func (o *Operator) Update(args map[string]string, r *http.Request) (interface{}, error) {
+	if r.Method == "GET" {
+		result, err := o.Read(args, r)
+		if err != nil {
+			return nil, err
+		}
+
+		operator := result.(*Operator)
+
+		return operator, nil
+	} else {
+		operator, err := o.Read(args, r)
+		if err != nil {
+			return nil, err
+		}
+
+		err = renderer.Decode(operator, r)
+		if err != nil {
+			return nil, err
+		}
+		_, err = SaveOperator(operator)
+		if err != nil {
+			return nil, err
+		}
+		operator, err = o.Read(args, r)
+		if err != nil {
+			return nil, err
+		}
+
+		return operator.(*Operator), nil
+	}
+}
+
+func (o *Operator) Delete(args map[string]string, r *http.Request) (interface{}, error) {
+	operator, err := o.Read(args, r)
+	if err != nil {
+		return nil, err
+	}
+	if err := DB().Unscoped().Delete(operator.(*Operator)).Error; err != nil {
+		return nil, err
+	}
+	return operator.(*Operator), nil
+}
+
+func SaveOperator(operator interface{}) (interface{}, error) {
+	if err := DB().Save(operator).Error; err != nil {
+		return nil, err
+	}
+	return operator, nil
+}
+
+func CreateOperator(operator *Operator) (*Operator, error) {
+	if err := DB().Create(operator).Error; err != nil {
+		return nil, err
+	}
+	return operator, nil
+}

+ 1 - 0
orm/orm.go

@@ -81,6 +81,7 @@ func AutoMigrate() {
 		&GeneratorType{},
 		&Log{},
 		&Group{},
+		&Operator{},
 	).Error; err != nil {
 		panic(err)
 	}

+ 41 - 8
orm/student.go

@@ -33,16 +33,21 @@ type Student struct {
 	TutorID uint `schema:"tutor_id"`
 	ClassID uint `schema:"class_id"`
 
+	OperatorIDs []uint      `schema:"operator_ids" gorm:"-"`
+	Operators   []*Operator `gorm:"many2many:student_operators"`
+
 	Class      *Class
 	Tutor      *Teacher
 	Teachers   []*Teacher
 	Activities []*Activity
 
-	AllClasses  []*Class   `gorm:"-"`
-	AllTeachers []*Teacher `gorm:"-"`
+	AllClasses   []*Class    `gorm:"-"`
+	AllTeachers  []*Teacher  `gorm:"-"`
+	AllOperators []*Operator `gorm:"-"`
 
-	SelectedClass   map[uint]string `gorm:"-"`
-	SelectedTeacher map[uint]string `gorm:"-"`
+	SelectedClass    map[uint]string `gorm:"-"`
+	SelectedTeacher  map[uint]string `gorm:"-"`
+	SelectedOperator map[uint]string `gorm:"-"`
 }
 
 var (
@@ -65,7 +70,9 @@ func (s *Student) Create(args map[string]string, r *http.Request) (interface{},
 		if err := DB().Find(&student.AllClasses).Error; err != nil {
 			return nil, err
 		}
-
+		if err := DB().Find(&student.AllOperators).Error; err != nil {
+			return nil, err
+		}
 		return student, nil
 	} else {
 		student := new(Student)
@@ -87,7 +94,7 @@ func (s *Student) Read(args map[string]string, r *http.Request) (interface{}, er
 
 	id := args["id"]
 
-	if err := DB().Preload("Class").Preload("Class.Coordinator").Where("id = ?", id).Find(&student).Error; err != nil {
+	if err := DB().Preload("Class").Preload("Class.Coordinator").Preload("Operators").Where("id = ?", id).Find(&student).Error; err != nil {
 		return nil, err
 	}
 
@@ -131,12 +138,21 @@ func (s *Student) Update(args map[string]string, r *http.Request) (interface{},
 			return nil, err
 		}
 
+		if err := DB().Find(&student.AllOperators).Error; err != nil {
+			return nil, err
+		}
+
 		student.SelectedTeacher = make(map[uint]string)
 		student.SelectedTeacher[student.TutorID] = "selected"
 
 		student.SelectedClass = make(map[uint]string)
 		student.SelectedClass[student.ClassID] = "selected"
 
+		student.SelectedOperator = make(map[uint]string)
+		for _, o := range student.Operators {
+			student.SelectedOperator[o.ID] = "selected"
+		}
+
 		return student, nil
 	} else {
 		student, err := s.Read(args, r)
@@ -153,10 +169,24 @@ func (s *Student) Update(args map[string]string, r *http.Request) (interface{},
 		if err != nil {
 			return nil, err
 		}
-		_, err = SaveStudent(student)
+		if err := DB().
+			Where([]uint(student.(*Student).OperatorIDs)).
+			Find(&student.(*Student).Operators).Error; err != nil {
+			return nil, err
+		}
+
+		_, err = SaveStudent(student.(*Student))
 		if err != nil {
 			return nil, err
 		}
+
+		if err := DB().
+			Model(student).
+			Association("Operators").
+			Replace(student.(*Student).Operators).Error; err != nil {
+			return nil, err
+		}
+
 		student, err = s.Read(args, r)
 		if err != nil {
 			return nil, err
@@ -177,7 +207,7 @@ func (s *Student) Delete(args map[string]string, r *http.Request) (interface{},
 	return student.(*Student), nil
 }
 
-func SaveStudent(student interface{}) (interface{}, error) {
+func SaveStudent(student *Student) (interface{}, error) {
 	if err := DB().Omit("Class", "Tutor", "Teachers", "Activities").Save(student).Error; err != nil {
 		return nil, err
 	}
@@ -185,6 +215,9 @@ func SaveStudent(student interface{}) (interface{}, error) {
 }
 
 func CreateStudent(student *Student) (*Student, error) {
+	if err := DB().Where([]uint(student.OperatorIDs)).Find(&student.Operators).Error; err != nil {
+		return nil, err
+	}
 	if err := DB().Create(student).Error; err != nil {
 		return nil, err
 	}

+ 3 - 0
templates/layout/base.html.tpl

@@ -21,6 +21,9 @@
 	   <li class="nav-item">
              <a class="nav-link" href="/teachers?{{query "tpl_layout" "base" "tpl_content" "teachers"}}">Docenti</a>
 	   </li>
+	   <li class="nav-item">
+	     <a class="nav-item nav-link" href="/operators?{{query "tpl_layout" "base" "tpl_content" "operators"}}">Operatori</a>
+	   </li>
 	   <li class="nav-item">
 	     <a class="nav-link" href="/departments?{{query "tpl_layout" "base" "tpl_content" "departments"}}">Dipartimenti</a>
 	   </li>

+ 30 - 0
templates/operators.html.tpl

@@ -0,0 +1,30 @@
+{{ define "content" }}
+
+<div class="container">  
+
+  {{$options := `
+  title: "Operatori"
+  buttonTitle: "Crea nuovo operatore"
+  `}}
+  
+  {{template "read_all_header" dict  "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "Operator")}}
+  {{template "search_input"}}
+    
+  {{if not .}}
+  {{template "display_no_elements"}}
+  {{else}}
+  <div class="list-group" id="myUL">
+    {{range $operator := .Data}}
+    <a class="list-group-item list-group-item-action" href={{$operator.ID|show "Operator"}}>
+      <span class="fa fa-user"></span>
+      {{$operator|string}}
+    </a>
+    {{end}}
+    {{end}}
+  </div>
+
+</div>
+
+{{ end }}
+
+

+ 41 - 0
templates/operators_add_update.html.tpl

@@ -0,0 +1,41 @@
+{{ define "content" }}
+
+<div class="container">
+
+  {{$update := .Options.Get "update"}}
+
+  {{if $update}}
+  {{template "breadcrumb" toSlice "Operatori" (all "Operator") (.Data|string) (.Data.ID|show "Operator") "Aggiorna" "current"}}
+  {{else}}
+  {{template "breadcrumb" toSlice "Operatori" (all "Operator") "Aggiungi" "current"}}
+  {{end}}
+  
+  {{template "add_update_header" dict "update" $update "addTitle" "Crea nuovo operatore" "updateTitle" (printf "Aggiorna operatore %s" (.Data|string))}}
+
+  {{$form := "form_operators_add_update"}}
+  <form
+    class="needs-validation"
+    action="{{if $update}}{{.Data.ID|update "Operator"}}{{else}}{{create "Operator"}}{{end}}"
+    method="POST"
+    role="form"
+    id={{$form}}>
+
+    {{$options := ` { name: "Firstname",id: "operator_firstname",label: "Nome",placeholder: "Nome ",type: "text"} `}}
+    {{template "input" dict "options" ($options|yaml) "value" (.Data|field "Firstname") "update" $update}}
+
+    {{$options := ` { name: "Lastname",id: "operator_lastname",label: "Cognome",placeholder: "Cognome",type: "text"} `}}
+    {{template "input" dict "options" ($options|yaml) "value" (.Data|field "Lastname") "update" $update}}
+
+    {{$options := ` { name: "Email",id: "operator_email",label: "Email",placeholder: "Email",type: "text"} `}}
+    {{template "input" dict "options" ($options|yaml) "value" (.Data|field "Email") "update" $update}}
+
+    {{$options := ` { name: "Description",id: "operator_description",label: "Descrizione",placeholder: "Descrizione",type: "text"} `}}
+    {{template "input" dict "options" ($options|yaml) "value" (.Data|field "Description") "update" $update}}
+
+    {{ $options := ` { cancelTitle: "Annulla", saveTitle: "Salva", model: "Student"} ` }}
+    {{template "submit_cancel_buttons" dict "options" ($options|yaml) "id" (.Data|field "ID") "update" $update}}  
+  </form>
+  
+</div>
+
+{{ end }}

+ 21 - 0
templates/operators_show.html.tpl

@@ -0,0 +1,21 @@
+{{ define "content" }}
+
+<div class="container">
+
+  {{template "breadcrumb" toSlice "Operatori" (all "Operator") (.Data|string) "current"}}
+  {{template "show_header" dict "title" (.Data|string) "updatePath" (.Data.ID|update "Operator") "deletePath" (.Data.ID|delete "Operator")}}
+
+  <h2 class="karmen-relation-header">Informazioni generali</h2>
+  <dl class="row">
+    <dt class="col-sm-3">Nome</dt>
+    <dd class="col-sm-9">{{.Data.Firstname}}</dd>
+    <dt class="col-sm-3">Cognome</dt>
+    <dd class="col-sm-9">{{.Data.Lastname}}</dd>
+    <dt class="col-sm-3">Email</dt>
+    <dd class="col-sm-9"><a href="mailto:{{.Data.Email}}">{{.Data.Email}}</a></dd>
+    <dt class="col-sm-3">Descrizione</dt>
+    <dd class="col-sm-9">{{.Data.Description}}</dd>
+  </dl>
+</div>    
+
+{{ end }}

+ 3 - 0
templates/students_add_update.html.tpl

@@ -41,6 +41,9 @@
     {{$options := ` { name: "BES",id: "student_bes",label: "BES (L. 170/10)",formClass: "form-group form-check" } `}}
     {{template "checkbox" dict "options" ($options|yaml) "value" (.Data|field "BES") "update" $update}}
 
+    {{$options := ` { name: "operator_ids", id: "operator_ids", label: "Operatori", title: "Seleziona gli operatori associati allo studente", multiple: "true"}`}}
+    {{template "select" dict "options" ($options|yaml) "data" (.Data|field "AllOperators") "selected" (.Data|field "SelectedOperator") "update" $update "form" $form}}
+
     {{$options := ` { name: "FatherLastname",id: "student_fatherlastname",label: "Cognome del padre",placeholder: "Cognome del padre",type: "text"} `}}
     {{template "input" dict "options" ($options|yaml) "value" (.Data|field "FatherLastname") "update" $update}}