/
contacts.go
47 lines (38 loc) · 1.08 KB
/
contacts.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
package transactions
import (
stdliberrors "errors"
"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/google/uuid"
"gorm.io/gorm"
)
func GetContacts(db *gorm.DB, limit int, page int) ([]models.Contact, *errors.Error) {
var contacts []models.Contact
offset := (page - 1) * limit
result := db.Limit(limit).Offset(offset).Find(&contacts)
if result.Error != nil {
return nil, &errors.FailedToGetContacts
}
return contacts, nil
}
func GetContact(db *gorm.DB, id uuid.UUID) (*models.Contact, *errors.Error) {
var contact models.Contact
if err := db.First(&contact, id).Error; err != nil {
if stdliberrors.Is(err, gorm.ErrRecordNotFound) {
return nil, &errors.ContactNotFound
} else {
return nil, &errors.FailedToGetContact
}
}
return &contact, nil
}
func DeleteContact(db *gorm.DB, id uuid.UUID) *errors.Error {
if result := db.Delete(&models.Contact{}, id); result.RowsAffected == 0 {
if result.Error == nil {
return &errors.ContactNotFound
} else {
return &errors.FailedToDeleteContact
}
}
return nil
}