mirror of
https://github.com/absmach/magistrala.git
synced 2026-08-07 07:14:46 +00:00
16ba29cf4a
Property Based Tests / api-test (push) Has been cancelled
Continuous Delivery / lint-and-build (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled
CI Pipeline / Lint Proto (push) Has been cancelled
CI Pipeline / Detect Changes (push) Has been cancelled
Continuous Delivery / Build and Push Docker Images (push) Has been cancelled
CI Pipeline / lint-and-build (push) Has been cancelled
CI Pipeline / Test ${{ matrix.module }} (push) Has been cancelled
CI Pipeline / Upload Coverage (push) Has been cancelled
Signed-off-by: Arvindh <arvindh91@gmail.com> Signed-off-by: dusan <borovcanindusan1@gmail.com> Signed-off-by: Rodney Osodo <socials@rodneyosodo.com> Co-authored-by: Dušan Borovčanin <dusan.borovcanin@absmach.eu> Co-authored-by: Rodney Osodo <socials@rodneyosodo.com> Co-authored-by: dusan <borovcanindusan1@gmail.com>
73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package alarms
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/absmach/magistrala"
|
|
"github.com/absmach/magistrala/pkg/authn"
|
|
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
|
|
)
|
|
|
|
type service struct {
|
|
idp magistrala.IDProvider
|
|
repo Repository
|
|
}
|
|
|
|
var _ Service = (*service)(nil)
|
|
|
|
func NewService(idp magistrala.IDProvider, repo Repository) Service {
|
|
return &service{
|
|
idp: idp,
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (s *service) CreateAlarm(ctx context.Context, alarm Alarm) (Alarm, error) {
|
|
id, err := s.idp.ID()
|
|
if err != nil {
|
|
return Alarm{}, err
|
|
}
|
|
alarm.ID = id
|
|
if alarm.CreatedAt.IsZero() {
|
|
alarm.CreatedAt = time.Now()
|
|
}
|
|
|
|
if err := alarm.Validate(); err != nil {
|
|
return Alarm{}, err
|
|
}
|
|
|
|
created, err := s.repo.CreateAlarm(ctx, alarm)
|
|
if err != nil && err != repoerr.ErrNotFound {
|
|
return Alarm{}, err
|
|
}
|
|
if err == repoerr.ErrNotFound {
|
|
return Alarm{}, nil
|
|
}
|
|
|
|
return created, nil
|
|
}
|
|
|
|
func (s *service) ViewAlarm(ctx context.Context, session authn.Session, alarmID string) (Alarm, error) {
|
|
return s.repo.ViewAlarm(ctx, alarmID, session.DomainID)
|
|
}
|
|
|
|
func (s *service) ListAlarms(ctx context.Context, session authn.Session, pm PageMetadata) (AlarmsPage, error) {
|
|
pm.DomainID = session.DomainID
|
|
return s.repo.ListAllAlarms(ctx, pm)
|
|
}
|
|
|
|
func (s *service) DeleteAlarm(ctx context.Context, session authn.Session, alarmID string) error {
|
|
return s.repo.DeleteAlarm(ctx, alarmID)
|
|
}
|
|
|
|
func (s *service) UpdateAlarm(ctx context.Context, session authn.Session, alarm Alarm) (Alarm, error) {
|
|
alarm.UpdatedAt = time.Now()
|
|
alarm.UpdatedBy = session.UserID
|
|
|
|
return s.repo.UpdateAlarm(ctx, alarm)
|
|
}
|