eintopf.info@v0.13.16/service/user/operator.go (about) 1 // Copyright (C) 2024 The Eintopf authors 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <https://www.gnu.org/licenses/>. 15 16 package user 17 18 import ( 19 "context" 20 21 "eintopf.info/internal/crud" 22 "eintopf.info/service/auth" 23 "eintopf.info/service/oqueue" 24 ) 25 26 type Operator struct { 27 storer Storer 28 queue oqueue.Service 29 } 30 31 func NewOperator(storer Storer, queue oqueue.Service) *Operator { 32 return &Operator{storer: storer, queue: queue} 33 } 34 35 type CreateOperation struct { 36 User *User 37 userID string 38 } 39 40 func NewCreateOperation(user *User, userID string) CreateOperation { 41 return CreateOperation{user, userID} 42 } 43 44 func (o CreateOperation) UserID() string { 45 return o.userID 46 } 47 48 func (i *Operator) Create(ctx context.Context, newUser *NewUser) (*User, error) { 49 user, err := i.storer.Create(ctx, newUser) 50 if err != nil { 51 return user, err 52 } 53 if user == nil { 54 return nil, nil 55 } 56 57 userID, err := auth.UserIDFromContext(ctx) 58 if err != nil { 59 return nil, err 60 } 61 i.queue.AddOperation(CreateOperation{ 62 User: user, 63 userID: userID, 64 }) 65 66 return user, nil 67 } 68 69 type UpdateOperation struct { 70 User *User 71 userID string 72 } 73 74 func NewUpdateOperation(user *User, userID string) UpdateOperation { 75 return UpdateOperation{user, userID} 76 } 77 78 func (o UpdateOperation) UserID() string { 79 return o.userID 80 } 81 82 func (i *Operator) Update(ctx context.Context, user *User) (*User, error) { 83 user, err := i.storer.Update(ctx, user) 84 if err != nil { 85 return user, err 86 } 87 if user == nil { 88 return nil, nil 89 } 90 91 userID, err := auth.UserIDFromContext(ctx) 92 if err != nil { 93 return nil, err 94 } 95 i.queue.AddOperation(UpdateOperation{ 96 User: user, 97 userID: userID, 98 }) 99 100 return user, nil 101 } 102 103 type DeleteOperation struct { 104 ID string 105 userID string 106 } 107 108 func NewDeleteOperation(id string, userID string) DeleteOperation { 109 return DeleteOperation{id, userID} 110 } 111 112 func (o DeleteOperation) UserID() string { 113 return o.userID 114 } 115 116 func (i *Operator) Delete(ctx context.Context, id string) error { 117 err := i.storer.Delete(ctx, id) 118 if err != nil { 119 return err 120 } 121 122 userID, err := auth.UserIDFromContext(ctx) 123 if err != nil { 124 return err 125 } 126 i.queue.AddOperation(DeleteOperation{ 127 ID: id, 128 userID: userID, 129 }) 130 131 return nil 132 } 133 134 func (i *Operator) FindByID(ctx context.Context, id string) (*User, error) { 135 return i.storer.FindByID(ctx, id) 136 } 137 138 func (i *Operator) Find(ctx context.Context, params *crud.FindParams[FindFilters]) ([]*User, int, error) { 139 return i.storer.Find(ctx, params) 140 }