func (r *GormWorkItemRepository) Save(ctx context.Context, spaceID uuid.UUID, wi WorkItem, modifierID uuid.UUID) (*WorkItem, error) {
	res := WorkItemStorage{}
	id, err := strconv.ParseUint(wi.ID, 10, 64)
	if err != nil || id == 0 {
		return nil, errors.NewNotFoundError("work item", wi.ID)
	}

	log.Info(ctx, map[string]interface{}{
		"wi_id":    wi.ID,
		"space_id": spaceID,
	}, "Looking for id for the work item repository")
	tx := r.db.Model(&res).Where("id=? AND space_id=?", id, spaceID).First(&res)
	if tx.RecordNotFound() {
		log.Error(ctx, map[string]interface{}{
			"wi_id":    wi.ID,
			"space_id": spaceID,
		}, "work item repository not found")
		return nil, errors.NewNotFoundError("work item", wi.ID)
	}
	if tx.Error != nil {
		return nil, errors.NewInternalError(err.Error())
	}
	if res.Version != wi.Version {
		return nil, errors.NewVersionConflictError("version conflict")
	}

	wiType, err := r.witr.LoadTypeFromDB(ctx, wi.Type)
	if err != nil {
		return nil, errors.NewBadParameterError("Type", wi.Type)
	}

	res.Version = res.Version + 1
	res.Type = wi.Type
	res.Fields = Fields{}
	res.ExecutionOrder = wi.Fields[SystemOrder].(float64)
	for fieldName, fieldDef := range wiType.Fields {
		if fieldName == SystemCreatedAt || fieldName == SystemUpdatedAt || fieldName == SystemOrder {
			continue
		}
		fieldValue := wi.Fields[fieldName]
		var err error
		res.Fields[fieldName], err = fieldDef.ConvertToModel(fieldName, fieldValue)
		if err != nil {
			return nil, errors.NewBadParameterError(fieldName, fieldValue)
		}
	}

	tx = tx.Where("Version = ?", wi.Version).Save(&res)
	if err := tx.Error; err != nil {
		log.Error(ctx, map[string]interface{}{
			"wi_id":    wi.ID,
			"space_id": spaceID,
			"err":      err,
		}, "unable to save the work item repository")
		return nil, errors.NewInternalError(err.Error())
	}
	if tx.RowsAffected == 0 {
		return nil, errors.NewVersionConflictError("version conflict")
	}
	// store a revision of the modified work item
	err = r.wirr.Create(context.Background(), modifierID, RevisionTypeUpdate, res)
	if err != nil {
		return nil, errs.Wrapf(err, "error while saving work item")
	}
	log.Info(ctx, map[string]interface{}{
		"wi_id":    wi.ID,
		"space_id": spaceID,
	}, "Updated work item repository")
	return ConvertWorkItemStorageToModel(wiType, &res)
}
// Save updates the given work item in storage. Version must be the same as the one int the stored version
// returns NotFoundError, VersionConflictError, ConversionError or InternalError
Save updates the given work item in storage. Version must be the same as the one int the stored version
returns NotFoundError, VersionConflictError, ConversionError or InternalError
// Save updates the given work item in storage . Version must be the same as the one int the stored version // returns NotFoundError VersionConflictError ConversionError or InternalError
func (r *GormWorkItemRepository) Save(ctx context.Context, spaceID uuid.UUID, wi WorkItem, modifierID uuid.UUID) (*WorkItem, error) {
	res := WorkItemStorage{}
	id, err := strconv.ParseUint(wi.ID, 10, 64)
	if err != nil || id == 0 {
		return nil, errors.NewNotFoundError("work item", wi.ID)
	}
	log.Info(ctx, map[string]interface{}{
		"wi_id":    wi.ID,
		"space_id": spaceID,
	}, "Looking for id for the work item repository")
	tx := r.db.Model(&res).Where("id=? AND space_id=?", id, spaceID).First(&res)
	if tx.RecordNotFound() {
		log.Error(ctx, map[string]interface{}{
			"wi_id":    wi.ID,
			"space_id": spaceID,
		}, "work item repository not found")
		return nil, errors.NewNotFoundError("work item", wi.ID)
	}
	if tx.Error != nil {
		return nil, errors.NewInternalError(err.Error())
	}
	if res.Version != wi.Version {
		return nil, errors.NewVersionConflictError("version conflict")
	}
	wiType, err := r.witr.LoadTypeFromDB(ctx, wi.Type)
	if err != nil {
		return nil, errors.NewBadParameterError("Type", wi.Type)
	}
	res.Version = res.Version + 1
	res.Type = wi.Type
	res.Fields = Fields{}
	res.ExecutionOrder = wi.Fields[SystemOrder].(float64)
	for fieldName, fieldDef := range wiType.Fields {
		if fieldName == SystemCreatedAt || fieldName == SystemUpdatedAt || fieldName == SystemOrder {
			continue
		}
		fieldValue := wi.Fields[fieldName]
		var err error
		res.Fields[fieldName], err = fieldDef.ConvertToModel(fieldName, fieldValue)
		if err != nil {
			return nil, errors.NewBadParameterError(fieldName, fieldValue)
		}
	}
	tx = tx.Where("Version = ?", wi.Version).Save(&res)
	if err := tx.Error; err != nil {
		log.Error(ctx, map[string]interface{}{
			"wi_id":    wi.ID,
			"space_id": spaceID,
			"err":      err,
		}, "unable to save the work item repository")
		return nil, errors.NewInternalError(err.Error())
	}
	if tx.RowsAffected == 0 {
		return nil, errors.NewVersionConflictError("version conflict")
	}
	err = r.wirr.Create(context.Background(), modifierID, RevisionTypeUpdate, res)
	if err != nil {
		return nil, errs.Wrapf(err, "error while saving work item")
	}
	log.Info(ctx, map[string]interface{}{
		"wi_id":    wi.ID,
		"space_id": spaceID,
	}, "Updated work item repository")
	return ConvertWorkItemStorageToModel(wiType, &res)
}
func ( r * GormWorkItemRepository ) Save ( ctx context . Context , spaceID uuid . UUID , wi WorkItem , modifierID uuid . UUID ) ( * WorkItem , error ) { res := WorkItemStorage { } id , err := strconv . ParseUint ( wi . ID , 10 , 64 ) if err != nil || id == 0 { return nil , errors . NewNotFoundError ( " " , wi . ID ) } log . Info ( ctx , map [ string ] interface { } { " " : wi . ID , " " : spaceID , } , " " ) tx := r . db . Model ( & res ) . Where ( " " , id , spaceID ) . First ( & res ) if tx . RecordNotFound ( ) { log . Error ( ctx , map [ string ] interface { } { " " : wi . ID , " " : spaceID , } , " " ) return nil , errors . NewNotFoundError ( " " , wi . ID ) } if tx . Error != nil { return nil , errors . NewInternalError ( err . Error ( ) ) } if res . Version != wi . Version { return nil , errors . NewVersionConflictError ( " " ) } wiType , err := r . witr . LoadTypeFromDB ( ctx , wi . Type ) if err != nil { return nil , errors . NewBadParameterError ( " " , wi . Type ) } res . Version = res . Version + 1 res . Type = wi . Type res . Fields = Fields { } res . ExecutionOrder = wi . Fields [ SystemOrder ] . ( float64 ) for fieldName , fieldDef := range wiType . Fields { if fieldName == SystemCreatedAt || fieldName == SystemUpdatedAt || fieldName == SystemOrder { continue } fieldValue := wi . Fields [ fieldName ] var err error res . Fields [ fieldName ] , err = fieldDef . ConvertToModel ( fieldName , fieldValue ) if err != nil { return nil , errors . NewBadParameterError ( fieldName , fieldValue ) } } tx = tx . Where ( " " , wi . Version ) . Save ( & res ) if err := tx . Error ; err != nil { log . Error ( ctx , map [ string ] interface { } { " " : wi . ID , " " : spaceID , " " : err , } , " " ) return nil , errors . NewInternalError ( err . Error ( ) ) } if tx . RowsAffected == 0 { return nil , errors . NewVersionConflictError ( " " ) } err = r . wirr . Create ( context . Background ( ) , modifierID , RevisionTypeUpdate , res ) if err != nil { return nil , errs . Wrapf ( err , " " ) } log . Info ( ctx , map [ string ] interface { } { " " : wi . ID , " " : spaceID , } , " " ) return ConvertWorkItemStorageToModel ( wiType , & res ) }
Save updates the given work item in storage.
Save updates the given work item in storage .
// store a revision of the modified work item