github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/dashboard_card.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/stub"
    23  	"github.com/labstack/echo/v4"
    24  
    25  	"github.com/e154/smart-home/api/dto"
    26  )
    27  
    28  // ControllerDashboardCard ...
    29  type ControllerDashboardCard struct {
    30  	*ControllerCommon
    31  }
    32  
    33  // NewControllerDashboardCard ...
    34  func NewControllerDashboardCard(common *ControllerCommon) *ControllerDashboardCard {
    35  	return &ControllerDashboardCard{
    36  		ControllerCommon: common,
    37  	}
    38  }
    39  
    40  // AddDashboardCard ...
    41  func (c ControllerDashboardCard) DashboardCardServiceAddDashboardCard(ctx echo.Context, _ stub.DashboardCardServiceAddDashboardCardParams) error {
    42  
    43  	obj := &stub.ApiNewDashboardCardRequest{}
    44  	if err := c.Body(ctx, obj); err != nil {
    45  		return c.ERROR(ctx, err)
    46  	}
    47  
    48  	card, err := c.endpoint.DashboardCard.Add(ctx.Request().Context(), c.dto.DashboardCard.AddDashboardCard(obj))
    49  	if err != nil {
    50  		return c.ERROR(ctx, err)
    51  	}
    52  
    53  	return c.HTTP201(ctx, ResponseWithObj(ctx, dto.ToDashboardCard(card)))
    54  }
    55  
    56  // UpdateDashboardCard ...
    57  func (c ControllerDashboardCard) DashboardCardServiceUpdateDashboardCard(ctx echo.Context, id int64, _ stub.DashboardCardServiceUpdateDashboardCardParams) error {
    58  
    59  	obj := &stub.DashboardCardServiceUpdateDashboardCardJSONBody{}
    60  	if err := c.Body(ctx, obj); err != nil {
    61  		return c.ERROR(ctx, err)
    62  	}
    63  
    64  	card, err := c.endpoint.DashboardCard.Update(ctx.Request().Context(), c.dto.DashboardCard.UpdateDashboardCard(obj, id))
    65  	if err != nil {
    66  		return c.ERROR(ctx, err)
    67  	}
    68  
    69  	return c.HTTP200(ctx, ResponseWithObj(ctx, dto.ToDashboardCard(card)))
    70  }
    71  
    72  // GetDashboardCardById ...
    73  func (c ControllerDashboardCard) DashboardCardServiceGetDashboardCardById(ctx echo.Context, id int64) error {
    74  
    75  	card, err := c.endpoint.DashboardCard.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.ToDashboardCard(card)))
    81  }
    82  
    83  // GetDashboardCardList ...
    84  func (c ControllerDashboardCard) DashboardCardServiceGetDashboardCardList(ctx echo.Context, params stub.DashboardCardServiceGetDashboardCardListParams) error {
    85  
    86  	pagination := c.Pagination(params.Page, params.Limit, params.Sort)
    87  	items, total, err := c.endpoint.DashboardCard.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.DashboardCard.ToListResult(items), total, pagination))
    93  }
    94  
    95  // DeleteDashboardCard ...
    96  func (c ControllerDashboardCard) DashboardCardServiceDeleteDashboardCard(ctx echo.Context, id int64) error {
    97  
    98  	if err := c.endpoint.DashboardCard.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  // ImportDashboardCard ...
   106  func (c ControllerDashboardCard) DashboardCardServiceImportDashboardCard(ctx echo.Context, _ stub.DashboardCardServiceImportDashboardCardParams) error {
   107  
   108  	obj := &stub.ApiDashboardCard{}
   109  	if err := c.Body(ctx, obj); err != nil {
   110  		return c.ERROR(ctx, err)
   111  	}
   112  
   113  	card, err := c.endpoint.DashboardCard.Import(ctx.Request().Context(), dto.ImportDashboardCard(obj))
   114  	if err != nil {
   115  		return c.ERROR(ctx, err)
   116  	}
   117  
   118  	return c.HTTP200(ctx, ResponseWithObj(ctx, dto.ToDashboardCard(card)))
   119  }