Brijesh's Git Server — identity @ 8c36e39b8c6eda064ffa45901a976c3ebf028d97

authentication service

core/internal/models/models.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
 102
 103
 104
 105
 106
package models

import (
	"time"

	"gorm.io/gorm"
)

type Admin struct {
	gorm.Model

	ID        string    `gorm:"primaryKey;default:gen_random_uuid()" json:"ID"`
	CreatedAt time.Time `json:"CreatedAt"`
	UpdatedAt time.Time `json:"UpdatedAt"`

	Email        string `gorm:"uniqueIndex;not null" json:"Email"`
	PasswordHash string `gorm:"not null" json:"PasswordHash"`
	FirstName    string `json:"FirstName"`
	LastName     string `json:"LastName"`

	Applications []Application `gorm:"foreignKey:AdminID" json:"Applications,omitempty"`
}

type Application struct {
	gorm.Model

	ID        string    `gorm:"primaryKey;default:gen_random_uuid()" json:"ID"`
	CreatedAt time.Time `json:"CreatedAt"`
	UpdatedAt time.Time `json:"UpdatedAt"`

	Name        string `gorm:"not null" json:"Name"`
	Description string `json:"Description"`

	AdminID string `gorm:"not null" json:"AdminID"`
	Admin   *Admin `gorm:"foreignKey:AdminID" json:"Admin,omitempty"`

	RefreshToken string `json:"RefreshToken"`

	Users []User `gorm:"foreignKey:ApplicationID" json:"Users,omitempty"`
}

type User struct {
	gorm.Model

	ID        string    `gorm:"primaryKey;default:gen_random_uuid()" json:"ID"`
	CreatedAt time.Time `json:"CreatedAt"`
	UpdatedAt time.Time `json:"UpdatedAt"`

	Email        string `gorm:"uniqueIndex;not null" json:"Email"`
	PasswordHash string `gorm:"not null" json:"PasswordHash"`
	FirstName    string `json:"FirstName"`
	LastName     string `json:"LastName"`

	ApplicationID string       `gorm:"not null" json:"ApplicationID"`
	Application   *Application `gorm:"foreignKey:ApplicationID" json:"Application,omitempty"`
}

type ResponseUser struct {
	ID        string    `gorm:"primaryKey;default:gen_random_uuid()" json:"ID"`
	CreatedAt time.Time `json:"CreatedAt"`
	UpdatedAt time.Time `json:"UpdatedAt"`

	Email     string `gorm:"uniqueIndex;not null" json:"Email"`
	FirstName string `json:"FirstName"`
	LastName  string `json:"LastName"`

	ApplicationID string       `gorm:"not null" json:"ApplicationID"`
	Application   *Application `gorm:"foreignKey:ApplicationID" json:"Application,omitempty"`
}

type ResponseAdmin struct {
	ID        string    `gorm:"primaryKey;default:gen_random_uuid()" json:"ID"`
	CreatedAt time.Time `json:"CreatedAt"`
	UpdatedAt time.Time `json:"UpdatedAt"`

	Email     string `gorm:"uniqueIndex;not null" json:"Email"`
	FirstName string `json:"FirstName"`
	LastName  string `json:"LastName"`

	Applications []Application `gorm:"foreignKey:AdminID" json:"Applications,omitempty"`
}

func (u *User) ToResponseUser() *ResponseUser {
	return &ResponseUser{
		ID:            u.ID,
		CreatedAt:     u.CreatedAt,
		UpdatedAt:     u.UpdatedAt,
		Email:         u.Email,
		FirstName:     u.FirstName,
		LastName:      u.LastName,
		ApplicationID: u.ApplicationID,
		Application:   u.Application,
	}
}

func (a *Admin) ToResponseAdmin() *ResponseAdmin {
	return &ResponseAdmin{
		ID:           a.ID,
		CreatedAt:    a.CreatedAt,
		UpdatedAt:    a.UpdatedAt,
		Email:        a.Email,
		FirstName:    a.FirstName,
		LastName:     a.LastName,
		Applications: a.Applications,
	}
}