github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/automation.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/labstack/echo/v4"
    23  
    24  	"github.com/e154/smart-home/api/stub"
    25  )
    26  
    27  // ControllerAutomation ...
    28  type ControllerAutomation struct {
    29  	*ControllerCommon
    30  }
    31  
    32  // NewControllerAutomation ...
    33  func NewControllerAutomation(common *ControllerCommon) *ControllerAutomation {
    34  	return &ControllerAutomation{
    35  		ControllerCommon: common,
    36  	}
    37  }
    38  
    39  // AddTask ...
    40  func (c ControllerAutomation) AutomationServiceAddTask(ctx echo.Context, _ stub.AutomationServiceAddTaskParams) error {
    41  
    42  	obj := &stub.ApiNewTaskRequest{}
    43  	if err := c.Body(ctx, obj); err != nil {
    44  		return c.ERROR(ctx, err)
    45  	}
    46  
    47  	task, err := c.endpoint.Task.Add(ctx.Request().Context(), c.dto.Automation.AddTask(obj))
    48  	if err != nil {
    49  		return c.ERROR(ctx, err)
    50  	}
    51  
    52  	return c.HTTP201(ctx, ResponseWithObj(ctx, c.dto.Automation.GetTask(task)))
    53  }
    54  
    55  // UpdateTask ...
    56  func (c ControllerAutomation) AutomationServiceUpdateTask(ctx echo.Context, id int64, _ stub.AutomationServiceUpdateTaskParams) error {
    57  
    58  	obj := &stub.AutomationServiceUpdateTaskJSONBody{}
    59  	if err := c.Body(ctx, obj); err != nil {
    60  		return c.ERROR(ctx, err)
    61  	}
    62  
    63  	task, err := c.endpoint.Task.Update(ctx.Request().Context(), c.dto.Automation.UpdateTask(obj, id))
    64  	if err != nil {
    65  		return c.ERROR(ctx, err)
    66  	}
    67  
    68  	return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Automation.GetTask(task)))
    69  }
    70  
    71  // GetTask ...
    72  func (c ControllerAutomation) AutomationServiceGetTask(ctx echo.Context, id int64) error {
    73  
    74  	task, err := c.endpoint.Task.GetById(ctx.Request().Context(), id)
    75  	if err != nil {
    76  		return c.ERROR(ctx, err)
    77  	}
    78  
    79  	return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.Automation.GetTask(task)))
    80  }
    81  
    82  // GetTaskList ...
    83  func (c ControllerAutomation) AutomationServiceGetTaskList(ctx echo.Context, params stub.AutomationServiceGetTaskListParams) error {
    84  
    85  	pagination := c.Pagination(params.Page, params.Limit, params.Sort)
    86  	items, total, err := c.endpoint.Task.List(ctx.Request().Context(), pagination)
    87  	if err != nil {
    88  		return c.ERROR(ctx, err)
    89  	}
    90  
    91  	return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.Automation.ToListResult(items), total, pagination))
    92  }
    93  
    94  // DeleteTask ...
    95  func (c ControllerAutomation) AutomationServiceDeleteTask(ctx echo.Context, id int64) error {
    96  
    97  	if err := c.endpoint.Task.Delete(ctx.Request().Context(), id); err != nil {
    98  		return c.ERROR(ctx, err)
    99  	}
   100  
   101  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
   102  }
   103  
   104  // EnableTask ...
   105  func (c ControllerAutomation) AutomationServiceEnableTask(ctx echo.Context, id int64) error {
   106  
   107  	if err := c.endpoint.Task.Enable(ctx.Request().Context(), id); err != nil {
   108  		return c.ERROR(ctx, err)
   109  	}
   110  
   111  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
   112  }
   113  
   114  // DisableTask ...
   115  func (c ControllerAutomation) AutomationServiceDisableTask(ctx echo.Context, id int64) error {
   116  
   117  	if err := c.endpoint.Task.Disable(ctx.Request().Context(), id); err != nil {
   118  		return c.ERROR(ctx, err)
   119  	}
   120  
   121  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
   122  }
   123  
   124  // ImportTask ...
   125  func (c ControllerAutomation) AutomationServiceImportTask(ctx echo.Context, _ stub.AutomationServiceImportTaskParams) error {
   126  
   127  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
   128  }