lunch/attendees.go

296 lines
6.0 KiB
Go
Raw Permalink Normal View History

2016-12-23 08:30:29 -05:00
package main
import (
"encoding/json"
"errors"
2016-12-23 08:30:29 -05:00
"log"
"net/http"
"strings"
2016-12-24 10:00:21 -05:00
"time"
2016-12-23 08:30:29 -05:00
)
type Attendee struct {
AttendeeId int64
2017-01-10 08:08:45 -05:00
GroupId int64 `json:"-"`
2016-12-23 08:30:29 -05:00
Name string
2016-12-24 10:00:21 -05:00
Date time.Time `json:"-"`
2016-12-23 08:30:29 -05:00
}
type PopularAttendee struct {
Name string
Popularity int64
}
2016-12-23 08:30:29 -05:00
type AttendeeList struct {
Attendees *[]*Attendee `json:"attendees"`
}
type PopularAttendeeList struct {
PopularAttendees *[]*PopularAttendee `json:"popularattendees"`
}
2016-12-23 08:30:29 -05:00
func (a *Attendee) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(a)
}
func (a *Attendee) Read(json_str string) error {
dec := json.NewDecoder(strings.NewReader(json_str))
return dec.Decode(a)
}
func (al *AttendeeList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(al)
}
func (pa *PopularAttendee) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(pa)
}
func (pal *PopularAttendeeList) Write(w http.ResponseWriter) error {
enc := json.NewEncoder(w)
return enc.Encode(pal)
}
2016-12-23 08:30:29 -05:00
type AttendeeExistsError struct{}
func (aeu AttendeeExistsError) Error() string {
return "Attendee exists"
}
type AttendeeInUseError struct{}
func (aeu AttendeeInUseError) Error() string {
return "Attendee in use (by suggestion)"
}
2017-01-10 08:08:45 -05:00
func GetAttendees(groupid int64, date time.Time) (*[]*Attendee, error) {
2016-12-23 08:30:29 -05:00
var attendees []*Attendee
2017-01-10 08:08:45 -05:00
_, err := DB.Select(&attendees, "SELECT * from attendees WHERE GroupId=? AND Date=?", groupid, date)
2016-12-23 08:30:29 -05:00
if err != nil {
return nil, err
}
return &attendees, nil
}
func GetPopularAttendees() (*[]*PopularAttendee, error) {
var attendees []*Attendee
attendeeMap := make(map[string]int64)
popularAttendees := make([]*PopularAttendee, 0)
_, err := DB.Select(&attendees, "SELECT * from attendees")
if err != nil {
return nil, err
}
for i := range attendees {
attendeeMap[attendees[i].Name] += 1
}
for name, count := range attendeeMap {
var popularAttendee PopularAttendee
popularAttendee.Name = name
popularAttendee.Popularity = count
popularAttendees = append(popularAttendees, &popularAttendee)
}
return &popularAttendees, nil
}
2016-12-23 08:30:29 -05:00
func InsertAttendee(a *Attendee) error {
transaction, err := DB.Begin()
if err != nil {
return err
}
2017-01-10 08:08:45 -05:00
existing, err := transaction.SelectInt("SELECT count(*) from attendees where GroupId=? AND Name=? AND Date=?", a.GroupId, a.Name, a.Date)
2016-12-23 08:30:29 -05:00
if err != nil {
transaction.Rollback()
return err
}
if existing > 0 {
transaction.Rollback()
return AttendeeExistsError{}
}
err = transaction.Insert(a)
if err != nil {
transaction.Rollback()
return err
}
err = transaction.Commit()
if err != nil {
transaction.Rollback()
return err
}
return nil
}
2017-01-10 08:08:45 -05:00
func GetAttendee(attendeeid int64, groupid int64, date time.Time) (*Attendee, error) {
var a Attendee
2017-01-10 08:08:45 -05:00
err := DB.SelectOne(&a, "SELECT * from attendees where GroupId=? AND AttendeeId=? AND Date=?", groupid, attendeeid, date)
if err != nil {
return nil, err
}
return &a, nil
}
func DeleteAttendee(a *Attendee) error {
transaction, err := DB.Begin()
if err != nil {
return err
}
// Ensure attendee isn't used in any suggestions
2017-01-10 08:08:45 -05:00
suggestions, err := GetAttendeesSuggestions(transaction, a.GroupId, a.Date, a.AttendeeId)
if err != nil {
transaction.Rollback()
return err
}
if len(*suggestions) > 0 {
transaction.Rollback()
return AttendeeInUseError{}
}
2016-12-29 20:15:39 -05:00
count, err := transaction.Delete(a)
if err != nil {
transaction.Rollback()
return err
}
if count != 1 {
transaction.Rollback()
return errors.New("Was going to delete more than one attendee")
}
err = transaction.Commit()
if err != nil {
transaction.Rollback()
return err
}
return nil
}
2016-12-23 08:30:29 -05:00
func AttendeeHandler(w http.ResponseWriter, r *http.Request) {
2016-12-24 10:00:21 -05:00
user, err := GetUserFromSession(r)
2016-12-23 08:30:29 -05:00
if err != nil {
WriteError(w, 1 /*Not Signed In*/)
return
}
2017-09-15 13:28:39 -04:00
today := time.Now().Truncate(time.Hour * 24)
2016-12-24 10:00:21 -05:00
2016-12-23 08:30:29 -05:00
if r.Method == "POST" {
attendee_json := r.PostFormValue("attendee")
if attendee_json == "" {
WriteError(w, 3 /*Invalid Request*/)
return
}
var attendee Attendee
err := attendee.Read(attendee_json)
if err != nil {
WriteError(w, 3 /*Invalid Request*/)
return
}
attendee.AttendeeId = -1
2017-01-10 08:08:45 -05:00
attendee.GroupId = user.GroupId
2016-12-24 10:00:21 -05:00
attendee.Date = today
2016-12-23 08:30:29 -05:00
err = InsertAttendee(&attendee)
if err != nil {
if _, ok := err.(AttendeeExistsError); ok {
WriteError(w, 5 /*Attendee Exists*/)
} else {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
}
return
}
w.WriteHeader(201 /*Created*/)
err = attendee.Write(w)
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
} else if r.Method == "GET" {
var al AttendeeList
2017-01-10 08:08:45 -05:00
attendees, err := GetAttendees(user.GroupId, today)
2016-12-23 08:30:29 -05:00
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
al.Attendees = attendees
err = (&al).Write(w)
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
} else if r.Method == "DELETE" {
attendeeid, err := GetURLID(r.URL.Path)
if err != nil {
WriteError(w, 3 /* Invalid Request */)
return
}
2017-01-10 08:08:45 -05:00
attendee, err := GetAttendee(attendeeid, user.GroupId, today)
if err != nil {
WriteError(w, 3 /*Invalid Request*/)
return
}
err = DeleteAttendee(attendee)
if err != nil {
if _, ok := err.(AttendeeInUseError); ok {
WriteError(w, 7 /*Attendee In Use*/)
} else {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
}
return
}
WriteSuccess(w)
2016-12-23 08:30:29 -05:00
} else {
/* No PUT */
2016-12-23 08:30:29 -05:00
WriteError(w, 3 /*Invalid Request*/)
return
}
}
func PopularAttendeeHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
var pal PopularAttendeeList
attendees, err := GetPopularAttendees()
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return
}
pal.PopularAttendees = attendees
err = (&pal).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
}
}