all repos — emessage @ 0cbb6ef61f1c6f3f3a199c14add57b3daed63684

The EMessage email client

contact.go (view raw)

 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
 97
 98
 99
 100
 101
package main

import (
	"strings"
	"time"
	"errors"

	"gorm.io/gorm"
)

type Contact struct {
	gorm.Model
	Firstname       string
	Lastname        string
	Association     string
	Job             string
	Emails          []EmailAddress
	PhoneNumbers    []PhoneNumber
	Urls            []URL
	StreetAddresses []StreetAddress
	Dates           []Date
	Other           []OtherField
	Notes           string
}

func (contact Contact) Validate() error {
	if len(strings.TrimSpace(contact.Firstname)) == 0 &&
		len(strings.TrimSpace(contact.Lastname)) == 0 {
		return errors.New("Not a valid name")
	}
	return nil
}

func (app *App) GetUserContact() Contact {
	return app.UserContact
}

func (app *App) SaveContact(contact Contact) error {
	if err := contact.Validate(); err != nil {
		return err
	}
	if contact.ID == 1 {
		app.UserContact = contact
	}
	app.DB.Save(contact)
	return nil
}

type EmailAddress struct {
	gorm.Model
	ContactID uint
	Key       string

	Value string
}

type PhoneNumber struct {
	gorm.Model
	ContactID uint
	Key       string

	Value string
}

type URL struct {
	gorm.Model
	ContactID uint
	Key       string

	Value string
}

type StreetAddress struct {
	gorm.Model
	ContactID uint
	Key       string

	Street1 string
	Street2 string
	City    string
	State   string
	ZipCode string
	Country string
}

type Date struct {
	gorm.Model
	ContactID uint
	Key       string

	Value time.Time
}

type OtherField struct {
	gorm.Model
	ContactID uint
	Key       string

	Value string
}