github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/projectoperator/project_operator.go (about)

     1  package projectoperator
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
     7  	"github.com/machinefi/w3bstream/pkg/errors/status"
     8  	"github.com/machinefi/w3bstream/pkg/models"
     9  	"github.com/machinefi/w3bstream/pkg/types"
    10  )
    11  
    12  func IsOperatorOccupied(ctx context.Context, operatorID types.SFID) (bool, error) {
    13  	d := types.MustMgrDBExecutorFromContext(ctx)
    14  	m := &models.ProjectOperator{}
    15  	pos, err := m.List(d, m.ColOperatorID().Eq(operatorID))
    16  	if err != nil {
    17  		return false, status.DatabaseError.StatusErr().WithDesc(err.Error())
    18  	}
    19  	return len(pos) > 0, nil
    20  }
    21  
    22  func GetByProject(ctx context.Context, projectID types.SFID) (*models.ProjectOperator, error) {
    23  	d := types.MustMgrDBExecutorFromContext(ctx)
    24  	m := &models.ProjectOperator{
    25  		RelProject: models.RelProject{ProjectID: projectID},
    26  	}
    27  
    28  	if err := m.FetchByProjectID(d); err != nil {
    29  		if sqlx.DBErr(err).IsNotFound() {
    30  			return nil, status.ProjectOperatorNotFound
    31  		}
    32  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    33  	}
    34  	return m, nil
    35  }
    36  
    37  func RemoveByProject(ctx context.Context, projectID types.SFID) error {
    38  	d := types.MustMgrDBExecutorFromContext(ctx)
    39  	m := &models.ProjectOperator{
    40  		RelProject: models.RelProject{ProjectID: projectID},
    41  	}
    42  	if err := m.DeleteByProjectID(d); err != nil {
    43  		return status.DatabaseError.StatusErr().WithDesc(err.Error())
    44  	}
    45  	return nil
    46  }
    47  
    48  func Create(ctx context.Context, projectID, operatorID types.SFID) (*models.ProjectOperator, error) {
    49  	d := types.MustMgrDBExecutorFromContext(ctx)
    50  
    51  	m := &models.ProjectOperator{
    52  		RelProject:  models.RelProject{ProjectID: projectID},
    53  		RelOperator: models.RelOperator{OperatorID: operatorID},
    54  	}
    55  
    56  	if err := m.Create(d); err != nil {
    57  		if sqlx.DBErr(err).IsConflict() {
    58  			return nil, status.ProjectOperatorConflict
    59  		}
    60  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    61  	}
    62  	return m, nil
    63  }