Skip to content

Commit

Permalink
Merge pull request #99 from builderscon/topic/issue-98
Browse files Browse the repository at this point in the history
Properly decorate the session
  • Loading branch information
lestrrat authored Jul 9, 2016
2 parents 45891c3 + 8417e42 commit d82d178
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
31 changes: 22 additions & 9 deletions octav/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,18 @@ func doCreateSession(ctx context.Context, w http.ResponseWriter, r *http.Request
return
}

if err := tx.Commit(); err != nil {
v := model.Session{}
if err := v.FromRow(vdb); err != nil {
httpError(w, `CreateSession`, http.StatusInternalServerError, err)
return
}

v := model.Session{}
if err := v.FromRow(vdb); err != nil {
if err := s.Decorate(tx, &v); err != nil {
httpError(w, `LookupSession`, http.StatusInternalServerError, err)
return
}

if err := tx.Commit(); err != nil {
httpError(w, `CreateSession`, http.StatusInternalServerError, err)
return
}
Expand Down Expand Up @@ -1114,12 +1119,20 @@ func doLookupSession(ctx context.Context, w http.ResponseWriter, r *http.Request
return
}

if payload.Lang.Valid() {
s := service.Session{}
if err := s.ReplaceL10NStrings(tx, &v, payload.Lang.String); err != nil {
httpError(w, `LookupSession`, http.StatusInternalServerError, err)
return
}
s := service.Session{}
if err := s.Decorate(tx, &v); err != nil {
httpError(w, `LookupSession`, http.StatusInternalServerError, err)
return
}

if !payload.Lang.Valid() {
httpJSON(w, v)
return
}

if err := s.ReplaceL10NStrings(tx, &v, payload.Lang.String); err != nil {
httpError(w, `LookupSession`, http.StatusInternalServerError, err)
return
}

httpJSON(w, v)
Expand Down
8 changes: 4 additions & 4 deletions octav/model/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type RoomList []Room
type Session struct {
ID string `json:"id"`
ConferenceID string `json:"conference_id"`
RoomID string `json:"room_id"`
RoomID string `json:"room_id,omitempty"`
SpeakerID string `json:"speaker_id"`
Title string `json:"title" l10n:"true"`
Abstract string `json:"abstract" l10n:"true"`
Expand All @@ -73,9 +73,9 @@ type Session struct {
HasInterpretation bool `json:"has_interpretation"`
Status string `json:"status"`
Confirmed bool `json:"confirmed"`
Conference *Conference `json:"conference"` // only populated for JSON response
Room *Room `json:"room"` // only populated for JSON response
Speaker *User `json:"speaker"` // only populated for JSON response
Conference *Conference `json:"conference,omitempy"` // only populated for JSON response
Room *Room `json:"room,omitempty"` // only populated for JSON response
Speaker *User `json:"speaker,omitempty"` // only populated for JSON response
}
type SessionList []Session

Expand Down
26 changes: 26 additions & 0 deletions octav/service/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/builderscon/octav/octav/db"
"github.com/builderscon/octav/octav/model"
"github.com/builderscon/octav/octav/tools"
"github.com/pkg/errors"
)

func (v *Session) populateRowForCreate(vdb *db.Session, payload model.CreateSessionRequest) error {
Expand Down Expand Up @@ -188,3 +189,28 @@ func (v *Session) LoadByConference(tx *db.Tx, vdbl *db.SessionList, cid string,
return nil
}

func (v *Session) Decorate(tx *db.Tx, session *model.Session) error {
// session must be associated with a conference
conf := model.Conference{}
if err := conf.Load(tx, session.ConferenceID); err != nil {
return errors.Wrap(err, "failed to load conference")
}
session.Conference = &conf

// ... but not necessarily with a room
if session.RoomID != "" {
room := model.Room{}
if err := conf.Load(tx, session.RoomID); err != nil {
return errors.Wrap(err, "failed to load room")
}
session.Room = &room
}

speaker := model.User{}
if err := speaker.Load(tx, session.SpeakerID); err != nil {
return errors.Wrap(err, "failed to load speaker")
}
session.Speaker = &speaker

return nil
}

0 comments on commit d82d178

Please sign in to comment.