Esempio n. 1
0
func (self *Orm) Update() *api.Err {
	errors := api.NewError()

	data, _ := util.ToMap(self.Model, "json")

	pkValue := self.getPkValue()

	err := Collection.Update(
		bson.M{
			self.PkBson: pkValue,
		},
		bson.M{
			"$set": data,
		},
	)

	if err != nil {
		errors.SetErr(
			err.Error(),
			api.ErrMongoUpdate,
			19,
		)
	}
	return errors
}
Esempio n. 2
0
func (self *Orm) Validate() *api.Err {
	errors := api.NewError()
	valid := validation.Validation{}
	b, _ := valid.Valid(self.Model)
	if !b {
		for _, err := range valid.Errors {
			errors.SetErr(err.Key, err.Message, 0)
		}
	}
	return errors
}
Esempio n. 3
0
func (self *Orm) FindByCondition(model interface{}, cond interface{}) *api.Err {
	errors := api.NewError()
	err := Collection.Find(cond).All(model)
	if err != nil {
		errors.SetErr(
			err.Error(),
			api.ErrMongoSelectAll,
			17,
		)
	}
	return errors
}
Esempio n. 4
0
func (self *Orm) UpdateAllByConditions(a, b bson.M) *api.Err {
	errors := api.NewError()
	_, err := Collection.UpdateAll(a, b)
	if err != nil {
		errors.SetErr(
			err.Error(),
			api.ErrMongoUpdate,
			19,
		)
	}
	return errors
}
Esempio n. 5
0
func (self *Orm) Save() *api.Err {
	errors := api.NewError()
	err := Collection.Insert(self.Model)
	if err != nil {
		errors.SetErr(
			err.Error(),
			api.ErrMongoInsert,
			18,
		)
	}
	return errors
}
Esempio n. 6
0
func (self *Orm) SaveAll(data []interface{}) *api.Err {
	errors := api.NewError()
	x := Collection.Bulk()
	x.Unordered() //magic! :)
	x.Insert(data...)
	_, err := x.Run()
	if err != nil {
		errors.SetErr(
			err.Error(),
			api.ErrMongoInsert,
			18,
		)
	}
	return errors
}
Esempio n. 7
0
// DecodeCreateIDRequest creates a decoder that assings an ID to the returned interface
// for a POST request
func DecodeCreateIDRequest(req IDSetter, field string) func(context.Context, *http.Request) (interface{}, error) {
	return func(_ context.Context, r *http.Request) (interface{}, error) {
		if r.ContentLength <= 0 && len(r.URL.Query().Get(field)) == 0 {
			return req, nil
		}

		if err := json.NewDecoder(r.Body).Decode(req); err != nil {
			jserr := api.NewError()
			jserr.Push(api.Msg{Error: err.Error()})
			return nil, jserr
		}

		return req.SetID(r.URL.Query().Get(field)), nil
	}
}
Esempio n. 8
0
func (self *Orm) FindById(id string) (interface{}, *api.Err) {
	errors := api.NewError()
	err := Collection.Find(
		bson.M{
			self.PkBson: id,
		},
	).One(self.Model)
	if err != nil {
		errors.SetErr(
			err.Error(),
			api.ErrMongoSelectAll,
			17,
		)
	}
	return self.Model, errors
}
Esempio n. 9
0
func (self *Orm) Delete() *api.Err {
	errors := api.NewError()
	id := self.getPkValue()
	err := Collection.Remove(
		bson.M{
			self.PkBson: id,
		},
	)
	if err != nil {
		errors.SetErr(
			err.Error(),
			api.ErrMongoDelete,
			20,
		)
	}
	return errors
}