-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.go
More file actions
79 lines (64 loc) · 2.39 KB
/
model.go
File metadata and controls
79 lines (64 loc) · 2.39 KB
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
package main
import (
"errors"
"time"
"github.com/jinzhu/gorm"
)
const (
PaymentStatusOpen = iota
PaymentStatusDelayed
PaymentStatusOK
)
type Subscription struct {
CPF string `json:"cpf" gorm:"column:cpf"`
Owner string `json:"owner" gorm:"column:owner"`
Address string `json:"address"`
DeliveryAddress string `json:"delivery_address"`
CreditCardNumber string `json:"credit_card" gorm:"column:credit_card"`
Email string `json:"email" gorm:"column:email;primary_key"`
Phone string `json:"phone"`
}
func (subs *Subscription) UpdateByEmail(email string, db *gorm.DB) error {
return db.Where("email = ?", email).Save(subs).Error
}
func NewSubscription(cpf, address, deliveryAddress, creditCardNumber, email, phone string) (*Subscription, error) {
if cpf == "" || email == "" || deliveryAddress == "" || creditCardNumber == "" {
return nil, errors.New("Invalid Input")
}
return &Subscription{CPF: cpf, Address: address, DeliveryAddress: deliveryAddress, CreditCardNumber: creditCardNumber, Email: email, Phone: phone}, nil
}
type Subscriber struct {
Base
PatientId int `json:"patient_id"`
Email string `json:"email" gorm:"column:email;primary_key"`
LastPayed time.Time `json:"last_payed"`
NextPayment time.Time `json:"next_payment"`
PaymentStatus int `json:"payment_status" gorm:"column:payment_status;default:0"`
}
func NewSubscriber(patientId int, email string, lastPayed, nextPayment time.Time, paymentStatus int) (*Subscriber, error) {
if email == "" {
return nil, errors.New("Invalid Input")
}
return &Subscriber{PatientId: patientId, Email: email, LastPayed: lastPayed, NextPayment: nextPayment, PaymentStatus: paymentStatus}, nil
}
func (s *Subscriber) RetrieveSubscriber(db *gorm.DB) (*Subscriber, error) {
subscribers := []Subscriber{}
if err := db.Where(s).Find(&subscribers).Error; err != nil {
return nil, err
}
if len(subscribers) != 1 {
return nil, errors.New("[FATAL] Database is inconsistent. More then one subscriber associated with the same pacient_id")
}
return &subscribers[0], nil
}
func (s *Subscriber) Retrieve(db *gorm.DB) ([]Subscriber, error) {
subs := []Subscriber{}
err := db.Where(s).Find(&subs, s.Base.BuildQuery()).Error
return subs, err
}
func (s *Subscriber) Update(db *gorm.DB) error {
return db.Save(s).Error
}
type Payer interface {
Pay(*Subscriber, Subscription) error
}