Parcourir la source

Refactoring teachers views

Andrea Fazzi il y a 5 ans
Parent
commit
ec37e99d65

+ 35 - 5
renderer/funcmap.go

@@ -3,20 +3,29 @@ package renderer
 import (
 	"fmt"
 	"html/template"
-	"log"
 	"net/url"
 	"reflect"
+	"strings"
 	"time"
+
+	"github.com/jinzhu/inflection"
 )
 
 var (
 	funcMap = template.FuncMap{
 		"query":       query,
 		"convertDate": convertDate,
-		"model":       model,
+		"modelPath":   modelPath,
+		"slice":       slice,
 	}
 )
 
+func slice(values ...string) []string {
+	var result []string
+	result = append(result, values...)
+	return result
+}
+
 func getType(myvar interface{}) (res string) {
 	t := reflect.TypeOf(myvar)
 	for t.Kind() == reflect.Ptr {
@@ -42,7 +51,28 @@ func convertDate(t time.Time) string {
 	return fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
 }
 
-func model(value interface{}) string {
-	log.Println(value)
-	return getType(value)
+func modelPath(model string, action string, id uint) string {
+	var q template.URL
+
+	action = strings.ToLower(action)
+	plural := inflection.Plural(strings.ToLower(model))
+
+	if action != "" {
+		switch action {
+		case "show":
+			q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
+			return fmt.Sprintf("/%s/%d?%s", plural, id, q)
+		case "update":
+			q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural), "update", "true")
+			return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
+		case "create":
+			q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
+			return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
+		case "delete":
+			q = query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
+			return fmt.Sprintf("/%s/%d/%s?%s", plural, id, action, q)
+		}
+	}
+	q = query("tpl_layout", "base", "tpl_content", plural)
+	return fmt.Sprintf("/%s?%s", plural, q)
 }

+ 2 - 2
templates/layout/create_button.html.tpl

@@ -1,6 +1,6 @@
 {{define "create_button"}}
-<a href="/teachers/create/?{{query "tpl_layout" "base" "tpl_content" "teachers_add_update"}}" class="btn btn-primary">
+<a href="{{index . 1}}" class="btn btn-primary">
   <span class="fa fa-plus-circle" aria-hidden="true"></span>
-  {{.}}
+  {{index . 0}}
 </a>
 {{end}}

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

@@ -0,0 +1,3 @@
+{{define "display_no_elements"}}
+<p>Non c'è alcun elemento da visualizzare</p>
+{{end}}

+ 7 - 0
templates/layout/search_input.html.tpl

@@ -0,0 +1,7 @@
+{{define "search_input"}}
+<div class="input-group" style="margin-bottom: 20px">
+  <span class="input-group-addon" id="search-query"><span class="glyphicon glyphicon-search"></span></span>
+  <input type="text" id="myInput" class="form-control" aria-describedby="search-query">
+</div>
+{{end}}
+

+ 5 - 8
templates/teachers.html.tpl

@@ -10,24 +10,21 @@
       </div>
       <div class="col-md-4">
 	<div class="btn-group float-right">
-	  {{template "create_button" "Crea nuovo docente"}}
+	  {{template "create_button" slice "Crea nuovo docente" (modelPath "teacher" "create" 0)}}
         </div>
       </div>
     </div>
     
   </div>
 
-  <div class="input-group" style="margin-bottom: 20px">
-    <span class="input-group-addon" id="search-query"><span class="glyphicon glyphicon-search"></span></span>
-    <input type="text" id="myInput" class="form-control" aria-describedby="search-query">
-  </div>
-  
+  {{template "search_input"}}
+    
   {{if not .}}
-  <p>Non c'è alcun elemento da visualizzare</p>
+  {{template "display_no_elements"}}
   {{else}}
   <div class="list-group" id="myUL">
     {{range $teacher := .Data}}
-    <a class="list-group-item list-group-item-action" href="/teachers/{{$teacher.ID}}?{{query "tpl_layout" "base" "tpl_content" "teachers_show"}}">
+    <a class="list-group-item list-group-item-action" href={{$teacher.ID|modelPath "teacher" "show"}}>
       <span class="fa fa-user"></span>
       {{$teacher.Surname}} {{$teacher.Name}}
       <div class="text-right">

+ 13 - 13
templates/teachers_show.html.tpl

@@ -4,7 +4,7 @@
 
   <nav aria-label="breadcrumb">
     <ol class="breadcrumb">
-      <li class="breadcrumb-item"><a href="/teachers?{{query "tpl_layout" "base" "tpl_content" "teachers"}}">Docenti</a></li>
+      <li class="breadcrumb-item"><a href="{{modelPath "Teacher" "" 0}}">Docenti</a></li>
       <li class="breadcrumb-item active"><a href="#">{{.Data.Name}} {{.Data.Surname}}</a></li>
     </ol>
   </nav>
@@ -16,7 +16,7 @@
       </div>
       <div class="col-md-4">
 	<div class="btn-group float-right" role="group">
-	  <a href="/teachers/{{.Data.ID}}/update?{{query "tpl_layout" "base" "tpl_content" "teachers_add_update" "update" "true"}}" class="btn btn-primary">
+	  <a href="{{.Data.ID|modelPath "Teacher" "update"}}" class="btn btn-primary">
 	    <span class="fa fa-edit" aria-hidden="true"></span>
 	    Modifica
 	  </a>
@@ -38,7 +38,7 @@
       {{if .Data.Subjects}}
       <div class="list-group" id="materie_list_group">
     	{{range $subject := .Data.Subjects}}
-    	<a href="/subjects/{{$subject.ID}}?{{query "tpl_layout" "base" "tpl_content" "subjects_show"}}" class="list-group-item list-group-item-action">
+    	<a href="{{$subject.ID | modelPath "Subject" "show"}}" class="list-group-item list-group-item-action">
     	  <span class="fa fa-book"></span>
     	  {{$subject.Name}}
     	  {{end}}
@@ -46,7 +46,7 @@
       </div>
       {{else}}
       <p>Al docente non è associata alcuna attività
-    	didattica. Clicca <a href="/activities/create/?{{query "tpl_layout" "base" "tpl_content" "activities_add_update"}}">qui</a> per
+    	didattica. Clicca <a href="{{modelPath "Activity" "create" 0}}">qui</a> per
     	creare una nuova attività didattica da associare al docente.</p>
       {{end}}
     </div>
@@ -60,7 +60,7 @@
       {{if .Data.Classes}}
       <div class="list-group" id="classes_list_group">
 	{{range $class := .Data.Classes}}
-	<a href="/classes/{{$class.ID}}?{{query "tpl_layout" "base" "tpl_content" "classes_show"}}" class="list-group-item list-group-item-action">
+	<a href="{{$class.ID | modelPath "Class" "show"}}" class="list-group-item list-group-item-action">
 	  <span class="fa fa-users"></span>
 	  {{$class.Name}}
 	  {{end}}
@@ -68,7 +68,7 @@
       </div>
       {{else}}
       <p>Al docente non è associata alcuna attività
-	didattica. Clicca <a href="/activities/create/?{{query "tpl_layout" "base" "tpl_content" "activities_add_update"}}">qui</a> per
+	didattica. Clicca <a href="{{modelPath "Activity" "create" 0}}">qui</a> per
 	creare una nuova attività didattica da associare al docente.</p>
       {{end}}
     </div>
@@ -82,7 +82,7 @@
       {{if .Data.Students}}
       <div class="list-group" id="students_list_group">
 	{{range $student := .Data.Students}}
-	<a href="/students/{{$student.ID}}?{{query "tpl_layout" "base" "tpl_content" "students_show"}}" class="list-group-item list-group-item-action">
+	<a href="{{$student.ID | modelPath "Student" "show"}}" class="list-group-item list-group-item-action">
 	  <span class="fa fa-user"></span>
 	  {{$student.Surname}} {{$student.Name}}
 	  {{end}}
@@ -90,7 +90,7 @@
       </div>
       {{else}}
       <p>Al docente non è associata alcuna attività
-	didattica. Clicca <a href="/activities/create/?{{query "tpl_layout" "base" "tpl_content" "activities_add_update"}}">qui</a> per
+	didattica. Clicca <a href="{{modelPath "Activity" "create" 0}}">qui</a> per
 	creare una nuova attività didattica da associare al docente.</p>
       {{end}}
     </div>
@@ -104,9 +104,9 @@
       {{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">
+	<a href="{{$activity.ID | modelPath "Activity" "show"}}" class="list-group-item list-group-item-action">
 	  <span class="fa fa-business-time"></span>
-	  {{$activity.Subject.Name}} {{if $activity.Class}}{{$activity.Class.Name}}{{else}}no classe{{end}} {{$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}}
+	  {{$activity.Subject.Name}}{{if $activity.Class}}{{$activity.Class.Name}}{{else}}no classe{{end}} {{$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>
@@ -115,7 +115,7 @@
       <p>Il docente è <strong>coordinatore</strong> nelle seguenti classi</p>
       <div class="list-group" id="coord_classes_list_group">
 	{{range $class := $classes}}
-	<a href="/classes/{{$class.ID}}?{{query "tpl_layout" "base" "tpl_content" "classes_show"}}" class="list-group-item list-group-item-action">
+	<a href="{{$class.ID | modelPath "Class" "show"}}" class="list-group-item list-group-item-action">
 	  <span class="fa fa-users"></span>
 	  {{$class.Name}}
 	  {{end}}
@@ -127,7 +127,7 @@
       <p>Il docente è <strong>verbalizzante</strong> nelle seguenti classi</p>
       <div class="list-group" id="coord_classes_list_group">
 	{{range $class := $classes}}
-	<a href="/classes/{{$class.ID}}?{{query "tpl_layout" "base" "tpl_content" "classes_show"}}" class="list-group-item list-group-item-action">
+	<a href="{{$class.ID|modelPath "Class" "show"}}" class="list-group-item list-group-item-action">
 	  <span class="fa fa-users"></span>
 	  {{$class.Name}}
 	  {{end}}
@@ -138,7 +138,7 @@
       
       {{else}}
       <p>Al docente non è associata alcuna attività
-	didattica. Clicca <a href="/activities/create/?{{query "base" "activities_add_update"}}">qui</a> per
+	didattica. Clicca <a href="{{modelPath "Activity" "create" 0}}">qui</a> per
 	creare una nuova attività didattica da associare al docente.</p>
       {{end}}
     </div>