github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/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 dto
    20  
    21  import (
    22  	stub "github.com/e154/smart-home/api/stub"
    23  	"github.com/e154/smart-home/common"
    24  	m "github.com/e154/smart-home/models"
    25  )
    26  
    27  // Area ...
    28  type Area struct{}
    29  
    30  // NewAreaDto ...
    31  func NewAreaDto() Area {
    32  	return Area{}
    33  }
    34  
    35  // AddArea ...
    36  func (r Area) AddArea(from *stub.ApiNewAreaRequest) (area *m.Area) {
    37  	area = &m.Area{
    38  		Name:        from.Name,
    39  		Description: from.Description,
    40  		Polygon:     make([]m.Point, 0),
    41  		Zoom:        from.Zoom,
    42  
    43  		Resolution: from.Resolution,
    44  	}
    45  	if from.Center != nil {
    46  		area.Center = m.Point{
    47  			Lon: from.Center.Lon,
    48  			Lat: from.Center.Lat,
    49  		}
    50  	}
    51  	for _, point := range from.Polygon {
    52  		area.Polygon = append(area.Polygon, m.Point{
    53  			Lon: point.Lon,
    54  			Lat: point.Lat,
    55  		})
    56  	}
    57  	return
    58  }
    59  
    60  // UpdateArea ...
    61  func (r Area) UpdateArea(from *stub.AreaServiceUpdateAreaJSONBody, id int64) (area *m.Area) {
    62  	area = &m.Area{
    63  		Id:          id,
    64  		Name:        from.Name,
    65  		Description: from.Description,
    66  		Polygon:     make([]m.Point, 0),
    67  		Zoom:        from.Zoom,
    68  		Resolution:  from.Resolution,
    69  	}
    70  	if from.Center != nil {
    71  		area.Center = m.Point{
    72  			Lon: from.Center.Lon,
    73  			Lat: from.Center.Lat,
    74  		}
    75  	}
    76  	for _, point := range from.Polygon {
    77  		area.Polygon = append(area.Polygon, m.Point{
    78  			Lon: point.Lon,
    79  			Lat: point.Lat,
    80  		})
    81  	}
    82  	return
    83  }
    84  
    85  // ToSearchResult ...
    86  func (r Area) ToSearchResult(list []*m.Area) stub.ApiSearchAreaResult {
    87  
    88  	items := make([]stub.ApiArea, 0, len(list))
    89  
    90  	for _, i := range list {
    91  		items = append(items, *GetStubArea(i))
    92  	}
    93  
    94  	return stub.ApiSearchAreaResult{
    95  		Items: items,
    96  	}
    97  }
    98  
    99  // ToListResult ...
   100  func (r Area) ToListResult(list []*m.Area) []stub.ApiArea {
   101  
   102  	items := make([]stub.ApiArea, 0, len(list))
   103  
   104  	for _, i := range list {
   105  		items = append(items, *GetStubArea(i))
   106  	}
   107  
   108  	return items
   109  }
   110  
   111  // GetStubArea ...
   112  func GetStubArea(area *m.Area) (obj *stub.ApiArea) {
   113  	if area == nil {
   114  		return
   115  	}
   116  	obj = &stub.ApiArea{
   117  		Id:          area.Id,
   118  		Name:        area.Name,
   119  		Description: area.Description,
   120  		Polygon:     make([]stub.ApiAreaLocation, 0, len(area.Polygon)),
   121  		Center: &stub.ApiAreaLocation{
   122  			Lat: area.Center.Lat,
   123  			Lon: area.Center.Lon,
   124  		},
   125  		Zoom:       area.Zoom,
   126  		Resolution: area.Resolution,
   127  		CreatedAt:  area.CreatedAt,
   128  		UpdatedAt:  area.UpdatedAt,
   129  	}
   130  	for _, location := range area.Polygon {
   131  		obj.Polygon = append(obj.Polygon, stub.ApiAreaLocation{
   132  			Lat: location.Lat,
   133  			Lon: location.Lon,
   134  		})
   135  	}
   136  	return
   137  }
   138  
   139  func ImportArea(from *stub.ApiArea) (*int64, *m.Area) {
   140  	if from == nil {
   141  		return nil, nil
   142  	}
   143  	return common.Int64(from.Id), &m.Area{
   144  		Id:          from.Id,
   145  		Name:        from.Name,
   146  		Description: from.Description,
   147  	}
   148  }