gitlab.com/picnic-app/backend/role-api@v0.0.0-20230614140944-06a76ff3696d/internal/repo/spanner/assigned_roles.go (about)

     1  package spanner
     2  
     3  import (
     4  	"context"
     5  
     6  	"cloud.google.com/go/spanner"
     7  	"github.com/Masterminds/squirrel"
     8  	"google.golang.org/grpc/codes"
     9  
    10  	"gitlab.com/picnic-app/backend/role-api/internal/model"
    11  	"gitlab.com/picnic-app/backend/role-api/internal/repo"
    12  	"gitlab.com/picnic-app/backend/role-api/internal/repo/spanner/helpers"
    13  	"gitlab.com/picnic-app/backend/role-api/internal/repo/spanner/tables"
    14  )
    15  
    16  func (w writeOnly) InsertAssignedRole(
    17  	ctx context.Context,
    18  	in model.AssignedRole,
    19  ) error {
    20  	return InsertAssignedRole(ctx, w.bufferWriter(ctx), in)
    21  }
    22  
    23  func (rw readWrite) InsertAssignedRole(
    24  	ctx context.Context,
    25  	in model.AssignedRole,
    26  ) error {
    27  	return InsertAssignedRole(ctx, rw.tx, in)
    28  }
    29  
    30  func InsertAssignedRole(
    31  	_ context.Context,
    32  	db helpers.BufferWriter,
    33  	in model.AssignedRole,
    34  ) error {
    35  	var table tables.AssignedRoles
    36  
    37  	in.CreatedAt = spanner.CommitTimestamp
    38  
    39  	m, err := spanner.InsertStruct(
    40  		table.TableName(),
    41  		in,
    42  	)
    43  	if err != nil {
    44  		return fromError(err)
    45  	}
    46  
    47  	return db.BufferWrite([]*spanner.Mutation{m})
    48  }
    49  
    50  func (r readOnly) GetAssignedRole(
    51  	ctx context.Context,
    52  	entityID, roleID, circleID, userID string,
    53  ) (model.AssignedRole, bool, error) {
    54  	return GetAssignedRole(ctx, r.tx, entityID, roleID, circleID, userID)
    55  }
    56  
    57  func (rw readWrite) GetAssignedRole(
    58  	ctx context.Context,
    59  	entityID, roleID, circleID, userID string,
    60  ) (model.AssignedRole, bool, error) {
    61  	return GetAssignedRole(ctx, rw.tx, entityID, roleID, circleID, userID)
    62  }
    63  
    64  func GetAssignedRole(ctx context.Context,
    65  	db helpers.Queryer,
    66  	entityID, roleID, circleID, userID string,
    67  ) (model.AssignedRole, bool, error) {
    68  	table := tables.Get().AssignedRoles()
    69  
    70  	builder := squirrel.
    71  		Select(table.Columns()...).
    72  		From(table.TableName()).
    73  		Where(squirrel.Eq{table.EntityID(): entityID}).
    74  		Where(squirrel.Eq{table.RoleID(): roleID})
    75  
    76  	if circleID != "" {
    77  		builder = builder.Where(squirrel.Eq{table.CircleID(): circleID})
    78  	}
    79  
    80  	if userID != "" {
    81  		builder = builder.Where(squirrel.Eq{table.UserID(): userID})
    82  	}
    83  
    84  	q, args, err := builder.PlaceholderFormat(squirrel.AtP).ToSql()
    85  	if err != nil {
    86  		return model.AssignedRole{}, false, fromError(err)
    87  	}
    88  
    89  	params := helpers.ArgsToParams(args)
    90  	stmt := spanner.Statement{SQL: q, Params: params}
    91  
    92  	out, err := helpers.GetResult[model.AssignedRole](db.Query(ctx, stmt))
    93  	if err != nil {
    94  		if spanner.ErrCode(err) == codes.NotFound {
    95  			return model.AssignedRole{}, false, nil
    96  		}
    97  		return model.AssignedRole{}, false, fromError(err)
    98  	}
    99  
   100  	return out, true, nil
   101  }
   102  
   103  func (r readOnly) GetAssignedRoles(
   104  	ctx context.Context,
   105  	entityID, circleID, userID string,
   106  ) ([]model.AssignedRole, error) {
   107  	return GetAssignedRoles(ctx, r.tx, entityID, circleID, userID)
   108  }
   109  
   110  func (rw readWrite) GetAssignedRoles(
   111  	ctx context.Context,
   112  	entityID, circleID, userID string,
   113  ) ([]model.AssignedRole, error) {
   114  	return GetAssignedRoles(ctx, rw.tx, entityID, circleID, userID)
   115  }
   116  
   117  func GetAssignedRoles(ctx context.Context,
   118  	db helpers.Queryer,
   119  	entityID, circleID, userID string,
   120  ) ([]model.AssignedRole, error) {
   121  	table := tables.Get().AssignedRoles()
   122  
   123  	builder := squirrel.
   124  		Select(table.Columns()...).
   125  		From(table.TableName()).
   126  		Where(squirrel.Eq{table.EntityID(): entityID})
   127  
   128  	if circleID != "" {
   129  		builder = builder.Where(squirrel.Eq{table.CircleID(): circleID})
   130  	}
   131  
   132  	if userID != "" {
   133  		builder = builder.Where(squirrel.Eq{table.UserID(): userID})
   134  	}
   135  
   136  	q, args, err := builder.PlaceholderFormat(squirrel.AtP).ToSql()
   137  	if err != nil {
   138  		return nil, fromError(err)
   139  	}
   140  
   141  	params := helpers.ArgsToParams(args)
   142  	stmt := spanner.Statement{SQL: q, Params: params}
   143  
   144  	out, err := helpers.GetResults[model.AssignedRole](db.Query(ctx, stmt))
   145  	if err != nil {
   146  		return nil, fromError(err)
   147  	}
   148  
   149  	return out, nil
   150  }
   151  
   152  func (r readOnly) GetCircleAssignedRoles(
   153  	ctx context.Context,
   154  	entityID string,
   155  	circleIDs ...string,
   156  ) ([]model.AssignedRole, error) {
   157  	return GetCircleAssignedRoles(ctx, r.tx, entityID, circleIDs...)
   158  }
   159  
   160  func (rw readWrite) GetCircleAssignedRoles(
   161  	ctx context.Context,
   162  	entityID string,
   163  	circleIDs ...string,
   164  ) ([]model.AssignedRole, error) {
   165  	return GetCircleAssignedRoles(ctx, rw.tx, entityID, circleIDs...)
   166  }
   167  
   168  func GetCircleAssignedRoles(
   169  	ctx context.Context,
   170  	db helpers.Queryer,
   171  	entityID string,
   172  	circleIDs ...string,
   173  ) ([]model.AssignedRole, error) {
   174  	table := tables.Get().AssignedRoles()
   175  
   176  	builder := squirrel.
   177  		Select(table.Columns()...).
   178  		From(table.TableName()).
   179  		Where(squirrel.Eq{table.EntityID(): entityID}).
   180  		Where(squirrel.Eq{table.CircleID(): circleIDs})
   181  
   182  	q, args, err := builder.PlaceholderFormat(squirrel.AtP).ToSql()
   183  	if err != nil {
   184  		return nil, fromError(err)
   185  	}
   186  
   187  	params := helpers.ArgsToParams(args)
   188  	stmt := spanner.Statement{SQL: q, Params: params}
   189  
   190  	out, err := helpers.GetResults[model.AssignedRole](db.Query(ctx, stmt))
   191  	if err != nil {
   192  		return nil, fromError(err)
   193  	}
   194  
   195  	return out, nil
   196  }
   197  
   198  func (w writeOnly) DeleteAssignedRoles(
   199  	ctx context.Context,
   200  	itemIDs ...string,
   201  ) error {
   202  	return w.tx(ctx, func(ctx context.Context, tx repo.WriteActions) error {
   203  		return tx.DeleteAssignedRoles(ctx, itemIDs...)
   204  	})
   205  }
   206  
   207  func (rw readWrite) DeleteAssignedRoles(
   208  	ctx context.Context,
   209  	itemIDs ...string,
   210  ) error {
   211  	return DeleteAssignedRoles(ctx, rw.tx, itemIDs...)
   212  }
   213  
   214  func DeleteAssignedRoles(
   215  	_ context.Context,
   216  	db helpers.DBWriter,
   217  	itemIDs ...string,
   218  ) error {
   219  	err := helpers.Delete[tables.AssignedRoles](db, itemIDs...)
   220  	if err != nil {
   221  		return fromError(err)
   222  	}
   223  
   224  	return nil
   225  }