Exemplo n.º 1
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
}
Exemplo n.º 2
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

}