Passed
Pull Request — main (#42)
by Yume
01:17
created

models.*Log.ToJSON   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
package models
2
3
import (
4
	"encoding/json"
5
	"fmt"
6
	"github.com/memnix/memnixrest/pkg/database"
7
	"time"
8
)
9
10
type Log struct {
11
	Type      LogType   `json:"type"`
12
	Message   string    `json:"message"`
13
	Event     LogEvent  `json:"event"`
14
	CreatedAt time.Time `json:"createdat"`
15
	UserID    uint      `json:"userid"`
16
	DeckID    uint      `json:"deckid"`
17
	CardID    uint      `json:"cardid"`
18
}
19
20
// SendLog sends log to the rabbitmq channel
21
func (l *Log) SendLog() error {
22
	jsonObject, _ := l.ToJSON()
23
	key := fmt.Sprintf("%s.%s", l.Type, l.Event)
24
25
	err := database.SendMessageToChannel(database.RabbitMqChan, jsonObject, key)
26
	return err
27
}
28
29
// Set Log
30
func (l *Log) Set(logType LogType, message string, event LogEvent, userID, deckID, cardID uint) {
31
	l.Type = logType
32
	l.Message = message
33
	l.Event = event
34
	l.UserID = userID
35
	l.CardID = cardID
36
	l.DeckID = deckID
37
	l.CreatedAt = time.Now()
38
}
39
40
// CreateLog returns a new Log object
41
func CreateLog(message string, event LogEvent) *Log {
42
	return &Log{Message: message, Event: event, CreatedAt: time.Now()}
43
}
44
45
// SetType method
46
func (l *Log) SetType(logType LogType) *Log {
47
	l.Type = logType
48
	return l
49
}
50
51
// AttachIDs method
52
func (l *Log) AttachIDs(userID, deckID, cardID uint) *Log {
53
	if userID != 0 {
54
		l.UserID = userID
55
	}
56
57
	if cardID != 0 {
58
		l.CardID = cardID
59
	}
60
	if deckID != 0 {
61
		l.DeckID = deckID
62
	}
63
64
	return l
65
}
66
67
// ToJSON method
68
func (l *Log) ToJSON() ([]byte, error) {
69
	body, err := json.Marshal(l)
70
	if err != nil {
71
		return nil, err
72
	}
73
	return body, nil
74
}
75
76
type LogType string
77
78
const (
79
	LogTypeInfo    LogType = "info"
80
	LogTypeWarning LogType = "warning"
81
	LogTypeError   LogType = "error"
82
)
83
84
// LogEvent enum type
85
type LogEvent string
86
87
const (
88
	LogUndefined           LogEvent = "undefined"
89
	LogUserLogin           LogEvent = "user.login"
90
	LogUserLogout          LogEvent = "user.logout"
91
	LogUserRegister        LogEvent = "user.register"
92
	LogUserEdit            LogEvent = "user.edit"
93
	LogUserDeleted         LogEvent = "user.deleted"
94
	LogUserPasswordReset   LogEvent = "user.password_reset"
95
	LogUserPasswordChanged LogEvent = "user.password_changed"
96
	LogSubscribe           LogEvent = "user.subscribe"
97
	LogUnsubscribe         LogEvent = "user.unsubscribe"
98
	LogUserDeckLimit       LogEvent = "user.deckLimit"
99
	LogPublishRequest      LogEvent = "deck.publish"
100
	LogDeckCreated         LogEvent = "deck.created"
101
	LogDeckDeleted         LogEvent = "deck.deleted"
102
	LogDeckEdited          LogEvent = "deck.edited"
103
	LogDeckCardLimit       LogEvent = "deck.cardLimit"
104
	LogCardCreated         LogEvent = "card.created"
105
	LogCardDeleted         LogEvent = "card.deleted"
106
	LogCardEdited          LogEvent = "card.edited"
107
	LogAlreadyUsedEmail    LogEvent = "register.usedEmail"
108
	LogIncorrectEmail      LogEvent = "login.incorrectEmail"
109
	LogIncorrectPassword   LogEvent = "login.incorrectPassword"
110
	LogLoginError          LogEvent = "login.error"
111
	LogPermissionForbidden LogEvent = "permission.forbidden"
112
	LogQueryGetError       LogEvent = "query.get"
113
	LogBodyParserError     LogEvent = "query.bodyParser"
114
	LogBadRequest          LogEvent = "query.badRequest"
115
)
116