eintopf.info@v0.13.16/service/place/operator.go (about)

     1  // Copyright (C) 2022 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 place
    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  	service Service
    28  	queue   *oqueue.Queue
    29  }
    30  
    31  func NewOperator(service Service, queue *oqueue.Queue) Service {
    32  	return &operator{service: service, queue: queue}
    33  }
    34  
    35  type CreateOperation struct {
    36  	Place  *Place
    37  	userID string
    38  }
    39  
    40  func (o CreateOperation) UserID() string {
    41  	return o.userID
    42  }
    43  
    44  func (i *operator) Create(ctx context.Context, newPlace *NewPlace) (*Place, error) {
    45  	place, err := i.service.Create(ctx, newPlace)
    46  	if err != nil {
    47  		return place, err
    48  	}
    49  
    50  	if place != nil {
    51  		userID, err := auth.UserIDFromContext(ctx)
    52  		if err != nil {
    53  			return nil, err
    54  		}
    55  		i.queue.AddOperation(CreateOperation{
    56  			Place:  place,
    57  			userID: userID,
    58  		})
    59  	}
    60  
    61  	return place, nil
    62  }
    63  
    64  type UpdateOperation struct {
    65  	Place  *Place
    66  	userID string
    67  }
    68  
    69  func (o UpdateOperation) UserID() string {
    70  	return o.userID
    71  }
    72  
    73  func (i *operator) Update(ctx context.Context, place *Place) (*Place, error) {
    74  	place, err := i.service.Update(ctx, place)
    75  	if err != nil {
    76  		return place, err
    77  	}
    78  
    79  	if place != nil {
    80  		userID, err := auth.UserIDFromContext(ctx)
    81  		if err != nil {
    82  			return nil, err
    83  		}
    84  		i.queue.AddOperation(UpdateOperation{
    85  			Place:  place,
    86  			userID: userID,
    87  		})
    88  	}
    89  
    90  	return place, nil
    91  }
    92  
    93  type DeleteOperation struct {
    94  	ID     string
    95  	userID string
    96  }
    97  
    98  func (o DeleteOperation) UserID() string {
    99  	return o.userID
   100  }
   101  
   102  func (i *operator) Delete(ctx context.Context, id string) error {
   103  	err := i.service.Delete(ctx, id)
   104  	if err != nil {
   105  		return err
   106  	}
   107  
   108  	userID, err := auth.UserIDFromContext(ctx)
   109  	if err != nil {
   110  		return err
   111  	}
   112  	i.queue.AddOperation(DeleteOperation{
   113  		ID:     id,
   114  		userID: userID,
   115  	})
   116  
   117  	return nil
   118  }
   119  
   120  func (i *operator) FindByID(ctx context.Context, id string) (*Place, error) {
   121  	return i.service.FindByID(ctx, id)
   122  }
   123  
   124  func (i *operator) Find(ctx context.Context, params *crud.FindParams[FindFilters]) ([]*Place, int, error) {
   125  	return i.service.Find(ctx, params)
   126  }