github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/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 controllers
    20  
    21  import (
    22  	"github.com/e154/smart-home/api/dto"
    23  	"github.com/labstack/echo/v4"
    24  
    25  	"github.com/e154/smart-home/api/stub"
    26  )
    27  
    28  // ControllerArea ...
    29  type ControllerArea struct {
    30  	*ControllerCommon
    31  }
    32  
    33  // NewControllerArea ...
    34  func NewControllerArea(common *ControllerCommon) *ControllerArea {
    35  	return &ControllerArea{
    36  		ControllerCommon: common,
    37  	}
    38  }
    39  
    40  // AddArea ...
    41  func (c ControllerArea) AreaServiceAddArea(ctx echo.Context, _ stub.AreaServiceAddAreaParams) error {
    42  
    43  	obj := &stub.ApiNewAreaRequest{}
    44  	if err := c.Body(ctx, obj); err != nil {
    45  		return c.ERROR(ctx, err)
    46  	}
    47  
    48  	area, err := c.endpoint.Area.Add(ctx.Request().Context(), c.dto.Area.AddArea(obj))
    49  	if err != nil {
    50  		return c.ERROR(ctx, err)
    51  	}
    52  
    53  	return c.HTTP201(ctx, ResponseWithObj(ctx, dto.GetStubArea(area)))
    54  }
    55  
    56  // UpdateArea ...
    57  func (c ControllerArea) AreaServiceUpdateArea(ctx echo.Context, id int64, _ stub.AreaServiceUpdateAreaParams) error {
    58  
    59  	obj := &stub.AreaServiceUpdateAreaJSONBody{}
    60  	if err := c.Body(ctx, obj); err != nil {
    61  		return c.ERROR(ctx, err)
    62  	}
    63  
    64  	area, err := c.endpoint.Area.Update(ctx.Request().Context(), c.dto.Area.UpdateArea(obj, id))
    65  	if err != nil {
    66  		return c.ERROR(ctx, err)
    67  	}
    68  
    69  	return c.HTTP200(ctx, ResponseWithObj(ctx, dto.GetStubArea(area)))
    70  }
    71  
    72  // GetAreaById ...
    73  func (c ControllerArea) AreaServiceGetAreaById(ctx echo.Context, id int64) error {
    74  
    75  	area, err := c.endpoint.Area.GetById(ctx.Request().Context(), id)
    76  	if err != nil {
    77  		return c.ERROR(ctx, err)
    78  	}
    79  
    80  	return c.HTTP200(ctx, ResponseWithObj(ctx, dto.GetStubArea(area)))
    81  }
    82  
    83  // GetAreaList ...
    84  func (c ControllerArea) AreaServiceGetAreaList(ctx echo.Context, params stub.AreaServiceGetAreaListParams) error {
    85  
    86  	pagination := c.Pagination(params.Page, params.Limit, params.Sort)
    87  	items, total, err := c.endpoint.Area.GetList(ctx.Request().Context(), pagination)
    88  	if err != nil {
    89  		return c.ERROR(ctx, err)
    90  	}
    91  
    92  	return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Area.ToListResult(items), total, pagination))
    93  }
    94  
    95  // DeleteArea ...
    96  func (c ControllerArea) AreaServiceDeleteArea(ctx echo.Context, id int64) error {
    97  
    98  	if err := c.endpoint.Area.Delete(ctx.Request().Context(), id); err != nil {
    99  		return c.ERROR(ctx, err)
   100  	}
   101  
   102  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
   103  }
   104  
   105  // SearchArea ...
   106  func (c ControllerArea) AreaServiceSearchArea(ctx echo.Context, params stub.AreaServiceSearchAreaParams) error {
   107  
   108  	search := c.Search(params.Query, params.Limit, params.Offset)
   109  	items, _, err := c.endpoint.Area.Search(ctx.Request().Context(), search.Query, search.Limit, search.Offset)
   110  	if err != nil {
   111  		return c.ERROR(ctx, err)
   112  	}
   113  
   114  	return c.HTTP200(ctx, c.dto.Area.ToSearchResult(items))
   115  }