eintopf.info@v0.13.16/service/revent/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 revent
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"eintopf.info/internal/crud"
    23  	"eintopf.info/service/auth"
    24  	"eintopf.info/service/event"
    25  	"eintopf.info/service/oqueue"
    26  )
    27  
    28  type operator struct {
    29  	service Service
    30  	queue   oqueue.Service
    31  }
    32  
    33  func NewOperator(service Service, queue oqueue.Service) Service {
    34  	return &operator{service: service, queue: queue}
    35  }
    36  
    37  type CreateOperation struct {
    38  	RepeatingEvent *RepeatingEvent
    39  	userID         string
    40  }
    41  
    42  func NewCreateOperation(repeatingEvent *RepeatingEvent, userID string) CreateOperation {
    43  	return CreateOperation{repeatingEvent, userID}
    44  }
    45  
    46  func (o CreateOperation) UserID() string {
    47  	return o.userID
    48  }
    49  
    50  func (i *operator) Create(ctx context.Context, newRepeatingEvent *NewRepeatingEvent) (*RepeatingEvent, error) {
    51  	repeatingEvent, err := i.service.Create(ctx, newRepeatingEvent)
    52  	if err != nil {
    53  		return repeatingEvent, err
    54  	}
    55  	if repeatingEvent == nil {
    56  		return nil, nil
    57  	}
    58  
    59  	userID, err := auth.UserIDFromContext(ctx)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	i.queue.AddOperation(CreateOperation{
    64  		RepeatingEvent: repeatingEvent,
    65  		userID:         userID,
    66  	})
    67  
    68  	return repeatingEvent, nil
    69  }
    70  
    71  type UpdateOperation struct {
    72  	RepeatingEvent *RepeatingEvent
    73  	userID         string
    74  }
    75  
    76  func NewUpdateOperation(repeatingEvent *RepeatingEvent, userID string) UpdateOperation {
    77  	return UpdateOperation{repeatingEvent, userID}
    78  }
    79  
    80  func (o UpdateOperation) UserID() string {
    81  	return o.userID
    82  }
    83  
    84  func (i *operator) Update(ctx context.Context, repeatingEvent *RepeatingEvent) (*RepeatingEvent, error) {
    85  	repeatingEvent, err := i.service.Update(ctx, repeatingEvent)
    86  	if err != nil {
    87  		return repeatingEvent, err
    88  	}
    89  	if repeatingEvent == nil {
    90  		return nil, nil
    91  	}
    92  
    93  	userID, err := auth.UserIDFromContext(ctx)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	i.queue.AddOperation(UpdateOperation{
    98  		RepeatingEvent: repeatingEvent,
    99  		userID:         userID,
   100  	})
   101  
   102  	return repeatingEvent, nil
   103  }
   104  
   105  type DeleteOperation struct {
   106  	ID     string
   107  	userID string
   108  }
   109  
   110  func NewDeleteOperation(id string, userID string) DeleteOperation {
   111  	return DeleteOperation{id, userID}
   112  }
   113  
   114  func (o DeleteOperation) UserID() string {
   115  	return o.userID
   116  }
   117  
   118  func (i *operator) Delete(ctx context.Context, id string) error {
   119  	err := i.service.Delete(ctx, id)
   120  	if err != nil {
   121  		return err
   122  	}
   123  
   124  	userID, err := auth.UserIDFromContext(ctx)
   125  	if err != nil {
   126  		return err
   127  	}
   128  	i.queue.AddOperation(DeleteOperation{
   129  		ID:     id,
   130  		userID: userID,
   131  	})
   132  
   133  	return nil
   134  }
   135  
   136  func (i *operator) FindByID(ctx context.Context, id string) (*RepeatingEvent, error) {
   137  	return i.service.FindByID(ctx, id)
   138  }
   139  
   140  func (i *operator) Find(ctx context.Context, params *crud.FindParams[FindFilters]) ([]*RepeatingEvent, int, error) {
   141  	return i.service.Find(ctx, params)
   142  }
   143  
   144  func (o *operator) GenerateEvents(ctx context.Context, id string, start, end time.Time) ([]*event.Event, error) {
   145  	return o.service.GenerateEvents(ctx, id, start, end)
   146  }