github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/api/dto/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 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  // User ...
    28  type User struct{}
    29  
    30  // NewUserDto ...
    31  func NewUserDto() User {
    32  	return User{}
    33  }
    34  
    35  // AddUserRequest ...
    36  func (u User) AddUserRequest(req *stub.ApiNewtUserRequest) (user *m.User) {
    37  	user = &m.User{}
    38  	_ = common.Copy(&user, req, common.JsonEngine)
    39  	if req.ImageId != nil {
    40  		_ = user.ImageId.Scan(*req.ImageId)
    41  	}
    42  	user.RoleName = req.RoleName
    43  	return
    44  }
    45  
    46  // ToUserFull ...
    47  func (u User) ToUserFull(user *m.User) (result *stub.ApiUserFull) {
    48  	roleDto := NewRoleDto()
    49  	imageDto := NewImageDto()
    50  	result = &stub.ApiUserFull{
    51  		Id:                  user.Id,
    52  		Nickname:            user.Nickname,
    53  		FirstName:           common.String(user.FirstName),
    54  		LastName:            common.String(user.LastName),
    55  		Email:               user.Email,
    56  		Status:              user.Status,
    57  		SignInCount:         user.SignInCount,
    58  		Role:                roleDto.GetStubRole(user.Role),
    59  		RoleName:            user.RoleName,
    60  		Lang:                user.Lang,
    61  		AuthenticationToken: common.StringValue(user.AuthenticationToken),
    62  		CurrentSignInAt:     user.CurrentSignInAt,
    63  		LastSignInAt:        user.LastSignInAt,
    64  		ResetPasswordSentAt: user.ResetPasswordSentAt,
    65  		DeletedAt:           user.DeletedAt,
    66  		CreatedAt:           user.CreatedAt,
    67  		UpdatedAt:           user.UpdatedAt,
    68  	}
    69  
    70  	if user.LastSignInIp != "" {
    71  		result.LastSignInIp = common.String(user.LastSignInIp)
    72  	}
    73  
    74  	if user.CurrentSignInIp != "" {
    75  		result.CurrentSignInIp = common.String(user.CurrentSignInIp)
    76  	}
    77  
    78  	// history
    79  	if user.History != nil {
    80  		result.History = make([]stub.ApiUserHistory, 0, len(user.History))
    81  		for _, h := range user.History {
    82  			result.History = append(result.History, stub.ApiUserHistory{
    83  				Ip:   h.Ip,
    84  				Time: h.Time,
    85  			})
    86  		}
    87  	}
    88  
    89  	// meta
    90  	if user.Meta != nil {
    91  		result.Meta = make([]stub.ApiUserMeta, 0, len(user.Meta))
    92  		for _, m := range user.Meta {
    93  			result.Meta = append(result.Meta, stub.ApiUserMeta{
    94  				Key:   m.Key,
    95  				Value: m.Value,
    96  			})
    97  		}
    98  	}
    99  
   100  	// image
   101  	if user.Image != nil {
   102  		result.Image = imageDto.ToImage(user.Image)
   103  	}
   104  
   105  	// parent
   106  	if user.User != nil {
   107  		result.User = &stub.ApiUserFullParent{
   108  			Id:       user.User.Id,
   109  			Nickname: user.User.Nickname,
   110  		}
   111  	}
   112  	return
   113  }
   114  
   115  // ToUserShot ...
   116  func (u User) ToUserShot(user *m.User) (result *stub.ApiUserShot) {
   117  
   118  	roleDto := NewRoleDto()
   119  	imageDto := NewImageDto()
   120  	result = &stub.ApiUserShot{
   121  		Id:        user.Id,
   122  		Nickname:  user.Nickname,
   123  		FirstName: common.String(user.FirstName),
   124  		LastName:  common.String(user.LastName),
   125  		Email:     user.Email,
   126  		Status:    user.Status,
   127  		Lang:      user.Lang,
   128  		Role:      roleDto.GetStubRole(user.Role),
   129  		RoleName:  user.RoleName,
   130  		CreatedAt: user.CreatedAt,
   131  		UpdatedAt: user.UpdatedAt,
   132  	}
   133  
   134  	// image
   135  	if user.Image != nil {
   136  		result.Image = imageDto.ToImage(user.Image)
   137  	}
   138  
   139  	// parent
   140  	if user.User != nil {
   141  		result.User = &stub.ApiUserShotParent{
   142  			Id:       user.User.Id,
   143  			Nickname: user.User.Nickname,
   144  		}
   145  	}
   146  	return
   147  }
   148  
   149  // ToListResult ...
   150  func (u User) ToListResult(list []*m.User) []*stub.ApiUserShot {
   151  
   152  	items := make([]*stub.ApiUserShot, 0, len(list))
   153  
   154  	for _, i := range list {
   155  		items = append(items, u.ToUserShot(i))
   156  	}
   157  
   158  	return items
   159  }
   160  
   161  // UpdateUserByIdRequest ...
   162  func (u User) UpdateUserByIdRequest(req *stub.UserServiceUpdateUserByIdJSONBody, id int64) (user *m.User) {
   163  	user = &m.User{}
   164  	_ = common.Copy(&user, req, common.JsonEngine)
   165  	if req.ImageId != nil {
   166  		_ = user.ImageId.Scan(*req.ImageId)
   167  	}
   168  	user.Id = id
   169  	return
   170  }