From 5ad723d1e5ba7498dd04b7388e79ef13ab488181 Mon Sep 17 00:00:00 2001 From: DRypa Date: Sat, 23 Mar 2024 16:43:49 +0300 Subject: [PATCH] ForRent should contain reference for refresh session --- backend/device/for_rent.go | 2 +- backend/device/service.go | 17 +++++++---------- backend/workers/settings.go | 4 ++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/backend/device/for_rent.go b/backend/device/for_rent.go index 3eb27a0..3a57d53 100644 --- a/backend/device/for_rent.go +++ b/backend/device/for_rent.go @@ -3,6 +3,6 @@ package device import "receipt_collector/nalogru/device" type ForRent struct { - device.Device + *device.Device IsRent bool } diff --git a/backend/device/service.go b/backend/device/service.go index 982cbf5..f93ea6a 100644 --- a/backend/device/service.go +++ b/backend/device/service.go @@ -25,7 +25,7 @@ func NewService(ctx context.Context, r *repository.Repository) (*Service, error) for i, v := range all { v.Update = s.updateDeviceFunc(ctx, &v) s.devices[i] = ForRent{ - Device: v, + Device: &v, IsRent: false, } } @@ -39,7 +39,7 @@ func (s *Service) Add(ctx context.Context, d *device.Device) error { d.Update = s.updateDeviceFunc(ctx, d) forRent := ForRent{ - Device: *d, + Device: d, IsRent: false, } s.devices = append(s.devices, forRent) @@ -62,7 +62,7 @@ func (s *Service) Rent(ctx context.Context) (*device.Device, error) { for _, v := range s.devices { if v.IsRent == false { s.rent(&v) - return &v.Device, nil + return v.Device, nil } } return nil, errors.New("no available devices found") @@ -110,20 +110,17 @@ func (s *Service) Free(_ context.Context, device *device.Device) error { func (s *Service) All(_ context.Context) []*device.Device { res := make([]*device.Device, len(s.devices)) for i, d := range s.devices { - res[i] = &d.Device + res[i] = d.Device } return res } func (s *Service) GetByUserId(ctx context.Context, userId string) (*device.Device, error) { - devices, err := s.r.All(ctx) - if err != nil { - return nil, err - } + devices := s.All(ctx) for _, d := range devices { if d.UserId == userId { - d.Update = s.updateDeviceFunc(ctx, &d) - return &d, nil + d.Update = s.updateDeviceFunc(ctx, d) + return d, nil } } return nil, nil diff --git a/backend/workers/settings.go b/backend/workers/settings.go index d65f8cb..23cd835 100644 --- a/backend/workers/settings.go +++ b/backend/workers/settings.go @@ -8,12 +8,12 @@ import ( const intervalEnvironmentVariable = "GET_RECEIPT_WORKER_INTERVAL" -//Settings for Worker. +// Settings for Worker. type Settings struct { Interval time.Duration } -//ReadFromEnvironment creates Settings from environment variables. +// ReadFromEnvironment creates Settings from environment variables. func ReadFromEnvironment() Settings { workerIntervalString := os.Getenv(intervalEnvironmentVariable)