-
Notifications
You must be signed in to change notification settings - Fork 3
/
accounts.go
89 lines (72 loc) · 1.92 KB
/
accounts.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
package main
import (
"errors"
"keyring-desktop/database"
"keyring-desktop/utils"
)
func (a *App) UpdateAccountName(id int, name string) error {
return database.UpdateCardName(a.sqlite, id, name)
}
// check if there is card paired already
func (a *App) CurrentAccount() (*CardInfo, error) {
utils.Sugar.Info("Check if there is smart card paired")
cards, err := database.QueryCurrentCard(a.sqlite)
if err != nil {
utils.Sugar.Error(err)
return nil, errors.New("failed to query current card")
}
if len(cards) != 0 {
return a.cardInfo(&cards[0]), nil
}
cards, err = database.QueryAllCards(a.sqlite)
if err != nil {
utils.Sugar.Error(err)
return nil, errors.New("failed to query all the cards")
}
if len(cards) != 0 {
err = database.UpdateCurrentCard(a.sqlite, cards[0].Id)
if err != nil {
utils.Sugar.Error(err)
return nil, errors.New("failed to update current card")
}
return a.cardInfo(&cards[0]), nil
}
return nil, nil
}
func (a *App) GetAllAccounts() ([]CardInfo, error) {
cards, err := database.QueryAllCards(a.sqlite)
if err != nil {
utils.Sugar.Error(err)
return nil, errors.New("failed to query all cards")
}
var res []CardInfo
for _, card := range cards {
info := a.cardInfo(&card)
res = append(res, *info)
}
return res, nil
}
func (a *App) SwitchAccount(cardId int) (*CardInfo, error) {
utils.Sugar.Infof("Switch account to: %v", cardId)
err := database.UpdateCurrentCard(a.sqlite, cardId)
if err != nil {
utils.Sugar.Error(err)
return nil, errors.New("failed to switch account")
}
cards, err := database.QueryCurrentCard(a.sqlite)
if err != nil {
utils.Sugar.Error(err)
return nil, errors.New("failed to query current card")
}
if len(cards) == 0 {
return nil, errors.New("current card is not set")
}
return a.cardInfo(&cards[0]), nil
}
func (a *App) cardInfo(card *database.Card) *CardInfo {
info := CardInfo{
Id: card.Id,
Name: card.Name,
}
return &info
}