本文整理匯總了Golang中github.com/coduno/api/model.Result.Put方法的典型用法代碼示例。如果您正苦於以下問題:Golang Result.Put方法的具體用法?Golang Result.Put怎麽用?Golang Result.Put使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/coduno/api/model.Result
的用法示例。
在下文中一共展示了Result.Put方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createFinalResult
func createFinalResult(ctx context.Context, w http.ResponseWriter, resultKey *datastore.Key, result model.Result) (int, error) {
var challenge model.Challenge
if err := datastore.Get(ctx, result.Challenge, &challenge); err != nil {
return http.StatusInternalServerError, nil
}
result.Finished = time.Now()
for i, taskKey := range challenge.Tasks {
key, err := getLatestSubmissionKey(ctx, resultKey, taskKey)
if err != nil {
return http.StatusInternalServerError, err
}
result.FinalSubmissions[i] = key
}
key, err := result.Put(ctx, resultKey)
if err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(result.Key(key))
go computeFinalScore(ctx, challenge, result, resultKey)
return http.StatusOK, nil
}
示例2: FinalSubmission
// FinalSubmission makes the last submission final.
func FinalSubmission(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
if r.Method != "POST" {
return http.StatusMethodNotAllowed, nil
}
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
var resultKey *datastore.Key
if resultKey, err = datastore.DecodeKey(mux.Vars(r)["resultKey"]); err != nil {
return http.StatusInternalServerError, err
}
if !util.HasParent(p.User, resultKey) {
return http.StatusBadRequest, errors.New("cannot submit answer for other users")
}
var index int
if index, err = strconv.Atoi(mux.Vars(r)["index"]); err != nil {
return http.StatusInternalServerError, err
}
if len(r.URL.Query()["submissionKey"]) == 0 {
return http.StatusOK, nil
}
var submissionKey *datastore.Key
if submissionKey, err = datastore.DecodeKey(r.URL.Query()["submissionKey"][0]); err != nil {
return http.StatusInternalServerError, err
}
var result model.Result
if err = datastore.Get(ctx, resultKey, &result); err != nil {
return http.StatusInternalServerError, err
}
result.FinalSubmissions[index] = submissionKey
if _, err = result.Put(ctx, resultKey); err != nil {
return http.StatusInternalServerError, err
}
w.Write([]byte("OK"))
return
}
示例3: init
func init() {
RegisterResulter(Average, func(ctx context.Context, resultKey *datastore.Key) error {
var result model.Result
if err := datastore.Get(ctx, resultKey, &result); err != nil {
return err
}
var challenge model.Challenge
if err := datastore.Get(ctx, result.Challenge, &challenge); err != nil {
return err
}
var tasks model.Tasks
if err := datastore.GetMulti(ctx, challenge.Tasks, &tasks); err != nil {
return err
}
weightSum := model.Skills{} // this could be SkillWeights, but would need more conversions
average := model.Skills{}
for i, task := range tasks {
taskResult, err := Tasker(task.Tasker).Call(ctx, challenge.Tasks[i], resultKey.Parent().Parent())
if err != nil {
return err
}
average = average.Add(taskResult.Mul(model.Skills(task.SkillWeights)))
weightSum = weightSum.Add(model.Skills(task.SkillWeights))
}
result.Skills = average.Div(weightSum)
result.Computed = time.Now()
_, err := result.Put(ctx, resultKey)
return err
})
}
示例4: TaskByKey
// TaskByKey loads a task by key.
func TaskByKey(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
p, ok := passenger.FromContext(ctx)
if !ok {
return http.StatusUnauthorized, nil
}
if r.Method != "GET" {
return http.StatusMethodNotAllowed, nil
}
taskKey, err := datastore.DecodeKey(mux.Vars(r)["key"])
if err != nil {
return http.StatusInternalServerError, err
}
var u model.User
if err = datastore.Get(ctx, p.User, &u); err != nil {
return http.StatusInternalServerError, nil
}
if len(r.URL.Query()["result"]) > 0 {
rk, err := datastore.DecodeKey(r.URL.Query()["result"][0])
if err != nil {
return http.StatusInternalServerError, err
}
if util.HasParent(p.User, rk) {
var result model.Result
if err = datastore.Get(ctx, rk, &result); err != nil {
return http.StatusInternalServerError, err
}
var challenge model.Challenge
if err = datastore.Get(ctx, result.Challenge, &challenge); err != nil {
return http.StatusInternalServerError, err
}
emptyTime := time.Time{}
updateResult := false
for i, val := range challenge.Tasks {
if taskKey.Equal(val) {
if result.StartTimes[i].Equal(emptyTime) {
result.StartTimes[i] = time.Now()
updateResult = true
break
}
}
}
if updateResult {
if _, err = result.Put(ctx, rk); err != nil {
return http.StatusInternalServerError, err
}
}
}
}
var task model.Task
if err = datastore.Get(ctx, taskKey, &task); err != nil {
return http.StatusInternalServerError, err
}
json.NewEncoder(w).Encode(task.Key(taskKey))
return http.StatusOK, nil
}