forked from devinterface/startersaas-go-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.service.go
74 lines (65 loc) · 2.1 KB
/
account.service.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
package services
import (
"devinterface.com/startersaas-go-api/models"
"github.com/Kamva/mgm/v3"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson"
)
// AccountService struct
type AccountService struct{ BaseService }
func (accountService *AccountService) getCollection() (collection *mgm.Collection) {
coll := mgm.CollectionByName("account")
return coll
}
// ByID function
func (accountService *AccountService) ByID(id interface{}) (account *models.Account, err error) {
account = &models.Account{}
err = accountService.getCollection().FindByID(id, account)
return account, err
}
// OneBy function
func (accountService *AccountService) OneBy(q bson.M) (account *models.Account, err error) {
account = &models.Account{}
err = accountService.getCollection().First(q, account)
return account, err
}
// Update function
func (accountService *AccountService) Update(id interface{}, params interface{}) (updatedAccount *models.Account, err error) {
account := &models.Account{}
err = accountService.getCollection().FindByID(id, account)
if err != nil {
return account, err
}
err = mapstructure.Decode(params, &account)
if err != nil {
return account, err
}
err = accountService.getCollection().Update(account)
return account, err
}
// Create function
func (accountService *AccountService) Create(params interface{}) (createdAccount *models.Account, err error) {
account := &models.Account{}
err = mapstructure.Decode(params, &account)
if err != nil {
return account, err
}
err = accountService.getCollection().Create(account)
return account, err
}
// Delete function
func (accountService *AccountService) Delete(id interface{}) (success bool, err error) {
account := &models.Account{}
err = accountService.getCollection().FindByID(id, account)
if err != nil {
return false, err
}
err = accountService.getCollection().Delete(account)
return err == nil, err
}
// FindBy function
func (accountService *AccountService) FindBy(q bson.M) (accounts []models.Account, err error) {
accounts = []models.Account{}
err = accountService.getCollection().SimpleFind(&accounts, q)
return accounts, err
}