|
6 | 6 |
|
7 | 7 | "github.com/tuihub/librarian/app/sephirah/internal/biz/biztiphereth" |
8 | 8 | "github.com/tuihub/librarian/app/sephirah/internal/data/internal/converter" |
| 9 | + "github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent" |
9 | 10 | "github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/account" |
10 | 11 | "github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/user" |
11 | 12 | "github.com/tuihub/librarian/app/sephirah/internal/model/modeltiphereth" |
@@ -114,16 +115,37 @@ func (t tipherethRepo) GetUser(ctx context.Context, id model.InternalID) (*model |
114 | 115 | return converter.ToBizUser(u), nil |
115 | 116 | } |
116 | 117 |
|
117 | | -func (t tipherethRepo) CreateAccount(ctx context.Context, a modeltiphereth.Account, u model.InternalID) error { |
118 | | - return t.data.db.Account.Create(). |
119 | | - SetBindUserID(u). |
120 | | - SetID(a.ID). |
121 | | - SetPlatform(converter.ToEntAccountPlatform(a.Platform)). |
122 | | - SetPlatformAccountID(a.PlatformAccountID). |
123 | | - SetName(a.Name). |
124 | | - SetAvatarURL(a.AvatarURL). |
125 | | - SetProfileURL(a.ProfileURL). |
126 | | - Exec(ctx) |
| 118 | +func (t tipherethRepo) LinkAccount(ctx context.Context, a modeltiphereth.Account, u model.InternalID) error { |
| 119 | + return t.data.WithTx(ctx, func(tx *ent.Tx) error { |
| 120 | + acc, err := tx.Account.Query().Where( |
| 121 | + account.PlatformEQ(converter.ToEntAccountPlatform(a.Platform)), |
| 122 | + account.PlatformAccountIDEQ(a.PlatformAccountID), |
| 123 | + ).Only(ctx) |
| 124 | + if ent.IsNotFound(err) { |
| 125 | + return t.data.db.Account.Create(). |
| 126 | + SetBindUserID(u). |
| 127 | + SetID(a.ID). |
| 128 | + SetPlatform(converter.ToEntAccountPlatform(a.Platform)). |
| 129 | + SetPlatformAccountID(a.PlatformAccountID). |
| 130 | + SetName(a.Name). |
| 131 | + SetAvatarURL(a.AvatarURL). |
| 132 | + SetProfileURL(a.ProfileURL). |
| 133 | + Exec(ctx) |
| 134 | + } |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + exist, err := acc.QueryBindUser().Exist(ctx) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + if exist { |
| 143 | + return errors.New("account already bound to an user") |
| 144 | + } |
| 145 | + return t.data.db.Account.UpdateOneID(acc.ID). |
| 146 | + SetBindUserID(u). |
| 147 | + Exec(ctx) |
| 148 | + }) |
127 | 149 | } |
128 | 150 |
|
129 | 151 | func (t tipherethRepo) UnLinkAccount(ctx context.Context, a modeltiphereth.Account, u model.InternalID) error { |
|
0 commit comments