Example #1
0
func (h *Handler) apiJSONputPowerOrders(w http.ResponseWriter, r *http.Request) {
	item := &models.PowerOrder{}
	err := jsend.Read(r, &item)
	if err != nil {
		JSONUserError(w, "Cannot read json into powerorder data")
		return
	}
	_, ok, err := h.Validate(item.GID, item.FID)
	if my, bad := Check(err, "API PUT failure on resource validation", "type", "powerorder", "GID", item.GID, "FID", item.FID); bad {
		JSONServerError(w, my)
		return
	}
	if !ok {
		JSONUserError(w, "You are not authorized for that faction")
		return
	}
	errS, errU := InternalSetPowerOrder(item.GID, item.FID, item.UpPower, item.Loc)
	if my, bad := Check(errS, "API JSON PUT powerorder failure on command execution", "item", item); bad {
		JSONServerError(w, my)
		return
	}
	if errU != nil {
		JSONUserError(w, errU.Error())
		return
	}
	JSONSuccess(w, nil)
}
Example #2
0
func (h *Handler) apiJSONputTruces(w http.ResponseWriter, r *http.Request) {
	item := &TruceCommand{}
	err := jsend.Read(r, &item)
	if err != nil {
		JSONUserError(w, "Cannot read json into trucecommand data")
		return
	}
	facs, err := h.M.Faction().SelectWhere(h.M.GID(item.GID))
	if my, bad := Check(err, "API PUT failure on resource validation", "type", "truce", "GID", item.GID); bad {
		JSONServerError(w, my)
		return
	}
	var authID int
	facMap := make(map[int]bool, len(facs))
	for _, f := range facs {
		if h.LoggedIn && f.Owner() == h.User.String() {
			authID = f.FID()
		}
		facMap[f.FID()] = true
	}
	if authID == 0 {
		JSONUserError(w, "You are not authorized for that faction")
		return
	}
	for _, fid := range item.Trucees {
		if !facMap[fid] {
			JSONUserError(w, "FACTION ID NOT FOUND", KV{"fid", fid})
			return
		}
		if fid == authID {
			JSONUserError(w, "CANNOT FORM TRUCE WITH SELF")
			return
		}
	}
	errS, errU := InternalSetTruce(item)
	if my, bad := Check(errS, "API JSON PUT truce failure on command execution", "item", item); bad {
		JSONServerError(w, my)
		return
	}
	if errU != nil {
		JSONUserError(w, errU.Error())
		return
	}
	JSONSuccess(w, nil)
}