github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/endpoint/area.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package endpoint
    20  
    21  import (
    22  	"context"
    23  
    24  	"github.com/e154/smart-home/common"
    25  	"github.com/e154/smart-home/common/apperr"
    26  	m "github.com/e154/smart-home/models"
    27  )
    28  
    29  // AreaEndpoint ...
    30  type AreaEndpoint struct {
    31  	*CommonEndpoint
    32  }
    33  
    34  // NewAreaEndpoint ...
    35  func NewAreaEndpoint(common *CommonEndpoint) *AreaEndpoint {
    36  	return &AreaEndpoint{
    37  		CommonEndpoint: common,
    38  	}
    39  }
    40  
    41  // Add ...
    42  func (n *AreaEndpoint) Add(ctx context.Context, params *m.Area) (result *m.Area, err error) {
    43  
    44  	if ok, errs := n.validation.Valid(params); !ok {
    45  		err = apperr.ErrInvalidRequest
    46  		apperr.SetValidationErrors(err, errs)
    47  		return
    48  	}
    49  
    50  	if _, err = n.adaptors.Area.Add(ctx, params); err != nil {
    51  		return
    52  	}
    53  
    54  	result, err = n.adaptors.Area.GetByName(ctx, params.Name)
    55  
    56  	return
    57  }
    58  
    59  // GetById ...
    60  func (n *AreaEndpoint) GetById(ctx context.Context, id int64) (result *m.Area, err error) {
    61  
    62  	result, err = n.adaptors.Area.GetById(ctx, id)
    63  
    64  	return
    65  }
    66  
    67  // GetByName ...
    68  func (n *AreaEndpoint) GetByName(ctx context.Context, name string) (result *m.Area, err error) {
    69  
    70  	result, err = n.adaptors.Area.GetByName(ctx, name)
    71  
    72  	return
    73  }
    74  
    75  // Update ...
    76  func (n *AreaEndpoint) Update(ctx context.Context, params *m.Area) (area *m.Area, err error) {
    77  
    78  	area, err = n.adaptors.Area.GetById(ctx, params.Id)
    79  	if err != nil {
    80  		return
    81  	}
    82  
    83  	area.Name = params.Name
    84  	area.Description = params.Description
    85  	area.Zoom = params.Zoom
    86  	area.Center = params.Center
    87  	area.Resolution = params.Resolution
    88  	area.Polygon = params.Polygon
    89  
    90  	if ok, errs := n.validation.Valid(area); !ok {
    91  		err = apperr.ErrInvalidRequest
    92  		apperr.SetValidationErrors(err, errs)
    93  		return
    94  	}
    95  
    96  	if err = n.adaptors.Area.Update(ctx, area); err != nil {
    97  		return
    98  	}
    99  
   100  	return
   101  }
   102  
   103  // GetList ...
   104  func (n *AreaEndpoint) GetList(ctx context.Context, pagination common.PageParams) (result []*m.Area, total int64, err error) {
   105  
   106  	result, total, err = n.adaptors.Area.List(ctx, pagination.Limit, pagination.Offset, pagination.Order, pagination.SortBy)
   107  	if err != nil {
   108  	}
   109  	return
   110  }
   111  
   112  // Delete ...
   113  func (n *AreaEndpoint) Delete(ctx context.Context, id int64) (err error) {
   114  
   115  	var area *m.Area
   116  	area, err = n.adaptors.Area.GetById(ctx, id)
   117  	if err != nil {
   118  		return
   119  	}
   120  
   121  	err = n.adaptors.Area.DeleteByName(ctx, area.Name)
   122  
   123  	return
   124  }
   125  
   126  // Search ...
   127  func (n *AreaEndpoint) Search(ctx context.Context, query string, limit, offset int64) (result []*m.Area, total int64, err error) {
   128  
   129  	if limit == 0 {
   130  		limit = common.DefaultPageSize
   131  	}
   132  
   133  	result, total, err = n.adaptors.Area.Search(ctx, query, limit, offset)
   134  
   135  	return
   136  }