gitlab.com/picnic-app/backend/role-api@v0.0.0-20230614140944-06a76ff3696d/internal/repo/spanner/roles.go (about) 1 package spanner 2 3 import ( 4 "context" 5 6 "cloud.google.com/go/spanner" 7 "google.golang.org/grpc/codes" 8 9 "gitlab.com/picnic-app/backend/role-api/internal/model" 10 "gitlab.com/picnic-app/backend/role-api/internal/repo/spanner/data" 11 "gitlab.com/picnic-app/backend/role-api/internal/repo/spanner/deserialize" 12 "gitlab.com/picnic-app/backend/role-api/internal/repo/spanner/helpers" 13 "gitlab.com/picnic-app/backend/role-api/internal/repo/spanner/serialize" 14 "gitlab.com/picnic-app/backend/role-api/internal/repo/spanner/tables" 15 ) 16 17 func (w writeOnly) InsertRole(ctx context.Context, in model.Role) error { 18 return InsertRole(ctx, w.bufferWriter(ctx), in) 19 } 20 21 func (rw readWrite) InsertRole(ctx context.Context, in model.Role) error { 22 return InsertRole(ctx, rw.tx, in) 23 } 24 25 func InsertRole(_ context.Context, db helpers.BufferWriter, in model.Role) error { 26 var table tables.Roles 27 28 in.CreatedAt = spanner.CommitTimestamp 29 30 m, err := spanner.InsertStruct( 31 table.TableName(), 32 serialize.Role(in), 33 ) 34 if err != nil { 35 return fromError(err) 36 } 37 38 err = db.BufferWrite([]*spanner.Mutation{m}) 39 if err != nil { 40 return fromError(err) 41 } 42 43 return nil 44 } 45 46 func (r readOnly) GetRole(ctx context.Context, itemID string) (model.Role, bool, error) { 47 return GetRole(ctx, r.tx, itemID) 48 } 49 50 func (rw readWrite) GetRole(ctx context.Context, itemID string) (model.Role, bool, error) { 51 return GetRole(ctx, rw.tx, itemID) 52 } 53 54 func GetRole(ctx context.Context, db helpers.RowReader, itemID string) (model.Role, bool, error) { 55 item, err := helpers.GetByKey[data.Role, tables.Roles](ctx, db, itemID) 56 if err != nil { 57 if spanner.ErrCode(err) == codes.NotFound { 58 return model.Role{}, false, nil 59 } 60 61 return model.Role{}, false, fromError(err) 62 } 63 64 return deserialize.Role(item), true, nil 65 } 66 67 func (r readOnly) GetRoles(ctx context.Context, itemIDs []string) ([]model.Role, error) { 68 return GetRoles(ctx, r.tx, itemIDs) 69 } 70 71 func (rw readWrite) GetRoles(ctx context.Context, itemIDs []string) ([]model.Role, error) { 72 return GetRoles(ctx, rw.tx, itemIDs) 73 } 74 75 func GetRoles(ctx context.Context, db helpers.Reader, itemIDs []string) ([]model.Role, error) { 76 items, err := helpers.GetByKeys[data.Role, tables.Roles](ctx, db, itemIDs...) 77 if err != nil { 78 return nil, fromError(err) 79 } 80 81 return deserialize.Roles(items), nil 82 } 83 84 func (w writeOnly) UpdateRole(ctx context.Context, in model.Role) error { 85 return UpdateRole(ctx, w.bufferWriter(ctx), in) 86 } 87 88 func (rw readWrite) UpdateRole(ctx context.Context, in model.Role) error { 89 return UpdateRole(ctx, rw.tx, in) 90 } 91 92 func UpdateRole(_ context.Context, db helpers.BufferWriter, in model.Role) error { 93 var table tables.Roles 94 m, err := spanner.UpdateStruct( 95 table.TableName(), 96 serialize.Role(in), 97 ) 98 if err != nil { 99 return fromError(err) 100 } 101 102 err = db.BufferWrite([]*spanner.Mutation{m}) 103 if err != nil { 104 return fromError(err) 105 } 106 107 return nil 108 } 109 110 func (w writeOnly) PartialUpdateRoles(ctx context.Context, itemIDs []string, in model.RoleUpdate) error { 111 return PartialUpdateRoles(ctx, w.bufferWriter(ctx), itemIDs, in) 112 } 113 114 func (rw readWrite) PartialUpdateRoles(ctx context.Context, itemIDs []string, in model.RoleUpdate) error { 115 return PartialUpdateRoles(ctx, rw.tx, itemIDs, in) 116 } 117 118 func PartialUpdateRoles(_ context.Context, db helpers.BufferWriter, itemIDs []string, in model.RoleUpdate) error { 119 var table tables.Roles 120 121 updates := make([]*spanner.Mutation, len(itemIDs)) 122 123 for i, itemID := range itemIDs { 124 columns := []string{table.ID()} 125 values := []any{itemID} 126 127 if in.Name != nil { 128 columns = append(columns, table.Name()) 129 values = append(values, in.Name.Value) 130 } 131 132 if in.Permissions != nil { 133 columns = append(columns, table.Permissions()) 134 values = append(values, serialize.Permissions(in.Permissions.Value.([]model.Permission))) 135 } 136 137 if in.DeletedAt != nil { 138 columns = append(columns, table.DeletedAt()) 139 values = append(values, in.DeletedAt.Value) 140 } 141 142 updates[i] = spanner.Update( 143 table.TableName(), 144 columns, 145 values, 146 ) 147 148 } 149 150 err := db.BufferWrite(updates) 151 if err != nil { 152 return fromError(err) 153 } 154 155 return nil 156 }