Example #1
0
func cancelInvite(u *url.URL, h http.Header, _ interface{}, c *Context) (int, http.Header, Response, error) {
	id := param(u, "id")
	intId, err := strconv.Atoi(id)
	if err != nil {
		return error400("'"+id+"' is not a valid Invite Id.", "Bad invite id.")
	}
	ii, err := profile.GetInvite(intId)
	if err != nil {
		errString := err.Error()
		if errString == profile.NotFoundError {
			return error404("Invite not found.", errString)
		}
		return error500("db failure: p269", errString)
	}
	err = ii.Cancel()
	if err != nil {
		return error500("db failure: p273", err.Error())
	}
	ii.Active = false
	i := Invite{}
	err = i.convert(*ii)
	if err != nil {
		return error500("db failure: p279", err.Error())
	}
	return http.StatusOK, nil, i, nil
}
Example #2
0
func invite(u *url.URL, h http.Header, i *NewInvite, c *Context) (int, http.Header, Response, error) {
	if i.End != nil && !i.Start.Before(*i.End) {
		complaint := i.End.String() + " is not after " + i.Start.String()
		return error400(complaint, "got bad Invite representation")
	}
	if len(i.Attendees) < 1 {
		complaint := "There must be at least one attendee for an invite."
		return error400(complaint, "got Invite without any attendees")
	}
	var atts []profile.Attendee
	for _, att := range i.Attendees {
		p, err := profile.GetProfile(att)
		if err != nil {
			complaint := "'" + strconv.Itoa(att) + "' is not a valid Profile id."
			return error400(complaint, "got a non-Profile Id for an attendee")
		}
		atts = append(atts, profile.Attendee{*p, profile.StatusPending})
	}
	ii := profile.Invite{}
	ii.Attendees = atts
	ii.Organizer = c.Profile.Id
	ii.Active = true
	ii.Start = i.Start
	ii.End = i.End
	ii.Created = time.Now()
	ii.Place = i.Place
	err := ii.Create()
	if err != nil {
		return error500("db failure: p175", err.Error())
	}
	if i.Message != nil {
		m := profile.Message{0, time.Now(), c.Profile.Id, ii.Id, i.Message.Photo, i.Message.Body}
		err := m.Create()
		if err != nil {
			return error500("db failure: p245", err.Error())
		}
	}
	newI, err := profile.GetInvite(ii.Id)
	if err != nil {
		return error500("db failure: p250", err.Error())
	}
	out := Invite{}
	err = out.convert(*newI)
	if err != nil {
		return error500("db failure: p227", err.Error())
	}

	return http.StatusOK, nil, out, nil
}
Example #3
0
func getInvite(u *url.URL, h http.Header, _ interface{}, c *Context) (int, http.Header, Response, error) {
	id := param(u, "id")
	intId, err := strconv.Atoi(id)
	if err != nil {
		return error400("'"+id+"' is not a valid Invite Id.", "Bad invite id.")
	}
	ii, err := profile.GetInvite(intId)
	if err != nil {
		return error500("db failure: p208", err.Error())
	}
	i := Invite{}
	err = i.convert(*ii)
	if err != nil {
		return error500("db failure: p204", err.Error())
	}
	return http.StatusOK, nil, i, nil
}
Example #4
0
func addAttendees(u *url.URL, h http.Header, as []int, c *Context) (int, http.Header, Response, error) {
	id := param(u, "id")
	intId, err := strconv.Atoi(id)
	if err != nil {
		return error400("'"+id+"' is not a valid Invite Id.", "Bad invite id.")
	}
	ii, err := profile.GetInvite(intId)
	if err != nil {
		errString := err.Error()
		if errString == profile.NotFoundError {
			return error404("Invite not found.", errString)
		}
		return error500("db failure: p373", errString)
	}
	if ii.Organizer != c.Profile.Id {
		return error403("You are not the Organizer for this Invite.", "Bad organizer!")
	}

	var atts []profile.Attendee
	for _, a := range as {
		p, err := profile.GetProfile(a)
		if err != nil {
			complaint := "'" + strconv.Itoa(a) + "' is not a valid Profile id."
			return error400(complaint, "got a non-Profile Id for an attendee")
		}
		atts = append(atts, profile.Attendee{*p, profile.StatusPending})
	}

	err = ii.AddAttendees(atts)
	if err != nil {
		return error500("db failure: p392", err.Error())
	}

	err = ii.RefreshAttendees(nil)
	if err != nil {
		return error500("db failure: p397", err.Error())
	}

	i := Invite{}
	err = i.convert(*ii)
	if err != nil {
		return error500("db failure: p289", err.Error())
	}
	return http.StatusOK, nil, i, nil
}
Example #5
0
func addMessage(u *url.URL, h http.Header, m *NewMessage, c *Context) (int, http.Header, Response, error) {
	id := param(u, "id")
	intId, err := strconv.Atoi(id)
	if err != nil {
		return error400("'"+id+"' is not a valid Invite Id.", "Bad invite id.")
	}
	ii, err := profile.GetInvite(intId)
	if err != nil {
		errString := err.Error()
		if errString == profile.NotFoundError {
			return error404("Invite not found.", errString)
		}
		return error500("db failure: p268", errString)
	}
	// if there's a photo, check that it exists and is owned by us
	if m.Photo != nil {
		photoId := *m.Photo
		_, err := c.Profile.GetPhoto(photoId)
		if err != nil {
			return error400("'"+strconv.Itoa(photoId)+"' is not a valid Photo Id.", "Bad photo id")
		}
	}

	im := profile.Message{0, time.Now(), c.Profile.Id, ii.Id, m.Photo, m.Body}
	err = im.Create()
	if err != nil {
		return error500("db failure: p273", err.Error())
	}
	ii.RefreshMessages(nil)

	i := Invite{}
	err = i.convert(*ii)
	if err != nil {
		return error500("db failure: p280", err.Error())
	}
	return http.StatusOK, nil, i, nil

}
Example #6
0
func changeStatus(u *url.URL, h http.Header, s *profile.Status, c *Context) (int, http.Header, Response, error) {
	status := *s
	if s == nil || !profile.Statuses[status] {
		complaint := "'" + string(status) + "' doesn't appear to be a valid status: "
		complaint += strings.Join(profile.StatusStrings(), ", ")
		return error400(complaint, "non-Status status update")
	}
	id := param(u, "id")
	intId, err := strconv.Atoi(id)
	if err != nil {
		return error400("'"+id+"' is not a valid Invite Id.", "Bad invite id.")
	}
	ii, err := profile.GetInvite(intId)
	if err != nil {
		errString := err.Error()
		if errString == profile.NotFoundError {
			return error404("Invite not found.", errString)
		}
		return error500("db failure: p270", errString)
	}
	err = ii.ChangeStatus(*c.Profile, status)
	if err != nil {
		return error500("db failure: p278", err.Error())
	}
	// let's avoid going back for another dozen db queries...
	for i := range ii.Attendees {
		if ii.Attendees[i].Id == c.Profile.Id {
			ii.Attendees[i].Status = status
		}
	}
	i := Invite{}
	err = i.convert(*ii)
	if err != nil {
		return error500("db failure: p289", err.Error())
	}
	return http.StatusOK, nil, i, nil
}