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(userid int64) (*[]*Suggestion, error) { var suggestions []*Suggestion _, err := DB.Select(&suggestions, "SELECT * from suggestions WHERE UserId=?", userid) if err != nil { return nil, err } return &suggestions, nil } func GetVetoedSuggestions(userid int64) (*[]*Suggestion, error) { var suggestions []*Suggestion _, err := DB.Select(&suggestions, "SELECT suggestions.* FROM suggestions INNER JOIN suggestions AS s2 WHERE suggestions.UserId=? AND s2.UserId=? AND suggestions.Date=s2.Date AND s2.VetoingId=suggestions.SuggestionId", userid, userid) if err != nil { return nil, err } return &suggestions, nil } func GetNonVetoedSuggestions(userid 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.UserId=?;", userid) if err != nil { return nil, err } return &suggestions, nil } func GetAllAttendees(userid int64) (*[]*Attendee, error) { var attendees []*Attendee _, err := DB.Select(&attendees, "SELECT * from attendees WHERE UserId=?", userid) if err != nil { return nil, err } return &attendees, 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 == "non-vetoed-suggestions" { report.Title = "Non-Vetoed Suggestions" suggestions, err := GetNonVetoedSuggestions(user.UserId) 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.UserId) 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.UserId) 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.UserId) 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 } }