252 lines
6.8 KiB
Go
252 lines
6.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type ReportElement struct {
|
|
Label string
|
|
Value int64
|
|
}
|
|
|
|
type Report struct {
|
|
ReportId string
|
|
Title string
|
|
Data []*ReportElement
|
|
}
|
|
|
|
func (r *ReportElement) Write(w http.ResponseWriter) error {
|
|
enc := json.NewEncoder(w)
|
|
return enc.Encode(r)
|
|
}
|
|
|
|
func (r *Report) Write(w http.ResponseWriter) error {
|
|
enc := json.NewEncoder(w)
|
|
return enc.Encode(r)
|
|
}
|
|
|
|
func (r *Report) FromSummedAttendeeList(attendees *[]*Attendee) {
|
|
attendeeMap := make(map[string]int64)
|
|
r.Data = make([]*ReportElement, 0)
|
|
|
|
for i := range *attendees {
|
|
attendeeMap[(*attendees)[i].Name] += 1
|
|
}
|
|
for name, count := range attendeeMap {
|
|
var element ReportElement
|
|
element.Label = name
|
|
element.Value = count
|
|
r.Data = append(r.Data, &element)
|
|
}
|
|
}
|
|
|
|
func (r *Report) FromSummedSuggestionList(suggestions *[]*Suggestion) {
|
|
suggestionMap := make(map[string]int64)
|
|
r.Data = make([]*ReportElement, 0)
|
|
|
|
for i := range *suggestions {
|
|
suggestionMap[(*suggestions)[i].RestaurantName] += 1
|
|
}
|
|
for name, count := range suggestionMap {
|
|
var element ReportElement
|
|
element.Label = name
|
|
element.Value = count
|
|
r.Data = append(r.Data, &element)
|
|
}
|
|
}
|
|
|
|
func GetAllSuggestions(groupid int64) (*[]*Suggestion, error) {
|
|
var suggestions []*Suggestion
|
|
|
|
_, err := DB.Select(&suggestions, "SELECT * from suggestions WHERE GroupId=?", groupid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &suggestions, nil
|
|
}
|
|
|
|
func GetVetoedSuggestions(groupid int64) (*[]*Suggestion, error) {
|
|
var suggestions []*Suggestion
|
|
|
|
_, err := DB.Select(&suggestions, "SELECT suggestions.* FROM suggestions INNER JOIN suggestions AS s2 WHERE suggestions.GroupId=? AND s2.GroupId=? AND suggestions.Date=s2.Date AND s2.VetoingId=suggestions.SuggestionId", groupid, groupid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &suggestions, nil
|
|
}
|
|
|
|
func GetWinningSuggestions(groupid int64) (*[]*Suggestion, error) {
|
|
var suggestions []*Suggestion
|
|
|
|
_, err := DB.Select(&suggestions, "SELECT suggestions.* FROM suggestions LEFT OUTER JOIN suggestions AS s2 ON suggestions.SuggestionId=s2.VetoingId WHERE s2.SuggestionId IS NULL AND suggestions.GroupId=?;", groupid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &suggestions, nil
|
|
}
|
|
|
|
func GetAllAttendees(groupid int64) (*[]*Attendee, error) {
|
|
var attendees []*Attendee
|
|
|
|
_, err := DB.Select(&attendees, "SELECT * from attendees WHERE GroupId=?", groupid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &attendees, nil
|
|
}
|
|
|
|
func GetAllSuggestors(groupid int64) (*[]*Attendee, error) {
|
|
var suggestors []*Attendee
|
|
|
|
_, err := DB.Select(&suggestors, "SELECT attendees.* FROM attendees INNER JOIN suggestions ON suggestions.AttendeeId==attendees.AttendeeId WHERE suggestions.GroupId=?", groupid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &suggestors, nil
|
|
}
|
|
|
|
func GetVetoedSuggestors(groupid int64) (*[]*Attendee, error) {
|
|
var suggestors []*Attendee
|
|
|
|
_, err := DB.Select(&suggestors, "SELECT attendees.* FROM attendees INNER JOIN (SELECT suggestions.* FROM suggestions INNER JOIN suggestions AS s2 WHERE suggestions.GroupId=? AND s2.GroupId=? AND suggestions.Date=s2.Date AND s2.VetoingId=suggestions.SuggestionId) a ON a.AttendeeId==attendees.AttendeeId", groupid, groupid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &suggestors, nil
|
|
}
|
|
|
|
func GetWinningSuggestors(groupid int64) (*[]*Attendee, error) {
|
|
var suggestors []*Attendee
|
|
|
|
_, err := DB.Select(&suggestors, "SELECT attendees.* FROM attendees INNER JOIN (SELECT suggestions.* FROM suggestions LEFT OUTER JOIN suggestions AS s2 ON suggestions.SuggestionId=s2.VetoingId WHERE s2.SuggestionId IS NULL AND suggestions.GroupId=?) a ON a.AttendeeId==attendees.AttendeeId", groupid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &suggestors, nil
|
|
}
|
|
|
|
func GetVetoingSuggestors(groupid int64) (*[]*Attendee, error) {
|
|
var suggestors []*Attendee
|
|
|
|
_, err := DB.Select(&suggestors, "SELECT attendees.* FROM attendees INNER JOIN suggestions ON suggestions.AttendeeId==attendees.AttendeeId WHERE suggestions.VetoingId!=-1 AND suggestions.GroupId=?", groupid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &suggestors, nil
|
|
}
|
|
|
|
func ReportHandler(w http.ResponseWriter, r *http.Request) {
|
|
user, err := GetUserFromSession(r)
|
|
if err != nil {
|
|
WriteError(w, 1 /*Not Signed In*/)
|
|
return
|
|
}
|
|
|
|
if r.Method == "GET" {
|
|
var report Report
|
|
|
|
query, _ := url.ParseQuery(r.URL.RawQuery)
|
|
reportid := query.Get("id")
|
|
report.ReportId = reportid
|
|
|
|
if reportid == "winning-suggestions" {
|
|
report.Title = "Winning Suggestions"
|
|
suggestions, err := GetWinningSuggestions(user.GroupId)
|
|
if err != nil {
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
log.Print(err)
|
|
return
|
|
}
|
|
report.FromSummedSuggestionList(suggestions)
|
|
} else if reportid == "vetoed-suggestions" {
|
|
report.Title = "Vetoed Suggestions"
|
|
suggestions, err := GetVetoedSuggestions(user.GroupId)
|
|
if err != nil {
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
log.Print(err)
|
|
return
|
|
}
|
|
report.FromSummedSuggestionList(suggestions)
|
|
} else if reportid == "suggestions" {
|
|
report.Title = "Suggestion Frequency"
|
|
suggestions, err := GetAllSuggestions(user.GroupId)
|
|
if err != nil {
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
log.Print(err)
|
|
return
|
|
}
|
|
report.FromSummedSuggestionList(suggestions)
|
|
} else if reportid == "attendees" {
|
|
report.Title = "Attendee Frequency"
|
|
attendees, err := GetAllAttendees(user.GroupId)
|
|
if err != nil {
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
log.Print(err)
|
|
return
|
|
}
|
|
report.FromSummedAttendeeList(attendees)
|
|
} else if reportid == "winning-suggestors" {
|
|
report.Title = "Winning Suggestors"
|
|
attendees, err := GetWinningSuggestors(user.GroupId)
|
|
if err != nil {
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
log.Print(err)
|
|
return
|
|
}
|
|
report.FromSummedAttendeeList(attendees)
|
|
} else if reportid == "vetoed-suggestors" {
|
|
report.Title = "Vetoed Suggestors"
|
|
attendees, err := GetVetoedSuggestors(user.GroupId)
|
|
if err != nil {
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
log.Print(err)
|
|
return
|
|
}
|
|
report.FromSummedAttendeeList(attendees)
|
|
} else if reportid == "vetoing-suggestors" {
|
|
report.Title = "Vetoing Suggestors"
|
|
attendees, err := GetVetoingSuggestors(user.GroupId)
|
|
if err != nil {
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
log.Print(err)
|
|
return
|
|
}
|
|
report.FromSummedAttendeeList(attendees)
|
|
} else if reportid == "suggestors" {
|
|
report.Title = "Suggestor Frequency"
|
|
attendees, err := GetAllSuggestors(user.GroupId)
|
|
if err != nil {
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
log.Print(err)
|
|
return
|
|
}
|
|
report.FromSummedAttendeeList(attendees)
|
|
} else {
|
|
WriteError(w, 3 /*Invalid Request*/)
|
|
return
|
|
}
|
|
|
|
err = (&report).Write(w)
|
|
if err != nil {
|
|
WriteError(w, 999 /*Internal Error*/)
|
|
log.Print(err)
|
|
return
|
|
}
|
|
} else {
|
|
/* No POST, PUT, or DELETE */
|
|
WriteError(w, 3 /*Invalid Request*/)
|
|
return
|
|
}
|
|
}
|