153 lines
3.1 KiB
Go
153 lines
3.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type Suggestion struct {
|
||
|
SuggestionId int64
|
||
|
VetoingId int64 // -1 for initial suggestion
|
||
|
AttendeeId int64
|
||
|
UserId int64 `json:"-"`
|
||
|
RestaurantName string
|
||
|
Date time.Time `json:"-"`
|
||
|
}
|
||
|
|
||
|
type SuggestionList struct {
|
||
|
Suggestions *[]*Suggestion `json:"suggestions"`
|
||
|
}
|
||
|
|
||
|
func (s *Suggestion) Write(w http.ResponseWriter) error {
|
||
|
enc := json.NewEncoder(w)
|
||
|
return enc.Encode(s)
|
||
|
}
|
||
|
|
||
|
func (s *Suggestion) Read(json_str string) error {
|
||
|
dec := json.NewDecoder(strings.NewReader(json_str))
|
||
|
return dec.Decode(s)
|
||
|
}
|
||
|
|
||
|
func (sl *SuggestionList) Write(w http.ResponseWriter) error {
|
||
|
enc := json.NewEncoder(w)
|
||
|
return enc.Encode(sl)
|
||
|
}
|
||
|
|
||
|
type SuggestionExistsError struct{}
|
||
|
|
||
|
func (aeu SuggestionExistsError) Error() string {
|
||
|
return "Suggestion exists"
|
||
|
}
|
||
|
|
||
|
func GetSuggestions(userid int64, date time.Time) (*[]*Suggestion, error) {
|
||
|
var suggestions []*Suggestion
|
||
|
|
||
|
_, err := DB.Select(&suggestions, "SELECT * from suggestions WHERE UserId=? AND Date=?", userid, date)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &suggestions, nil
|
||
|
}
|
||
|
|
||
|
func InsertSuggestion(s *Suggestion) error {
|
||
|
transaction, err := DB.Begin()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
existing, err := transaction.SelectInt("SELECT count(*) from suggestions where RestaurantName=? AND UserId=? AND Date=?", s.RestaurantName, s.UserId, s.Date)
|
||
|
if err != nil {
|
||
|
transaction.Rollback()
|
||
|
return err
|
||
|
}
|
||
|
if existing > 0 {
|
||
|
transaction.Rollback()
|
||
|
return SuggestionExistsError{}
|
||
|
}
|
||
|
|
||
|
err = transaction.Insert(s)
|
||
|
if err != nil {
|
||
|
transaction.Rollback()
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
err = transaction.Commit()
|
||
|
if err != nil {
|
||
|
transaction.Rollback()
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func SuggestionHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
user, err := GetUserFromSession(r)
|
||
|
if err != nil {
|
||
|
WriteError(w, 1 /*Not Signed In*/)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
today := time.Now().Truncate(time.Hour * 24)
|
||
|
|
||
|
if r.Method == "POST" {
|
||
|
suggestion_json := r.PostFormValue("suggestion")
|
||
|
if suggestion_json == "" {
|
||
|
WriteError(w, 3 /*Invalid Request*/)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var suggestion Suggestion
|
||
|
err := suggestion.Read(suggestion_json)
|
||
|
if err != nil {
|
||
|
WriteError(w, 3 /*Invalid Request*/)
|
||
|
return
|
||
|
}
|
||
|
suggestion.SuggestionId = -1
|
||
|
suggestion.UserId = user.UserId
|
||
|
suggestion.Date = today
|
||
|
|
||
|
err = InsertSuggestion(&suggestion)
|
||
|
if err != nil {
|
||
|
if _, ok := err.(SuggestionExistsError); ok {
|
||
|
WriteError(w, 6 /*Suggestion Exists*/)
|
||
|
} else {
|
||
|
WriteError(w, 999 /*Internal Error*/)
|
||
|
log.Print(err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
w.WriteHeader(201 /*Created*/)
|
||
|
err = suggestion.Write(w)
|
||
|
if err != nil {
|
||
|
WriteError(w, 999 /*Internal Error*/)
|
||
|
log.Print(err)
|
||
|
return
|
||
|
}
|
||
|
} else if r.Method == "GET" {
|
||
|
var sl SuggestionList
|
||
|
|
||
|
suggestions, err := GetSuggestions(user.UserId, today)
|
||
|
if err != nil {
|
||
|
WriteError(w, 999 /*Internal Error*/)
|
||
|
log.Print(err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
sl.Suggestions = suggestions
|
||
|
err = (&sl).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
|
||
|
}
|
||
|
}
|