Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 8 additions & 31 deletions core/app/service/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ func (u *HostService) SearchForTree(search dto.SearchForTree) ([]dto.HostTree, e
}

func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
if req.Name == "local" {
return nil, buserr.New("ErrRecordExist")
}
hostItem, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr), hostRepo.WithByUser(req.User), hostRepo.WithByPort(req.Port))
if hostItem.ID != 0 {
return nil, buserr.New("ErrRecordExist")
}

var err error
if len(req.Password) != 0 && req.AuthMode == "password" {
req.Password, err = u.EncryptHost(req.Password)
Expand Down Expand Up @@ -265,37 +273,6 @@ func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
host.GroupID = group.ID
req.GroupID = group.ID
}
var sameHostID uint
if req.Name == "local" {
hostSame, _ := hostRepo.Get(repo.WithByName("local"))
sameHostID = hostSame.ID
} else {
hostSame, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr), hostRepo.WithByUser(req.User), hostRepo.WithByPort(req.Port))
sameHostID = hostSame.ID
}
if sameHostID != 0 {
host.ID = sameHostID
upMap := make(map[string]interface{})
upMap["name"] = req.Name
upMap["group_id"] = req.GroupID
upMap["addr"] = req.Addr
upMap["port"] = req.Port
upMap["user"] = req.User
upMap["auth_mode"] = req.AuthMode
upMap["password"] = req.Password
upMap["private_key"] = req.PrivateKey
upMap["pass_phrase"] = req.PassPhrase
upMap["remember_password"] = req.RememberPassword
upMap["description"] = req.Description
if err := hostRepo.Update(sameHostID, upMap); err != nil {
return nil, err
}
var hostinfo dto.HostInfo
if err := copier.Copy(&hostinfo, &host); err != nil {
return nil, buserr.WithDetail("ErrStructTransform", err.Error(), nil)
}
return &hostinfo, nil
}

if err := hostRepo.Create(&host); err != nil {
return nil, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current version of the code, I have not identified any significant changes that would cause an issue. All minor modifications like variable naming conventions, indentation changes etc are included between two comments to keep lines as short as possible for brevity sake.

I will suggest improving function structure slightly more if it helps reduce redundancy. For example, consider consolidating SearchForTree and Create functions into one which accepts multiple arguments. Additionally, ensure all variables used globally are initialized properly at the beginning of the file rather than declaring them immediately before use each time.

However, none of these changes introduce new issues nor require materialization of any previously unnoticed errors.

All done!

Expand Down
Loading