@@ -20,11 +20,11 @@ type HostService struct{}
2020type IHostService interface {
2121 TestLocalConn (id uint ) bool
2222 TestByInfo (req dto.HostConnTest ) bool
23- GetHostInfo (id uint ) (* model. Host , error )
23+ GetHostByID (id uint ) (* dto. HostInfo , error )
2424 SearchForTree (search dto.SearchForTree ) ([]dto.HostTree , error )
2525 SearchWithPage (search dto.SearchHostWithPage ) (int64 , interface {}, error )
2626 Create (hostDto dto.HostOperate ) (* dto.HostInfo , error )
27- Update (id uint , upMap map [string ]interface {}) error
27+ Update (id uint , upMap map [string ]interface {}) ( * dto. HostInfo , error )
2828 Delete (id []uint ) error
2929
3030 EncryptHost (itemVal string ) (string , error )
@@ -124,33 +124,6 @@ func (u *HostService) TestLocalConn(id uint) bool {
124124 return true
125125}
126126
127- func (u * HostService ) GetHostInfo (id uint ) (* model.Host , error ) {
128- host , err := hostRepo .Get (repo .WithByID (id ))
129- if err != nil {
130- return nil , buserr .New ("ErrRecordNotFound" )
131- }
132- if len (host .Password ) != 0 {
133- host .Password , err = encrypt .StringDecrypt (host .Password )
134- if err != nil {
135- return nil , err
136- }
137- }
138- if len (host .PrivateKey ) != 0 {
139- host .PrivateKey , err = encrypt .StringDecrypt (host .PrivateKey )
140- if err != nil {
141- return nil , err
142- }
143- }
144-
145- if len (host .PassPhrase ) != 0 {
146- host .PassPhrase , err = encrypt .StringDecrypt (host .PassPhrase )
147- if err != nil {
148- return nil , err
149- }
150- }
151- return & host , err
152- }
153-
154127func (u * HostService ) SearchWithPage (req dto.SearchHostWithPage ) (int64 , interface {}, error ) {
155128 var options []global.DBOption
156129 if len (req .Info ) != 0 {
@@ -230,6 +203,48 @@ func (u *HostService) SearchForTree(search dto.SearchForTree) ([]dto.HostTree, e
230203 return datas , err
231204}
232205
206+ func (u * HostService ) GetHostByID (id uint ) (* dto.HostInfo , error ) {
207+ var item dto.HostInfo
208+ var host model.Host
209+ if id == 0 {
210+ host , _ = hostRepo .Get (repo .WithByName ("local" ))
211+ } else {
212+ host , _ = hostRepo .Get (repo .WithByID (id ))
213+ }
214+ if host .ID == 0 {
215+ return nil , buserr .New ("ErrRecordNotFound" )
216+ }
217+ if err := copier .Copy (& item , & host ); err != nil {
218+ return nil , buserr .WithDetail ("ErrStructTransform" , err .Error (), nil )
219+ }
220+ if ! item .RememberPassword {
221+ item .Password = ""
222+ item .PrivateKey = ""
223+ item .PassPhrase = ""
224+ return & item , nil
225+ }
226+ var err error
227+ if len (host .Password ) != 0 {
228+ item .Password , err = encrypt .StringDecrypt (host .Password )
229+ if err != nil {
230+ return nil , err
231+ }
232+ }
233+ if len (host .PrivateKey ) != 0 {
234+ item .PrivateKey , err = encrypt .StringDecrypt (host .PrivateKey )
235+ if err != nil {
236+ return nil , err
237+ }
238+ }
239+ if len (host .PassPhrase ) != 0 {
240+ item .PassPhrase , err = encrypt .StringDecrypt (host .PassPhrase )
241+ if err != nil {
242+ return nil , err
243+ }
244+ }
245+ return & item , err
246+ }
247+
233248func (u * HostService ) Create (req dto.HostOperate ) (* dto.HostInfo , error ) {
234249 if req .Name == "local" {
235250 return nil , buserr .New ("ErrRecordExist" )
@@ -297,8 +312,16 @@ func (u *HostService) Delete(ids []uint) error {
297312 return hostRepo .Delete (repo .WithByIDs (ids ))
298313}
299314
300- func (u * HostService ) Update (id uint , upMap map [string ]interface {}) error {
301- return hostRepo .Update (id , upMap )
315+ func (u * HostService ) Update (id uint , upMap map [string ]interface {}) (* dto.HostInfo , error ) {
316+ if err := hostRepo .Update (id , upMap ); err != nil {
317+ return nil , err
318+ }
319+ hostItem , _ := hostRepo .Get (repo .WithByID (id ))
320+ var hostinfo dto.HostInfo
321+ if err := copier .Copy (& hostinfo , & hostItem ); err != nil {
322+ return nil , buserr .WithDetail ("ErrStructTransform" , err .Error (), nil )
323+ }
324+ return & hostinfo , nil
302325}
303326
304327func (u * HostService ) EncryptHost (itemVal string ) (string , error ) {
@@ -309,3 +332,30 @@ func (u *HostService) EncryptHost(itemVal string) (string, error) {
309332 keyItem , err := encrypt .StringEncrypt (string (privateKey ))
310333 return keyItem , err
311334}
335+
336+ func GetHostInfo (id uint ) (* model.Host , error ) {
337+ host , err := hostRepo .Get (repo .WithByID (id ))
338+ if err != nil {
339+ return nil , buserr .New ("ErrRecordNotFound" )
340+ }
341+ if len (host .Password ) != 0 {
342+ host .Password , err = encrypt .StringDecrypt (host .Password )
343+ if err != nil {
344+ return nil , err
345+ }
346+ }
347+ if len (host .PrivateKey ) != 0 {
348+ host .PrivateKey , err = encrypt .StringDecrypt (host .PrivateKey )
349+ if err != nil {
350+ return nil , err
351+ }
352+ }
353+
354+ if len (host .PassPhrase ) != 0 {
355+ host .PassPhrase , err = encrypt .StringDecrypt (host .PassPhrase )
356+ if err != nil {
357+ return nil , err
358+ }
359+ }
360+ return & host , err
361+ }
0 commit comments