Exemplo n.º 1
0
func getUsers(p *passenger.Passenger, ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
	if p.UserKey.Parent() == nil {
		return http.StatusUnauthorized, nil
	}
	var invitations model.Invitations
	_, err = model.NewQueryForInvitation().Ancestor(p.UserKey.Parent()).GetAll(ctx, &invitations)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	cckeys, err := model.NewQueryForChallenge().Ancestor(p.UserKey.Parent()).KeysOnly().GetAll(ctx, nil)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	var resultKeys []*datastore.Key
	for _, val := range cckeys {
		rkeys, err := model.NewQueryForResult().Filter("Challenge =", val).KeysOnly().GetAll(ctx, nil)
		if err != nil {
			return http.StatusInternalServerError, err
		}
		resultKeys = append(resultKeys, rkeys...)
	}

	var users model.Users
	keys, err := model.NewQueryForUser().GetAll(ctx, &users)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	finishedUsers := make([]*datastore.Key, len(resultKeys))
	for i := range resultKeys {
		finishedUsers[i] = resultKeys[i].Parent().Parent()
	}

	// TODO(victorbalan): Don`t load invited users that have an result.
	invitedUsers := make([]*datastore.Key, len(invitations))
	for i, val := range invitations {
		invitedUsers[i] = val.User
	}
	mappedStates := make(map[string]string)
	for _, val := range invitedUsers {
		mappedStates[val.Encode()] = "invited"
	}
	for _, val := range finishedUsers {
		mappedStates[val.Encode()] = "coding"
	}

	usersWithState := make([]keyedUserWithState, len(users))
	for i := range users {
		usersWithState[i] = keyedUserWithState{
			KeyedUser: &model.KeyedUser{
				User: &users[i], Key: keys[i],
			},
			State: mappedStates[keys[i].Encode()],
		}
	}
	json.NewEncoder(w).Encode(usersWithState)
	return http.StatusOK, nil
}
Exemplo n.º 2
0
func GetChallengesForProfile(ctx context.Context, w http.ResponseWriter, r *http.Request) (status int, err error) {
	if r.Method != "GET" {
		return http.StatusMethodNotAllowed, nil
	}

	_, ok := passenger.FromContext(ctx)
	if !ok {
		return http.StatusUnauthorized, nil
	}

	var profileKey *datastore.Key
	if profileKey, err = datastore.DecodeKey(mux.Vars(r)["key"]); err != nil {
		return http.StatusInternalServerError, err
	}

	q := model.NewQueryForResult().
		Ancestor(profileKey)
	if finished := r.URL.Query()["finished"]; len(finished) > 0 && finished[0] == "true" {
		q = q.Filter("Finished >", time.Time{})
	}
	if order := r.URL.Query()["order"]; len(order) > 0 && order[0] != "" {
		q = q.Order(order[0])
	}

	if limitQuery := r.URL.Query()["limit"]; len(limitQuery) > 0 {
		if limit, err := strconv.Atoi(limitQuery[0]); err != nil {
			return http.StatusInternalServerError, err
		} else {
			q = q.Limit(limit)
		}
	}

	var results model.Results
	if _, err = q.GetAll(ctx, &results); err != nil {
		return http.StatusInternalServerError, err
	}

	challengeKeys := make([]*datastore.Key, len(results))
	for i, val := range results {
		challengeKeys[i] = val.Challenge
	}

	challenges := make(model.Challenges, len(challengeKeys))
	if err = datastore.GetMulti(ctx, challengeKeys, challenges); err != nil {
		return http.StatusInternalServerError, err
	}

	json.NewEncoder(w).Encode(challenges.Key(challengeKeys))
	return
}
Exemplo n.º 3
0
func GetResultForUserChallenge(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) {
	if r.Method != "GET" {
		return http.StatusMethodNotAllowed, nil
	}
	userKey, err := datastore.DecodeKey(mux.Vars(r)["userKey"])
	if err != nil {
		return http.StatusBadRequest, err
	}
	challengeKey, err := datastore.DecodeKey(mux.Vars(r)["challengeKey"])
	if err != nil {
		return http.StatusBadRequest, err
	}

	keys, err := model.NewQueryForProfile().
		Ancestor(userKey).
		Limit(1).
		KeysOnly().
		GetAll(ctx, nil)
	if err != nil {
		return http.StatusInternalServerError, err
	}
	if len(keys) != 1 {
		return http.StatusNotFound, nil
	}

	var results model.Results
	resultKeys, err := model.NewQueryForResult().
		Filter("Challenge =", challengeKey).
		Ancestor(keys[0]).
		Limit(1).
		GetAll(ctx, &results)

	if err != nil {
		return http.StatusInternalServerError, err
	}
	if len(resultKeys) != 1 {
		return http.StatusNotFound, nil
	}
	json.NewEncoder(w).Encode(results[0].Key(resultKeys[0]))
	return http.StatusOK, nil
}
Exemplo n.º 4
0
// GetResultsByChallenge queries the results for a certain challenge to be reviewed by a company.
func GetResultsByChallenge(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) {
	if r.Method != "GET" {
		return http.StatusMethodNotAllowed, nil
	}

	key, err := datastore.DecodeKey(mux.Vars(r)["key"])
	if err != nil {
		return http.StatusBadRequest, err
	}

	var results model.Results
	keys, err := model.NewQueryForResult().
		Filter("Challenge =", key).
		GetAll(ctx, &results)

	if err != nil {
		return http.StatusInternalServerError, err
	}

	json.NewEncoder(w).Encode(results.Key(keys))
	return http.StatusOK, nil
}
Exemplo n.º 5
0
// CreateResult saves a new result when a coder starts a challenge.
func CreateResult(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, error) {
	if r.Method != "POST" {
		return http.StatusMethodNotAllowed, nil
	}

	var body = struct {
		ChallengeKey string
	}{}

	p, ok := passenger.FromContext(ctx)

	if !ok {
		return http.StatusUnauthorized, nil
	}

	var profiles model.Profiles
	keys, err := model.NewQueryForProfile().
		Ancestor(p.User).
		GetAll(ctx, &profiles)

	if len(keys) != 1 {
		return http.StatusInternalServerError, errors.New("Profile not found")
	}

	err = json.NewDecoder(r.Body).Decode(&body)

	if err != nil {
		return http.StatusBadRequest, err
	}

	key, err := datastore.DecodeKey(body.ChallengeKey)

	if err != nil {
		return http.StatusBadRequest, err
	}

	var results []model.Result

	resultKeys, err := model.NewQueryForResult().
		Ancestor(keys[0]).
		Filter("Challenge = ", key).
		Limit(1).
		GetAll(ctx, &results)

	if err != nil {
		return http.StatusInternalServerError, err
	}

	if len(resultKeys) == 1 {
		json.NewEncoder(w).Encode(results[0].Key(resultKeys[0]))
		return http.StatusOK, nil
	}

	var challenge model.Challenge
	if err = datastore.Get(ctx, key, &challenge); err != nil {
		return http.StatusInternalServerError, err
	}

	result := model.Result{
		Challenge:        key,
		StartTimes:       make([]time.Time, len(challenge.Tasks)),
		FinalSubmissions: make([]*datastore.Key, len(challenge.Tasks)),
		Started:          time.Now(),
	}
	key, err = result.PutWithParent(ctx, keys[0])
	if err != nil {
		return http.StatusInternalServerError, err
	}

	json.NewEncoder(w).Encode(result.Key(key))
	return http.StatusOK, nil
}