/
transfer.go
98 lines (82 loc) · 2.21 KB
/
transfer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019-2020 Axetroy. All rights reserved. Apache License 2.0.
package host
import (
"errors"
"github.com/axetroy/wsm/internal/app/db"
"github.com/axetroy/wsm/internal/app/exception"
"github.com/axetroy/wsm/internal/app/schema"
"github.com/axetroy/wsm/internal/library/controller"
"github.com/axetroy/wsm/internal/library/helper"
"github.com/jinzhu/gorm"
)
func TransferHost(c *controller.Context) (res schema.Response) {
var (
err error
hostID = c.GetParam("host_id")
userID = c.GetParam("user_id")
tx *gorm.DB
shouldUpdate bool
)
defer func() {
if r := recover(); r != nil {
switch t := r.(type) {
case string:
err = errors.New(t)
case error:
err = t
default:
err = exception.Unknown
}
}
if tx != nil {
if err != nil || !shouldUpdate {
_ = tx.Rollback().Error
} else {
err = tx.Commit().Error
}
}
helper.Response(&res, nil, nil, err)
}()
tx = db.Db.Begin()
hostInfo := db.Host{Id: hostID, OwnerID: c.Uid}
if err := tx.Where(&hostInfo).First(&hostInfo).Error; err != nil {
if err == gorm.ErrRecordNotFound {
err = exception.NoData
}
return
}
userInfo := db.User{Id: userID}
if err = tx.Where(&userInfo).First(&userInfo).Error; err != nil {
if err == gorm.ErrRecordNotFound {
err = exception.NoData
}
return
}
// 更新 owner ID
if err = tx.Where(&db.Host{Id: hostInfo.Id}).Update(&db.Host{OwnerID: userID}).Error; err != nil {
return
}
// 删除原有的 Owner Host Record
if err = tx.Delete(&db.HostRecord{HostID: hostInfo.Id, UserID: c.Uid, Type: db.HostRecordTypeOwner}).Error; err != nil {
return
}
hostRecordInfo := db.HostRecord{HostID: hostID, UserID: userID}
if err = tx.Where(&hostRecordInfo).First(&hostRecordInfo).Error; err != nil {
if err == gorm.ErrRecordNotFound {
// 如果未存在,则创建
if err = tx.Create(db.HostRecord{
HostID: hostID,
UserID: userID,
Type: db.HostRecordTypeOwner,
}).Error; err != nil {
return
}
}
return
}
// 如果已是协作者,则更新资料
if err = tx.Where(&db.HostRecord{HostID: hostID, UserID: userID}).Update(&db.HostRecord{Type: db.HostRecordTypeOwner}).Error; err != nil {
return
}
return
}