Exemplo n.º 1
0
func DeleteClarification(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || (me.Level != data.Judge && me.Level != data.Administrator) {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	clar, err := data.GetClarification(id)
	catch(err)

	err = clar.Del()
	catch(err)

	json.NewEncoder(w).Encode(&struct {
		Id int64 `json:"id"`
	}{
		Id: clar.Id,
	})
	hub.Send([]interface{}{"SYNC", "clarifications"})

	err = data.NewActivity(me, fmt.Sprintf("deleted clarification %d", clar.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "activities"})
}
Exemplo n.º 2
0
func UpdateClarification(w http.ResponseWriter, r *http.Request) {
	me, _ := context.Get(r, "me").(*data.Account)
	if me == nil || (me.Level != data.Judge && me.Level != data.Administrator) {
		http.Error(w, "", http.StatusForbidden)
		return
	}

	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	clar, err := data.GetClarification(id)
	catch(err)

	body := struct {
		ProblemId int64         `json:"problemId"`
		Question  string        `json:"question"`
		Response  data.Response `json:"response"`
		Message   string        `json:"message"`
	}{}
	err = json.NewDecoder(r.Body).Decode(&body)
	catch(err)

	prob, err := data.GetProblem(body.ProblemId)
	catch(err)
	if prob != nil {
		clar.ProblemId = prob.Id
	} else {
		clar.ProblemId = 0
	}

	clar.Question = body.Question
	clar.Response = body.Response
	clar.Message = body.Message

	err = clar.Put()
	catch(err)

	err = json.NewEncoder(w).Encode(clar)
	catch(err)
	hub.Send([]interface{}{"SYNC", "clarifications", clar.Id})

	err = data.NewActivity(me, fmt.Sprintf("updated clarification %d", clar.Id)).Put()
	catch(err)
	hub.Send([]interface{}{"SYNC", "activities"})

	if clar.Response != data.Unresponded {
		if clar.Response == data.Broadcasted {
			err = data.NewNotification(0, data.Participant, fmt.Sprintf("Clarification %d updated", clar.Id)).Put()
			catch(err)
		} else {
			err = data.NewNotification(clar.AskerId, 0, fmt.Sprintf("Clarification %d updated", clar.Id)).Put()
			catch(err)
		}
		hub.Send([]interface{}{"SYNC", "notifications"})
	}
}
Exemplo n.º 3
0
func ServeClarification(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 10, 64)
	catch(err)
	clar, err := data.GetClarification(id)
	catch(err)

	err = json.NewEncoder(w).Encode(clar)
	catch(err)
}