Dualcoding con @bachoseven per la pagina "/storia" più qualche miglioria

main-old
Antonio De Lucreziis 3 years ago
parent e3b71962e5
commit b2a9355d1d

@ -1,16 +1,25 @@
module git.phc.dm.unipi.it/phc/website
go 1.13
go 1.18
require (
github.com/alecthomas/chroma v0.9.4
github.com/gofiber/fiber/v2 v2.34.0
github.com/gofiber/redirect/v2 v2.1.23
github.com/joho/godotenv v1.4.0
github.com/klauspost/compress v1.15.6 // indirect
github.com/litao91/goldmark-mathjax v0.0.0-20210217064022-a43cf739a50f
github.com/yuin/goldmark v1.4.4
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/klauspost/compress v1.15.6 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.37.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
)

@ -52,6 +52,8 @@ github.com/yuin/goldmark v1.4.4/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01 h1:0SJnXjE4jDClMW6grE0xpNhwpqbPwkBTn8zpVw5C0SI=
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01/go.mod h1:TwKQPa5XkCCRC2GRZ5wtfNUTQ2+9/i19mGRijFeJ4BE=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f h1:KK6mxegmt5hGJRcAnEDjSNLxIRhZxDcgwMbcO/lMCRM=
golang.org/x/exp v0.0.0-20220602145555-4a0574d9293f/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

@ -1,7 +1,6 @@
package main
import (
"encoding/json"
"html/template"
"git.phc.dm.unipi.it/phc/website/articles"
@ -45,7 +44,7 @@ func main() {
"/": "home.html",
"/link": "link.html",
"/login": "login.html",
"/storia": "storia.html",
"/utenti": "utenti.html",
}
for route, view := range actuallyStaticRoutes {
@ -62,19 +61,18 @@ func main() {
return err
}
c.Type("json")
return json.NewEncoder(c).Encode(utenti)
return c.JSON(utenti)
})
app.Get("/utenti", func(c *fiber.Ctx) error {
utenti, err := GetUtenti()
app.Get("/storia", func(c *fiber.Ctx) error {
storia, err := GetStoria()
if err != nil {
return err
}
c.Type("html")
return renderer.Render(c, "utenti.html", util.H{
"Utenti": utenti,
return renderer.Render(c, "storia.html", util.H{
"Storia": storia,
})
})

@ -800,7 +800,7 @@ form .field-set input {
}
.page-storia .history-container .events .spacer {
height: 4rem;
height: calc(var(--size) * 1rem);
}
@keyframes rotate {

@ -3,40 +3,127 @@ package main
import (
"encoding/json"
"io/ioutil"
"math"
"sort"
"strconv"
"strings"
"git.phc.dm.unipi.it/phc/website/util"
)
type byEventDateDescending []*GenericEvent
func (m byEventDateDescending) Len() int {
return len(m)
}
func (m byEventDateDescending) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m byEventDateDescending) Less(i, j int) bool {
return m[i].Date > m[j].Date
}
type Macchinista struct {
Name string `json:"username"`
Uid string `json:"password"`
Date string `json:"date"`
Type string `json:"type"`
Uid string `json:"uid"`
FullName string `json:"fullName"`
EntryDate string `json:"entryDate"`
ExitDate string `json:"exitDate"`
}
type GenericEvent struct {
// Campi principali
Type string `json:"type"`
Date string `json:"date"`
// Altri campi utilizzati ogni tanto
Title string `json:"title"`
Description string `json:"description"`
Date string `json:"date"`
Type string `json:"type"`
// Se il tipo di evento è riferito ad un utente usiamo anche questo campo apposta
Uid string `json:"uid"`
FullName string `json:"fullName"`
// Se il campo è uno "spacer" questa è la sua dimensione
Size int `json:"size"`
}
type EventsDB struct {
Macchinisti map[string]*Macchinista `json:"macchinisti"`
GenericEvent map[string]*GenericEvent `json:"eventi,omitempty"`
type HistoryDB struct {
Macchinisti []*Macchinista `json:"macchinisti"`
GenericEvent []*GenericEvent `json:"eventi"`
}
func GetEvents() ([]EventsDB, error) {
var events []EventsDB
func GetRawHistory() (*HistoryDB, error) {
var rawHistory HistoryDB
eventsJsonData, err := ioutil.ReadFile("./storia.json")
historyDB, err := ioutil.ReadFile("./storia.json")
if err != nil {
return nil, err
}
if err := json.Unmarshal(eventsJsonData, &events); err != nil {
if err := json.Unmarshal(historyDB, &rawHistory); err != nil {
return nil, err
}
return events, nil
return &rawHistory, nil
}
func getDateYear(date string) int {
year, err := strconv.Atoi(strings.Split(date, "/")[0])
if err != nil {
panic(err) // Tanto nel caso si nota in fase di sviluppo visto che "storia.json" è statico
}
return year
}
func withSpacers(events []*GenericEvent) []*GenericEvent {
// crea un nuovo slice con capacità già almeno "len(events)"
newEvents := make([]*GenericEvent, 0, len(events))
for i := 0; i < len(events)-1; i++ {
newEvents = append(newEvents, events[i])
delta := util.Abs(getDateYear(events[i+1].Date) - getDateYear(events[i].Date))
if delta > 1 {
logDelta := int(math.Log(float64(delta)))
newEvents = append(newEvents, &GenericEvent{
Type: "spacer",
Size: logDelta,
})
}
}
newEvents = append(newEvents, events[len(events)-1])
return newEvents
}
// TODO: Logica per ordinare gli eventi
func GetStoria() ([]*GenericEvent, error) {
history, err := GetRawHistory()
if err != nil {
return nil, err
}
events := history.GenericEvent[:]
for _, m := range history.Macchinisti {
if m.ExitDate != "" {
events = append(events, &GenericEvent{
Type: "exit-macchinista",
Uid: m.Uid,
FullName: m.FullName,
Date: m.ExitDate,
})
}
if m.EntryDate != "" {
events = append(events, &GenericEvent{
Type: "entry-macchinista",
Uid: m.Uid,
FullName: m.FullName,
Date: m.EntryDate,
})
}
}
sort.Sort(byEventDateDescending(events))
return withSpacers(events), nil
}

@ -1,22 +1,44 @@
[
"macchinisti":
{
"macchinisti": [
{
"nome": "Francesco Minnocci",
"uid": "minnocci",
"date": "2022/02",
"type": "entry"
"fullName": "Francesco Minnocci",
"entryDate": "2022/02"
},
{
"uid": "manicastri",
"fullName": "Francesco Manicastri",
"entryDate": "2022/05"
},
{
"uid": "delucreziis",
"fullName": "Antonio De Lucreziis",
"entryDate": "2019/??"
},
{
"nome": "Francesco Caporali",
"uid": "caporali",
"date": "201?/??",
"type": "exit"
"fullName": "Francesco Caporali",
"entryDate": "2018/??",
"exitDate": "2022/03"
},
{
"uid": "dachille",
"fullName": "Letizia D'Achille",
"entryDate": "2018/??",
"exitDate": "2022/03"
}
},
"eventi":
],
"eventi": [
{
"type": "simple",
"title": "Nuovo sito del PHC",
"description": "TBA",
"date": "2022/??"
},
{
"type": "simple",
"title": "Apertura del PHC",
"date": "1992"
}
]
]
}

@ -0,0 +1,17 @@
package util
import (
"golang.org/x/exp/constraints"
)
type Numeric interface {
constraints.Float | constraints.Integer
}
func Abs[T Numeric](a T) T {
if a < 0 {
return -a
}
return a
}

@ -12,68 +12,39 @@
<div class="history-container">
<div class="timeline-bar"></div>
<div class="events">
<!-- {{ range .Events }} -->
<!-- {{ if (.IsMacchinista) }} -->
<!-- {{ if (.IsEntry) }} -->
<!-- <i class="fa-solid fa-sign-in"></i> -->
<!-- {{ end }} -->
<!-- {{ if (.IsExit) }} -->
<!-- {{ end }} -->
<!-- {{ end }} -->
<!-- {{ if (.IsGeneric) }} -->
<!-- <div class="description">{{ .Description }}</div> -->
<!-- {{ if (.IsAnniversary) }} -->
<!-- {{ end }} -->
<!-- {{ end }} -->
<!-- {{ end }} -->
<div class="event">
<div class="title">Nuovo sito del PHC</div>
<div class="date">2022/??</div>
</div>
<div class="spacer"></div>
<div class="event">
<div class="title">Ingresso di <a href="#">Francesco Manicastri</a></div>
<div class="date">2022/05</div>
</div>
<div class="event">
<div class="title">Ingresso di <a href="#">Francesco Minnocci</a></div>
<div class="date">2022/02</div>
</div>
<div class="spacer"></div>
<div class="event">
<div class="title">Ingresso di <a href="#">Antonio De Lucreziis</a></div>
<div class="date">201?/??</div>
</div>
<div class="spacer"></div>
<div class="event">
<div class="title">Ingresso di <a href="#">Francesco Caporali</a></div>
<div class="date">201?/??</div>
</div>
<div class="spacer"></div>
<div class="event">
<div class="title">Ingresso di <a href="#">Letizia D'Achille</a></div>
<div class="date">201?/??</div>
</div>
<div class="spacer"></div>
<div class="event">
<div class="title">Ingresso di <a href="#">Giorgio Mossa</a></div>
<div class="date">201?/??</div>
</div>
<div class="event">
<div class="title">Ingresso di <a href="#">Cristiano Ricci</a></div>
<div class="date">201?/??</div>
</div>
<div class="spacer"></div>
<div class="spacer"></div>
<div class="event">
<div class="title">Prima versione del sito "Poisson"</div>
<div class="date">~1996/06/17</div>
</div>
<div class="spacer"></div>
<div class="event">
<div class="title">Apertura del PHC</div>
<div class="date">~1994</div>
</div>
{{ range .Storia }}
{{ if eq .Type "simple" }}
<div class="event">
<div class="title">
{{ .Title }}
</div>
<div class="date">{{ .Date }}</div>
</div>
{{ end }}
{{ if eq .Type "entry-macchinista" }}
<div class="event">
<div class="title">
Ingresso di
<i class="fa-solid fa-sign-in"></i>
<a href="{{ $.Config.UserPagesBaseUrl }}{{ .Uid }}">{{ .FullName }}</a>
</div>
<div class="date">{{ .Date }}</div>
</div>
{{ end }}
{{ if eq .Type "exit-macchinista" }}
<div class="event">
<div class="title">
Uscita di
<i class="fa-solid fa-sign-out"></i>
<a href="{{ $.Config.UserPagesBaseUrl }}{{ .Uid }}">{{ .FullName }}</a>
</div>
<div class="date">{{ .Date }}</div>
</div>
{{ end }}
{{ if eq .Type "spacer" }}
<div class="spacer" style="--size: {{ .Size }}"></div>
{{ end }}
{{ end }}
</div>
</div>
</section>

Loading…
Cancel
Save