func (m *Message) convert(im profile.Message) error { m.Id = im.Id m.Sent = im.Sent m.Body = im.Body ip, err := profile.GetProfile(im.Sender) if err != nil { return err } m.Sender = Profile{} err = m.Sender.convert(*ip) if im.Photo == nil { // short circuit out o' here; we're done return nil } ph, err := profile.GetPhoto(*im.Photo) if err != nil { return err } // dammit, we shouldn't have to keep running back to the database for this ip, err = profile.GetProfile(ph.Profile) if err != nil { return err } href := ph.GetExpiringUrl(ip.Folder) m.Photo = &Photo{ph.Id, ph.Created, href, ph.Caption} return nil }
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 }
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 }
func findProfile(u *url.URL, c *Context) (*profile.Profile, func(string, ...interface{}) (int, http.Header, Response, error), error) { id := param(u, "id") if id == "" { return c.Profile, nil, nil } intId, err := strconv.Atoi(id) if err != nil { complaint := "'" + id + "' is not a valid Profile Id." return nil, error400, errors.New(complaint) } ip, err := profile.GetProfile(intId) if err != nil { if err.Error() == profile.NotFoundError { return nil, error404, errors.New("profile not found") } return nil, error500, err } return ip, nil, nil }
func (i *Invite) convert(ii profile.Invite) error { i.Id = ii.Id i.Active = ii.Active i.Start = ii.Start i.End = ii.End i.Created = ii.Created i.Place = ii.Place // Organizer and Attendees are or contain Profiles, so there's some hoops to jump through ip, err := profile.GetProfile(ii.Organizer) if err != nil { return err } i.Organizer = Profile{} err = i.Organizer.convert(*ip) if err != nil { return err } for _, att := range ii.Attendees { log.Println("got status", att.Status, "for attendee", att.Profile.Id) p := Profile{} err = p.convert(att.Profile) if err != nil { return err } i.Attendees = append(i.Attendees, Attendee{p, att.Status}) } for _, im := range ii.Messages { m := Message{} err = m.convert(im) if err != nil { return err } i.Messages = append(i.Messages, m) } return nil }