func (p *HostService) UpdateById(objectId string, host entity.Host, x_auth_token string) (created bool, errorCode string, err error) { logrus.Infof("start to update host [%v]", host) // do authorize first if authorized := GetAuthService().Authorize("update_host", x_auth_token, objectId, p.collectionName); !authorized { err = errors.New("required opertion is not authorized!") errorCode = COMMON_ERROR_UNAUTHORIZED logrus.Errorf("update host with objectId [%v] error is %v", objectId, err) return } if !bson.IsObjectIdHex(objectId) { err = errors.New("invalide ObjectId.") errorCode = COMMON_ERROR_INVALIDATE return } // FIXING // hostquery, _, _ := p.QueryById(objectId, x_auth_token) var selector = bson.M{} selector["_id"] = bson.ObjectIdHex(objectId) host.ObjectId = bson.ObjectIdHex(objectId) host.TimeUpdate = dao.GetCurrentTime() logrus.Infof("start to change host") err = dao.HandleUpdateByQueryPartial(p.collectionName, selector, &host) // created, err = dao.HandleUpdateOne(&host, dao.QueryStruct{p.collectionName, selector, 0, 0, ""}) if err != nil { logrus.Errorf("update host [%v] error is %v", host, err) errorCode = HOST_ERROR_UPDATE } created = true return }
func (p *HostService) Create(host entity.Host, x_auth_token string) (newHost entity.Host, errorCode string, err error) { logrus.Infof("start to create host [%v]", host) // do authorize first if authorized := GetAuthService().Authorize("create_host", x_auth_token, "", p.collectionName); !authorized { err = errors.New("required opertion is not authorized!") errorCode = COMMON_ERROR_UNAUTHORIZED logrus.Errorf("create host [%v] error is %v", host, err) return } // generate ObjectId host.ObjectId = bson.NewObjectId() token, err := GetTokenById(x_auth_token) if err != nil { errorCode = HOST_ERROR_CREATE logrus.Errorf("get token failed when create host [%v], error is %v", host, err) return } // set token_id and user_id from token host.TenantId = token.Tenant.Id host.UserId = token.User.Id // set created_time and updated_time host.TimeCreate = dao.GetCurrentTime() host.TimeUpdate = host.TimeCreate // insert bson to mongodb err = dao.HandleInsert(p.collectionName, host) if err != nil { errorCode = HOST_ERROR_CREATE logrus.Errorf("insert host [%v] to db error is %v", host, err) return } newHost = host return }