本文整理汇总了Golang中github.com/russross/meddler.Load函数的典型用法代码示例。如果您正苦于以下问题:Golang Load函数的具体用法?Golang Load怎么用?Golang Load使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Load函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Get
func (db *repoStore) Get(ID int64) (*model.Repo, error) {
repo := new(model.Repo)
err := meddler.Load(db, repoTable, repo, ID)
if err != nil {
return nil, err
}
return repo, nil
}
示例2: PutProblemBundle
// PutProblemBundle handles a request to /v2/problem_bundles/:problem_id,
// updating an existing problem.
// The bundle must have a full set of passing commits signed by the daycare.
// If any assignments exist that refer to this problem, then the updates cannot change the number
// of steps in the problem.
func PutProblemBundle(w http.ResponseWriter, tx *sql.Tx, params martini.Params, bundle ProblemBundle, render render.Render) {
if bundle.Problem == nil {
loggedHTTPErrorf(w, http.StatusBadRequest, "bundle must contain a problem")
return
}
if bundle.Problem.ID <= 0 {
loggedHTTPErrorf(w, http.StatusBadRequest, "updated problem must have ID > 0")
return
}
old := new(Problem)
if err := meddler.Load(tx, "problems", old, bundle.Problem.ID); err != nil {
loggedHTTPDBNotFoundError(w, err)
return
}
if bundle.Problem.Unique != old.Unique {
loggedHTTPErrorf(w, http.StatusBadRequest, "updating a problem cannot change its unique ID from %q to %q; create a new problem instead", old.Unique, bundle.Problem.Unique)
return
}
if bundle.Problem.ProblemType != old.ProblemType {
loggedHTTPErrorf(w, http.StatusBadRequest, "updating a problem cannot change its type from %q to %q; create a new problem instead", old.ProblemType, bundle.Problem.ProblemType)
return
}
if !bundle.Problem.CreatedAt.Equal(old.CreatedAt) {
loggedHTTPErrorf(w, http.StatusBadRequest, "updating a problem cannot change its created time from %v to %v", old.CreatedAt, bundle.Problem.CreatedAt)
return
}
var assignmentCount int
if err := tx.QueryRow(`SELECT COUNT(1) FROM assignments INNER JOIN problem_sets ON assignments.problem_set_id = problem_sets.id INNER JOIN problem_set_problems ON problem_sets.id = problem_set_problems.problem_set_id WHERE problem_set_problems.problem_id = $1`, bundle.Problem.ID).Scan(&assignmentCount); err != nil {
loggedHTTPErrorf(w, http.StatusInternalServerError, "db error: %v", err)
return
}
if assignmentCount > 0 {
// count the steps in the old problem
var stepCount int
if err := tx.QueryRow(`SELECT COUNT(1) FROM problem_steps WHERE problem_id = $1`, bundle.Problem.ID).Scan(&stepCount); err != nil {
loggedHTTPErrorf(w, http.StatusInternalServerError, "db error: %v", err)
return
}
if len(bundle.ProblemSteps) != stepCount {
loggedHTTPErrorf(w, http.StatusBadRequest, "cannot change the number of steps in a problem that is already in use")
return
}
}
saveProblemBundleCommon(w, tx, &bundle, render)
}
示例3: doMoneyTest
func doMoneyTest(t *testing.T, curr string, amt string, expectedIso int16, expectedAmount int64) {
c := NewTestConfig(t)
ctx, err := NewContext(c)
if err != nil {
t.Fatal(err)
}
defer ctx.Close()
_, err = ctx.DB.Exec("CREATE TEMPORARY TABLE money_test (id BIGSERIAL PRIMARY KEY, cash monetary)")
if err != nil {
t.Fatal(err)
}
cash, err := NewMonetary(ctx.DB, curr, amt)
if err != nil {
if expectedIso == 0 && expectedAmount == 0 {
return
}
t.Fatal(err)
}
m := moneyTest{0, cash}
err = meddler.Insert(ctx.DB, "money_test", &m)
if err != nil {
t.Fatal(err)
}
m2 := moneyTest{}
err = meddler.Load(ctx.DB, "money_test", &m2, m.Id)
if err != nil {
t.Fatal(err)
}
if m2.Cash.Iso4217Code != expectedIso {
t.Fatal("ISO code incorrect")
}
if m2.Cash.Amount != expectedAmount {
t.Fatal("Amount incorrect")
}
}
示例4: GetCourse
// GetCourse handles /v2/courses/:course_id requests,
// returning a single course.
func GetCourse(w http.ResponseWriter, tx *sql.Tx, params martini.Params, currentUser *User, render render.Render) {
courseID, err := parseID(w, "course_id", params["course_id"])
if err != nil {
return
}
course := new(Course)
if currentUser.Admin {
err = meddler.Load(tx, "courses", course, courseID)
} else {
err = meddler.QueryRow(tx, course, `SELECT courses.* `+
`FROM courses JOIN assignments ON courses.id = assignments.course_id `+
`WHERE assignments.user_id = $1 AND assignments.course_id = $2`,
currentUser.ID, courseID)
}
if err != nil {
loggedHTTPDBNotFoundError(w, err)
return
}
render.JSON(http.StatusOK, course)
}
示例5: GetUser
// GetUser handles /v2/users/:user_id requests,
// returning a single user.
func GetUser(w http.ResponseWriter, tx *sql.Tx, params martini.Params, currentUser *User, render render.Render) {
userID, err := parseID(w, "user_id", params["user_id"])
if err != nil {
return
}
user := new(User)
if currentUser.Admin {
err = meddler.Load(tx, "users", user, int64(userID))
} else {
err = meddler.QueryRow(tx, &user, `SELECT users.* `+
`FROM users JOIN user_users ON users.id = user_users.other_user_id `+
`WHERE user_users.user_id = $1 AND user_users.other_user_id = $2`,
currentUser.ID, userID)
}
if err != nil {
loggedHTTPDBNotFoundError(w, err)
return
}
render.JSON(http.StatusOK, user)
}
示例6: GetProblem
// GetProblem handles a request to /v2/problems/:problem_id,
// returning a single problem.
func GetProblem(w http.ResponseWriter, tx *sql.Tx, params martini.Params, currentUser *User, render render.Render) {
problemID, err := parseID(w, "problem_id", params["problem_id"])
if err != nil {
return
}
problem := new(Problem)
if currentUser.Admin || currentUser.Author {
err = meddler.Load(tx, "problems", problem, problemID)
} else {
err = meddler.QueryRow(tx, problem, `SELECT problems.* `+
`FROM problems JOIN user_problems ON problems.id = problem_id `+
`WHERE user_id = $1 AND problem_id = $2`,
currentUser.ID, problemID)
}
if err != nil {
loggedHTTPDBNotFoundError(w, err)
return
}
render.JSON(http.StatusOK, problem)
}
示例7: PostProblemBundleUnconfirmed
// PostProblemBundleUnconfirmed handles a request to /v2/problem_bundles/unconfirmed,
// signing a new/updated problem that has not yet been tested on the daycare.
func PostProblemBundleUnconfirmed(w http.ResponseWriter, tx *sql.Tx, currentUser *User, bundle ProblemBundle, render render.Render) {
now := time.Now()
// basic sanity checks
if len(bundle.ProblemSteps) < 2 {
loggedHTTPErrorf(w, http.StatusBadRequest, "problem must have at least one step")
return
}
if len(bundle.ProblemSteps) != len(bundle.Commits) {
loggedHTTPErrorf(w, http.StatusBadRequest, "problem must have exactly one commit for each step")
return
}
if len(bundle.ProblemSignature) != 0 {
loggedHTTPErrorf(w, http.StatusBadRequest, "unconfirmed bundle must not have problem signature")
}
if len(bundle.CommitSignatures) != 0 {
loggedHTTPErrorf(w, http.StatusBadRequest, "unconfirmed bundle must not have commit signatures")
}
// clean up basic fields and do some checks
if err := bundle.Problem.Normalize(now, bundle.ProblemSteps); err != nil {
loggedHTTPErrorf(w, http.StatusBadRequest, "%v", err)
return
}
// if this is an update to an existing problem, we need to check that some things match
if bundle.Problem.ID != 0 {
old := new(Problem)
if err := meddler.Load(tx, "problems", old, int64(bundle.Problem.ID)); err != nil {
if err == sql.ErrNoRows {
loggedHTTPErrorf(w, http.StatusNotFound, "request to update problem %d, but that problem does not exist", bundle.Problem.ID)
} else {
loggedHTTPErrorf(w, http.StatusInternalServerError, "db error: %v", err)
}
return
}
if bundle.Problem.Unique != old.Unique {
loggedHTTPErrorf(w, http.StatusBadRequest, "updating a problem cannot change its unique ID from %q to %q; create a new problem instead", old.Unique, bundle.Problem.Unique)
return
}
if bundle.Problem.ProblemType != old.ProblemType {
loggedHTTPErrorf(w, http.StatusBadRequest, "updating a problem cannot change its type from %q to %q; create a new problem instead", old.ProblemType, bundle.Problem.ProblemType)
return
}
if !bundle.Problem.CreatedAt.Equal(old.CreatedAt) {
loggedHTTPErrorf(w, http.StatusBadRequest, "updating a problem cannot change its created time from %v to %v", old.CreatedAt, bundle.Problem.CreatedAt)
return
}
} else {
// for new problems, set the created timestamp to now
bundle.Problem.CreatedAt = now
}
// make sure the unique ID is unique
conflict := new(Problem)
if err := meddler.QueryRow(tx, conflict, `SELECT * FROM problems WHERE unique_id = $1`, bundle.Problem.Unique); err != nil {
if err == sql.ErrNoRows {
conflict.ID = 0
} else {
loggedHTTPErrorf(w, http.StatusInternalServerError, "db error: %v", err)
return
}
}
if conflict.ID != 0 && conflict.ID != bundle.Problem.ID {
loggedHTTPErrorf(w, http.StatusBadRequest, "unique ID %q is already in use by problem %d", bundle.Problem.Unique, conflict.ID)
return
}
// update the timestamp
bundle.Problem.UpdatedAt = now
// compute signature
bundle.ProblemSignature = bundle.Problem.ComputeSignature(Config.DaycareSecret, bundle.ProblemSteps)
// check the commits
whitelists := bundle.Problem.GetStepWhitelists(bundle.ProblemSteps)
bundle.CommitSignatures = nil
for n, commit := range bundle.Commits {
commit.ID = 0
commit.AssignmentID = 0
commit.ProblemID = bundle.Problem.ID
commit.Step = int64(n) + 1
if commit.Action != "confirm" {
loggedHTTPErrorf(w, http.StatusBadRequest, "commit %d has action %q, expected %q", n, commit.Action, "confirm")
return
}
commit.Transcript = []*EventMessage{}
commit.ReportCard = nil
commit.Score = 0.0
commit.CreatedAt = now
commit.UpdatedAt = now
if err := commit.Normalize(now, whitelists[n]); err != nil {
loggedHTTPErrorf(w, http.StatusBadRequest, "commit %d: %v", n, err)
return
}
//.........这里部分代码省略.........
示例8: Get
func (db *nodestore) Get(id int64) (*model.Node, error) {
var node = new(model.Node)
var err = meddler.Load(db, nodeTable, node, id)
return node, err
}
示例9: GetRepo
// GetRepo retrieves a specific repo from the
// datastore for the given ID.
func (db *Repostore) GetRepo(id int64) (*model.Repo, error) {
var repo = new(model.Repo)
var err = meddler.Load(db, repoTable, repo, id)
return repo, err
}
示例10: Get
func (db *userStore) Get(id int64) (*model.User, error) {
user := new(model.User)
err := meddler.Load(db, userTable, user, id)
return user, err
}
示例11: main
//.........这里部分代码省略.........
// rollback
log.Printf("rolling back transaction")
if err := tx.Rollback(); err != nil {
loggedHTTPErrorf(w, http.StatusInternalServerError, "db error rolling back transaction: %v", err)
return
}
}
}
// martini service: to require an active logged-in session
auth := func(w http.ResponseWriter, session sessions.Session) {
if userID := session.Get("id"); userID == nil {
loggedHTTPErrorf(w, http.StatusUnauthorized, "authentication: no user ID found in session")
return
}
}
// martini service: include the current logged-in user (requires withTx and auth)
withCurrentUser := func(c martini.Context, w http.ResponseWriter, tx *sql.Tx, session sessions.Session) {
rawID := session.Get("id")
if rawID == nil {
loggedHTTPErrorf(w, http.StatusInternalServerError, "cannot find user ID in session")
return
}
userID, ok := rawID.(int64)
if !ok {
session.Clear()
loggedHTTPErrorf(w, http.StatusInternalServerError, "error extracting user ID from session")
return
}
// load the user record
user := new(User)
if err := meddler.Load(tx, "users", user, userID); err != nil {
if err == sql.ErrNoRows {
loggedHTTPErrorf(w, http.StatusUnauthorized, "user %d not found", userID)
return
}
loggedHTTPErrorf(w, http.StatusInternalServerError, "db error: %v", err)
return
}
// map the current user to the request context
c.Map(user)
}
// martini service: require logged in user to be an administrator (requires withCurrentUser)
administratorOnly := func(w http.ResponseWriter, currentUser *User) {
if !currentUser.Admin {
loggedHTTPErrorf(w, http.StatusUnauthorized, "user %d (%s) is not an administrator", currentUser.ID, currentUser.Email)
return
}
}
// martini service: require logged in user to be an author or administrator (requires withCurrentUser)
authorOnly := func(w http.ResponseWriter, tx *sql.Tx, currentUser *User) {
if currentUser.Admin {
return
}
if !currentUser.Author {
loggedHTTPErrorf(w, http.StatusUnauthorized, "user %d (%s) is not an author", currentUser.ID, currentUser.Name)
return
}
}
// version
示例12: GetExpenseById
func GetExpenseById(db meddler.DB, id int64) (*Expense, error) {
expense := new(Expense)
err := meddler.Load(db, "expenses", expense, id)
return expense, err
}
示例13: GetTransactionById
func GetTransactionById(db meddler.DB, id int64) (*Transaction, error) {
trans := new(Transaction)
err := meddler.Load(db, "transactions", trans, id)
return trans, err
}
示例14: GetCommit
// GetCommit retrieves a commit from the
// datastore for the given ID.
func (db *Commitstore) GetCommit(id int64) (*model.Commit, error) {
var commit = new(model.Commit)
var err = meddler.Load(db, commitTable, commit, id)
return commit, err
}
示例15: GetUserById
func GetUserById(db meddler.DB, id int64) (*User, error) {
user := new(User)
err := meddler.Load(db, "users", user, id)
return user, err
}