144 lines
2.6 KiB
Go
144 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type Attendee struct {
|
|
AttendeeId int64
|
|
Name string
|
|
}
|
|
|
|
type AttendeeList struct {
|
|
Attendees *[]*Attendee `json:"attendees"`
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
type AttendeeExistsError struct{}
|
|
|
|
func (aeu AttendeeExistsError) Error() string {
|
|
return "Attendee exists"
|
|
}
|
|
|
|
func GetAttendees() (*[]*Attendee, error) {
|
|
var attendees []*Attendee
|
|
|
|
_, err := DB.Select(&attendees, "SELECT * from attendees")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &attendees, nil
|
|
}
|
|
|
|
func InsertAttendee(a *Attendee) error {
|
|
transaction, err := DB.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
existing, err := transaction.SelectInt("SELECT count(*) from users where Name=?", a.Name)
|
|
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
|
|
}
|
|
|
|
func AttendeeHandler(w http.ResponseWriter, r *http.Request) {
|
|
_, err := GetUserFromSession(r)
|
|
if err != nil {
|
|
WriteError(w, 1 /*Not Signed In*/)
|
|
return
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
|
|
attendees, err := GetAttendees()
|
|
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 {
|
|
/* No PUT or DELETE */
|
|
WriteError(w, 3 /*Invalid Request*/)
|
|
return
|
|
}
|
|
}
|