/
licenseinfo.go
96 lines (79 loc) · 3.64 KB
/
licenseinfo.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
// Copyright 2022 European Digital Reading Lab. All rights reserved.
// Use of this source code is governed by a BSD-style license
// specified in the Github project LICENSE file.
package stor
import (
"time"
"github.com/go-playground/validator/v10"
"gorm.io/gorm"
)
// LicenseInfo data model
// Note: the date of issue of the license is handled by the gorm model.
// but the license is not logically updated when a device registers,
// therefore we keep the Updated property, which must be maintained "by hand".
type LicenseInfo struct {
gorm.Model
Updated *time.Time `json:"updated,omitempty"` // see comment above
UUID string `json:"uuid" validate:"required,uuid" gorm:"uniqueIndex"`
Provider string `json:"provider" validate:"required,url"`
UserID string `json:"user_id,omitempty" validate:"required" gorm:"index"`
Start *time.Time `json:"start,omitempty"`
End *time.Time `json:"end,omitempty"`
MaxEnd *time.Time `json:"max_end,omitempty"`
Copy int32 `json:"copy,omitempty"`
Print int32 `json:"print,omitempty"`
Status string `json:"status" validate:"oneof=ready active expired cancelled revoked" gorm:"index"`
StatusUpdated *time.Time `json:"status_updated,omitempty"`
DeviceCount int `json:"device_count"`
PublicationID string `json:"publication_id" validate:"required,uuid"` // implicit foreign key to the related publication
Publication Publication `gorm:"references:UUID" validate:"-"` // the license belongs to the publication
}
// Validate checks required fields and values
func (l *LicenseInfo) Validate() error {
validate := validator.New()
return validate.Struct(l)
}
func (s licenseStore) ListAll() (*[]LicenseInfo, error) {
licenses := []LicenseInfo{}
// security: limited to 1000 results
return &licenses, s.db.Limit(1000).Order("id ASC").Find(&licenses).Error
}
func (s licenseStore) List(pageNum, pageSize int) (*[]LicenseInfo, error) {
licenses := []LicenseInfo{}
// pageNum starts at 1
// result sorted to assure the same order for each request
return &licenses, s.db.Offset((pageNum - 1) * pageSize).Limit(pageSize).Order("id ASC").Find(&licenses).Error
}
func (s licenseStore) FindByUser(userID string) (*[]LicenseInfo, error) {
licenses := []LicenseInfo{}
return &licenses, s.db.Limit(1000).Where("user_id= ?", userID).Find(&licenses).Error
}
func (s licenseStore) FindByPublication(publicationID string) (*[]LicenseInfo, error) {
licenses := []LicenseInfo{}
return &licenses, s.db.Limit(1000).Where("publication_id= ?", publicationID).Find(&licenses).Error
}
func (s licenseStore) FindByStatus(status string) (*[]LicenseInfo, error) {
licenses := []LicenseInfo{}
return &licenses, s.db.Limit(1000).Where("status= ?", status).Find(&licenses).Error
}
func (s licenseStore) FindByDeviceCount(min int, max int) (*[]LicenseInfo, error) {
licenses := []LicenseInfo{}
return &licenses, s.db.Limit(1000).Where("device_count >= ? AND device_count <= ?", min, max).Find(&licenses).Error
}
func (s licenseStore) Count() (int64, error) {
var count int64
return count, s.db.Model(LicenseInfo{}).Count(&count).Error
}
func (s licenseStore) Get(uuid string) (*LicenseInfo, error) {
var license LicenseInfo
return &license, s.db.Where("uuid = ?", uuid).First(&license).Error
}
func (s licenseStore) Create(newLicense *LicenseInfo) error {
return s.db.Create(newLicense).Error
}
func (s licenseStore) Update(changedLicense *LicenseInfo) error {
return s.db.Omit("Publication").Save(changedLicense).Error
}
func (s licenseStore) Delete(deletedLicense *LicenseInfo) error {
return s.db.Delete(deletedLicense).Error
}