Don't remove attendees used in suggestions

This commit is contained in:
Aaron Lindsay 2016-12-29 21:22:35 -05:00
parent 298b3b7cea
commit cfd25d986c
3 changed files with 34 additions and 3 deletions

View File

@ -60,6 +60,12 @@ func (aeu AttendeeExistsError) Error() string {
return "Attendee exists"
}
type AttendeeInUseError struct{}
func (aeu AttendeeInUseError) Error() string {
return "Attendee in use (by suggestion)"
}
func GetAttendees(userid int64, date time.Time) (*[]*Attendee, error) {
var attendees []*Attendee
@ -140,7 +146,16 @@ func DeleteAttendee(a *Attendee) error {
return err
}
// TODO ensure attendee isn't used in any suggestions
// Ensure attendee isn't used in any suggestions
suggestions, err := GetAttendeesSuggestions(transaction, a.UserId, a.Date, a.AttendeeId)
if err != nil {
transaction.Rollback()
return err
}
if len(*suggestions) > 0 {
transaction.Rollback()
return AttendeeInUseError{}
}
count, err := transaction.Delete(a)
if err != nil {
@ -237,8 +252,12 @@ func AttendeeHandler(w http.ResponseWriter, r *http.Request) {
err = DeleteAttendee(attendee)
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
if _, ok := err.(AttendeeInUseError); ok {
WriteError(w, 7 /*Attendee In Use*/)
} else {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
}
return
}

View File

@ -18,6 +18,7 @@ var error_codes = map[int]string{
4: "User Exists",
5: "Attendee Exists",
6: "Suggestion Exists",
7: "Attendee In Use",
// 5: "Connection Failed", //client-side error
999: "Internal Error",
}

View File

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"gopkg.in/gorp.v1"
"log"
"net/http"
"strings"
@ -61,6 +62,16 @@ func (sl *PopularSuggestionList) Write(w http.ResponseWriter) error {
return enc.Encode(sl)
}
func GetAttendeesSuggestions(transaction *gorp.Transaction, userid int64, date time.Time, attendeeid int64) (*[]*Suggestion, error) {
var suggestions []*Suggestion
_, err := transaction.Select(&suggestions, "SELECT * from suggestions WHERE UserId=? AND Date=? AND AttendeeID=?", userid, date, attendeeid)
if err != nil {
return nil, err
}
return &suggestions, nil
}
func GetSuggestions(userid int64, date time.Time) (*[]*Suggestion, error) {
var suggestions []*Suggestion