Exemplo n.º 1
0
func CreateAbility(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	var newAbility models.NewAbility

	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048676))

	if err != nil {
		panic(err)
	}

	if err := r.Body.Close(); err != nil {
		panic(err)
	}

	if err := json.Unmarshal(body, &newAbility); err != nil {
		message := models.APIMessage{"Input format invalid"}
		json.NewEncoder(w).Encode(message)
	} else {
		var ability models.Ability
		ability.Name = newAbility.Name

		database.DB.Create(&ability)

		if database.DB.NewRecord(ability) {
			message := models.APIMessage{"Error creating ability"}

			json.NewEncoder(w).Encode(message)
		} else {
			json.NewEncoder(w).Encode(ability)
		}
	}
}
Exemplo n.º 2
0
func UpdateAbility(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")

	urlParams := mux.Vars(r)
	strID := urlParams["id"]
	ability := models.Ability{}
	var newAbility models.NewAbility

	id, err := strconv.Atoi(strID)
	if err != nil {
		// handle error
		message := models.APIMessage{"Id format invalid"}
		json.NewEncoder(w).Encode(message)
	} else {

		database.DB.First(&ability, id)

		if ability.ID == 0 {
			message := models.APIMessage{"Ability not found"}
			json.NewEncoder(w).Encode(message)
		} else {

			body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048676))

			if err != nil {
				panic(err)
			}

			if err := r.Body.Close(); err != nil {
				panic(err)
			}

			if err := json.Unmarshal(body, &newAbility); err != nil {
				message := models.APIMessage{"Input format invalid"}
				json.NewEncoder(w).Encode(message)
			} else {
				ability.Name = newAbility.Name

				database.DB.Save(&ability)

				json.NewEncoder(w).Encode(ability)
			}
		}
	}
}