github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/controllers/user.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/e154/smart-home/common/apperr"
    24  	"github.com/labstack/echo/v4"
    25  
    26  	m "github.com/e154/smart-home/models"
    27  )
    28  
    29  // ControllerUser ...
    30  type ControllerUser struct {
    31  	*ControllerCommon
    32  }
    33  
    34  // NewControllerUser ...
    35  func NewControllerUser(common *ControllerCommon) *ControllerUser {
    36  	return &ControllerUser{
    37  		ControllerCommon: common,
    38  	}
    39  }
    40  
    41  // AddUser ...
    42  func (c ControllerUser) UserServiceAddUser(ctx echo.Context, _ stub.UserServiceAddUserParams) error {
    43  
    44  	obj := &stub.ApiNewtUserRequest{}
    45  	if err := c.Body(ctx, obj); err != nil {
    46  		return c.ERROR(ctx, err)
    47  	}
    48  
    49  	user := c.dto.User.AddUserRequest(obj)
    50  
    51  	if obj.Password == obj.PasswordRepeat {
    52  		_ = user.SetPass(obj.Password)
    53  	}
    54  
    55  	currentUser, err := c.currentUser(ctx)
    56  	if err != nil {
    57  		return c.ERROR(ctx, apperr.ErrInternal)
    58  	}
    59  
    60  	var userF *m.User
    61  	userF, err = c.endpoint.User.Add(ctx.Request().Context(), user, currentUser)
    62  	if err != nil {
    63  		return c.ERROR(ctx, err)
    64  	}
    65  
    66  	return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.User.ToUserFull(userF)))
    67  }
    68  
    69  // GetUserById ...
    70  func (c ControllerUser) UserServiceGetUserById(ctx echo.Context, id int64) error {
    71  
    72  	user, err := c.endpoint.User.GetById(ctx.Request().Context(), id)
    73  	if err != nil {
    74  		return c.ERROR(ctx, err)
    75  	}
    76  
    77  	return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.User.ToUserFull(user)))
    78  }
    79  
    80  // UpdateUserById ...
    81  func (c ControllerUser) UserServiceUpdateUserById(ctx echo.Context, id int64, _ stub.UserServiceUpdateUserByIdParams) error {
    82  
    83  	obj := &stub.UserServiceUpdateUserByIdJSONBody{}
    84  	if err := c.Body(ctx, obj); err != nil {
    85  		return c.ERROR(ctx, err)
    86  	}
    87  
    88  	user := c.dto.User.UpdateUserByIdRequest(obj, id)
    89  
    90  	if obj.Password != obj.PasswordRepeat {
    91  		return c.ERROR(ctx, apperr.ErrPassNotValid)
    92  	}
    93  
    94  	if obj.Password != "" {
    95  		_ = user.SetPass(obj.Password)
    96  	}
    97  
    98  	user, err := c.endpoint.User.Update(ctx.Request().Context(), user)
    99  	if err != nil {
   100  		return c.ERROR(ctx, err)
   101  	}
   102  
   103  	return c.HTTP200(ctx, ResponseWithObj(ctx, c.dto.User.ToUserFull(user)))
   104  }
   105  
   106  // GetUserList ...
   107  func (c ControllerUser) UserServiceGetUserList(ctx echo.Context, params stub.UserServiceGetUserListParams) error {
   108  
   109  	pagination := c.Pagination(params.Page, params.Limit, params.Sort)
   110  	items, total, err := c.endpoint.User.GetList(ctx.Request().Context(), pagination)
   111  	if err != nil {
   112  		return c.ERROR(ctx, err)
   113  	}
   114  
   115  	return c.HTTP200(ctx, ResponseWithList(ctx, c.dto.User.ToListResult(items), total, pagination))
   116  }
   117  
   118  // DeleteUserById ...
   119  func (c ControllerUser) UserServiceDeleteUserById(ctx echo.Context, id int64) error {
   120  
   121  	if err := c.endpoint.User.Delete(ctx.Request().Context(), id); err != nil {
   122  		return c.ERROR(ctx, err)
   123  	}
   124  
   125  	return c.HTTP200(ctx, ResponseWithObj(ctx, struct{}{}))
   126  }