// CreateChallenge will put a new entity of kind Challenge to Datastore. func CreateChallenge(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 u model.User if err := datastore.Get(ctx, p.User, &u); err != nil { return http.StatusInternalServerError, err } if u.Company == nil { return http.StatusUnauthorized, nil } var body = struct { model.Assignment Tasks []string }{} if err = json.NewDecoder(r.Body).Decode(&body); err != nil { return http.StatusBadRequest, err } keys := make([]*datastore.Key, len(body.Tasks)) for i := range body.Tasks { key, err := datastore.DecodeKey(body.Tasks[i]) if err != nil { return http.StatusInternalServerError, err } keys[i] = key } challenge := model.Challenge{ Assignment: body.Assignment, Resulter: int64(logic.Average), Tasks: keys, } key, err := challenge.PutWithParent(ctx, u.Company) if err != nil { return http.StatusInternalServerError, err } json.NewEncoder(w).Encode(challenge.Key(key)) return http.StatusOK, nil }
func CreateAndSaveChallenge(t *testing.T, ctx context.Context, company *datastore.Key) (model.Challenge, *datastore.Key) { assignment := model.Assignment{ Name: "Challenge Name", Description: "Challenge Description", Instructions: "Challenge Instructions", Duration: time.Hour, Endpoints: model.Endpoints{ WebInterface: "sequential-challenge", }, } tasks := []*datastore.Key{TaskKey} challenge := model.Challenge{ Assignment: assignment, Tasks: tasks, } var err error var key *datastore.Key if key, err = challenge.PutWithParent(ctx, company); err != nil { t.Fatal(err) } return challenge, key }