lunch/reports.go

252 lines
6.8 KiB
Go
Raw Permalink Normal View History

2017-01-08 09:47:30 -05:00
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)
}
}
2017-01-10 08:08:45 -05:00
func GetAllSuggestions(groupid int64) (*[]*Suggestion, error) {
2017-01-08 09:47:30 -05:00
var suggestions []*Suggestion
2017-01-10 08:08:45 -05:00
_, err := DB.Select(&suggestions, "SELECT * from suggestions WHERE GroupId=?", groupid)
2017-01-08 09:47:30 -05:00
if err != nil {
return nil, err
}
return &suggestions, nil
}
2017-01-10 08:08:45 -05:00
func GetVetoedSuggestions(groupid int64) (*[]*Suggestion, error) {
2017-01-08 09:47:30 -05:00
var suggestions []*Suggestion
2017-01-10 08:08:45 -05:00
_, 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)
2017-01-08 09:47:30 -05:00
if err != nil {
return nil, err
}
return &suggestions, nil
}
2017-01-10 20:31:13 -05:00
func GetWinningSuggestions(groupid int64) (*[]*Suggestion, error) {
2017-01-08 09:47:30 -05:00
var suggestions []*Suggestion
2017-01-10 08:08:45 -05:00
_, 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)
2017-01-08 09:47:30 -05:00
if err != nil {
return nil, err
}
return &suggestions, nil
}
2017-01-10 08:08:45 -05:00
func GetAllAttendees(groupid int64) (*[]*Attendee, error) {
2017-01-08 09:47:30 -05:00
var attendees []*Attendee
2017-01-10 08:08:45 -05:00
_, err := DB.Select(&attendees, "SELECT * from attendees WHERE GroupId=?", groupid)
2017-01-08 09:47:30 -05:00
if err != nil {
return nil, err
}
return &attendees, nil
}
2017-01-10 20:31:13 -05:00
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
}
2017-01-11 09:52:05 -05:00
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
}
2017-01-08 09:47:30 -05:00
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
2017-01-10 20:31:13 -05:00
if reportid == "winning-suggestions" {
report.Title = "Winning Suggestions"
suggestions, err := GetWinningSuggestions(user.GroupId)
2017-01-08 09:47:30 -05:00
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
report.FromSummedSuggestionList(suggestions)
} else if reportid == "vetoed-suggestions" {
report.Title = "Vetoed Suggestions"
2017-01-10 08:08:45 -05:00
suggestions, err := GetVetoedSuggestions(user.GroupId)
2017-01-08 09:47:30 -05:00
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
report.FromSummedSuggestionList(suggestions)
} else if reportid == "suggestions" {
report.Title = "Suggestion Frequency"
2017-01-10 08:08:45 -05:00
suggestions, err := GetAllSuggestions(user.GroupId)
2017-01-08 09:47:30 -05:00
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
report.FromSummedSuggestionList(suggestions)
} else if reportid == "attendees" {
report.Title = "Attendee Frequency"
2017-01-10 08:08:45 -05:00
attendees, err := GetAllAttendees(user.GroupId)
2017-01-08 09:47:30 -05:00
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
report.FromSummedAttendeeList(attendees)
2017-01-10 20:31:13 -05:00
} 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)
2017-01-11 09:52:05 -05:00
} 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)
2017-01-10 20:31:13 -05:00
} 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)
2017-01-08 09:47:30 -05:00
} 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
}
}