示例#1
0
// POST /rooms
func (this Room) Post() {

	_id := string(this.FormValue("id"))
	_name := string(this.FormValue("name"))
	_price := string(this.FormValue("price"))

	room := model.Room{}
	room.Id = _id
	room.Name = _name

	if priceToInt, err := strconv.Atoi(_price); err != nil {
		this.JSON(iris.StatusOK, model.Err("4"))
		return
	} else {
		room.Price = priceToInt
	}

	Db := db.MgoDb{}
	Db.Init()

	// Insert
	if err := Db.C("room").Insert(&room); err != nil {
		if Db.IsDup(err) {
			this.JSON(iris.StatusOK, model.Err("6"))
		} else {
			this.JSON(iris.StatusOK, model.Err("5"))
		}
	} else {
		this.JSON(iris.StatusOK, iris.Map{"response": true})
	}

	Db.Close()

}
示例#2
0
// GET /rooms
func (this Room) Get() {

	Db := db.MgoDb{}
	Db.Init()

	result := []model.Room{}
	if err := Db.C("room").Find(nil).All(&result); err != nil {
		this.JSON(iris.StatusOK, model.Err("1"))
		return
	} else {
		this.JSON(iris.StatusOK, &result)
	}

	Db.Close()

}
示例#3
0
// GET /rooms/{id}
func (this Room) GetBy(id string) {

	Db := db.MgoDb{}
	Db.Init()

	result := model.Room{}

	if err := Db.C("room").Find(bson.M{"id": id}).One(&result); err != nil {
		this.JSON(iris.StatusOK, model.Err("1"))
		return
	} else {
		this.JSON(iris.StatusOK, &result)
	}

	Db.Close()

}