Passed
Push — main ( f6597a...58b626 )
by Yume
01:31 queued 12s
created

domain.*Deck.ToPublicDeck   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
package domain
2
3
import (
4
	"github.com/go-playground/validator/v10"
5
	"github.com/memnix/memnix-rest/pkg/random"
6
	"gorm.io/gorm"
7
)
8
9
type Deck struct {
10
	gorm.Model  `swaggerignore:"true"`
11
	Name        string     `json:"name"`
12
	Description string     `json:"description"`
13
	Lang        string     `json:"lang"`
14
	Key         string     `json:"key"`
15
	Banner      string     `json:"banner"`
16
	Learners    []*User    ` json:"-" gorm:"many2many:user_decks;"`
17
	OwnerID     uint       `json:"owner_id"`
18
	Status      DeckStatus `json:"status"`
19
}
20
21
type DeckStatus int64
22
23
const (
24
	DeckStatusPrivate DeckStatus = iota
25
	DeckStatusToReview
26
	DeckStatusPublic
27
)
28
29
type PublicDeck struct {
30
	Name        string `json:"name"`
31
	Description string `json:"description"`
32
	Lang        string `json:"lang"`
33
	Banner      string `json:"banner"`
34
	ID          uint   `json:"id"`
35
}
36
37
func (d *Deck) ToPublicDeck() PublicDeck {
38
	return PublicDeck{
39
		ID:          d.ID,
40
		Name:        d.Name,
41
		Description: d.Description,
42
		Lang:        d.Lang,
43
		Banner:      d.Banner,
44
	}
45
}
46
47
func (d *Deck) IsOwner(id uint) bool {
48
	return d.OwnerID == id
49
}
50
51
type CreateDeck struct {
52
	Name        string `json:"name" validate:"required"`
53
	Description string `json:"description" validate:"required"`
54
	Lang        string `json:"lang" validate:"required,len=2,alpha"`
55
	Banner      string `json:"banner" validate:"required,uri"`
56
}
57
58
func (c *CreateDeck) Validate() error {
59
	validate := validator.New()
60
	return validate.Struct(c)
61
}
62
63
func (c *CreateDeck) ToDeck() Deck {
64
	key, _ := random.GenerateSecretCode(10)
65
	return Deck{
66
		Name:        c.Name,
67
		Description: c.Description,
68
		Lang:        c.Lang,
69
		Status:      DeckStatusPrivate,
70
		Key:         key,
71
	}
72
}
73
74
type DeckIndex map[string]interface{}
75