diff --git a/octav/handlers.go b/octav/handlers.go index f262575..575ece1 100644 --- a/octav/handlers.go +++ b/octav/handlers.go @@ -11,6 +11,7 @@ import ( "github.com/lestrrat/go-apache-logformat" "github.com/lestrrat/go-jsval" "github.com/lestrrat/go-pdebug" + "github.com/pkg/errors" "golang.org/x/net/context" ) @@ -533,8 +534,8 @@ func doCreateSession(ctx context.Context, w http.ResponseWriter, r *http.Request return } - if err := s.Decorate(tx, &v); err != nil { - httpError(w, `LookupSession`, http.StatusInternalServerError, err) + if err := errors.Wrap(s.Decorate(tx, &v), "failed to decorate session with associated data"); err != nil { + httpError(w, `CreateSession`, http.StatusInternalServerError, err) return } @@ -1106,6 +1107,11 @@ func doListVenue(ctx context.Context, w http.ResponseWriter, r *http.Request, pa } func doLookupSession(ctx context.Context, w http.ResponseWriter, r *http.Request, payload model.LookupSessionRequest) { + if pdebug.Enabled { + g := pdebug.Marker("doLookupSession") + defer g.End() + } + tx, err := db.Begin() if err != nil { httpError(w, `LookupSession`, http.StatusInternalServerError, err) @@ -1120,7 +1126,7 @@ func doLookupSession(ctx context.Context, w http.ResponseWriter, r *http.Request } s := service.Session{} - if err := s.Decorate(tx, &v); err != nil { + if err := errors.Wrap(s.Decorate(tx, &v), "failed to decorate session with associated data"); err != nil { httpError(w, `LookupSession`, http.StatusInternalServerError, err) return } diff --git a/octav/service/session.go b/octav/service/session.go index 905712d..c2b4a3c 100644 --- a/octav/service/session.go +++ b/octav/service/session.go @@ -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/lestrrat/go-pdebug" "github.com/pkg/errors" ) @@ -190,27 +191,35 @@ func (v *Session) LoadByConference(tx *db.Tx, vdbl *db.SessionList, cid string, } func (v *Session) Decorate(tx *db.Tx, session *model.Session) error { + if pdebug.Enabled { + g := pdebug.Marker("service.Session.Decorate") + defer g.End() + } // 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") + if session.ConferenceID != "" { + conf := model.Conference{} + if err := conf.Load(tx, session.ConferenceID); err != nil { + return errors.Wrap(err, "failed to load conference") + } + session.Conference = &conf } - session.Conference = &conf // ... but not necessarily with a room if session.RoomID != "" { room := model.Room{} - if err := conf.Load(tx, session.RoomID); err != nil { + if err := room.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") + if session.SpeakerID != "" { + speaker := model.User{} + if err := speaker.Load(tx, session.SpeakerID); err != nil { + return errors.Wrapf(err, "failed to load speaker '%s'", session.SpeakerID) + } + session.Speaker = &speaker } - session.Speaker = &speaker return nil } diff --git a/octav/sql/octav.sql b/octav/sql/octav.sql index 351f4df..c0a3d8b 100644 --- a/octav/sql/octav.sql +++ b/octav/sql/octav.sql @@ -100,9 +100,9 @@ CREATE TABLE conference_venues ( CREATE TABLE sessions ( oid INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, eid CHAR(64) CHARACTER SET latin1 NOT NULL, - conference_id CHAR(64) CHARACTER SET latin1 NOT NULL, + conference_id CHAR(64) CHARACTER SET latin1, room_id CHAR(64) CHARACTER SET latin1, - speaker_id CHAR(64) NOT NULL, + speaker_id CHAR(64) CHARACTER SET latin1, title TEXT NOT NULL, abstract TEXT, memo TEXT, @@ -124,6 +124,8 @@ CREATE TABLE sessions ( confirmed TINYINT(1) NOT NULL DEFAULT 0, created_on DATETIME NOT NULL, modified_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + FOREIGN KEY (speaker_id) REFERENCES users(eid) ON DELETE SET NULL, + FOREIGN KEY (conference_id) REFERENCES conferences(eid) ON DELETE SET NULL, UNIQUE KEY (eid), KEY(eid, conference_id, room_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;