// ChallengeByKey loads a challenge by key. func ChallengeByKey(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) { if r.Method != "GET" { return http.StatusMethodNotAllowed, nil } p, ok := passenger.FromContext(ctx) if !ok { return http.StatusUnauthorized, nil } key, err := datastore.DecodeKey(mux.Vars(r)["key"]) if err != nil { return http.StatusInternalServerError, err } var challenge model.Challenge err = datastore.Get(ctx, key, &challenge) if err != nil { return http.StatusInternalServerError, err } e := json.NewEncoder(w) if parent := p.UserKey.Parent(); parent == nil { // The current user is a coder so we must also create a result. e.Encode(challenge.Key(key)) } else { // TODO(pbochis): If a company representativemakes the request // we also include Tasks in the response. e.Encode(challenge.Key(key)) } return http.StatusOK, nil }
// 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 }