add comments, code refactor

This commit is contained in:
MaysWind
2020-12-23 01:12:57 +08:00
parent 995eeae041
commit 62ef7cbcbe
11 changed files with 115 additions and 8 deletions
+16
View File
@@ -2,8 +2,10 @@ package models
import "github.com/mayswind/lab/pkg/utils"
// TransactionType represents transaction type
type TransactionType byte
// Transaction types
const (
TRANSACTION_TYPE_MODIFY_BALANCE TransactionType = 1
TRANSACTION_TYPE_INCOME TransactionType = 2
@@ -11,6 +13,7 @@ const (
TRANSACTION_TYPE_TRANSFER TransactionType = 4
)
// Transaction represents transaction data stored in database
type Transaction struct {
TransactionId int64 `xorm:"PK"`
Uid int64 `xorm:"UNIQUE(UQE_transaction_uid_transaction_time) INDEX(IDX_transaction_uid_deleted_transaction_time) INDEX(IDX_transaction_uid_deleted_type_time) INDEX(IDX_transaction_uid_deleted_category_id_time) NOT NULL"`
@@ -28,6 +31,7 @@ type Transaction struct {
DeletedUnixTime int64
}
// TransactionCreateRequest represents all parameters of transaction creation request
type TransactionCreateRequest struct {
Type TransactionType `json:"type" binding:"required"`
CategoryId int64 `json:"categoryId,string"`
@@ -40,6 +44,7 @@ type TransactionCreateRequest struct {
Comment string `json:"comment" binding:"max=255"`
}
// TransactionModifyRequest represents all parameters of transaction modification request
type TransactionModifyRequest struct {
Id int64 `json:"id,string" binding:"required,min=1"`
CategoryId int64 `json:"categoryId,string"`
@@ -52,11 +57,13 @@ type TransactionModifyRequest struct {
Comment string `json:"comment" binding:"max=255"`
}
// TransactionListByMaxTimeRequest represents all parameters of transaction listing by max time request
type TransactionListByMaxTimeRequest struct {
MaxTime int64 `form:"max_time" binding:"required,min=1"`
Count int `form:"count" binding:"required,min=1,max=50"`
}
// TransactionListInMonthByPageRequest represents all parameters of transaction listing by month request
type TransactionListInMonthByPageRequest struct {
Year int `form:"year" binding:"required,min=1"`
Month int `form:"month" binding:"required,min=1"`
@@ -64,14 +71,17 @@ type TransactionListInMonthByPageRequest struct {
Count int `form:"count" binding:"required,min=1,max=50"`
}
// TransactionGetRequest represents all parameters of transaction getting request
type TransactionGetRequest struct {
Id int64 `form:"id,string" binding:"required,min=1"`
}
// TransactionDeleteRequest represents all parameters of transaction deleting request
type TransactionDeleteRequest struct {
Id int64 `json:"id,string" binding:"required,min=1"`
}
// TransactionInfoResponse represents a view-object of transaction
type TransactionInfoResponse struct {
Id int64 `json:"id,string"`
TimeSequenceId int64 `json:"timeSequenceId,string"`
@@ -86,11 +96,13 @@ type TransactionInfoResponse struct {
Comment string `json:"comment"`
}
// TransactionInfoPageWrapperResponse represents a response of transaction which contains items and next id
type TransactionInfoPageWrapperResponse struct {
Items TransactionInfoResponseSlice `json:"items"`
NextTimeSequenceId *int64 `json:"nextTimeSequenceId,string"`
}
// ToTransactionInfoResponse returns a view-object according to database model
func (c *Transaction) ToTransactionInfoResponse(tagIds []int64) *TransactionInfoResponse {
return &TransactionInfoResponse{
Id: c.TransactionId,
@@ -107,16 +119,20 @@ func (c *Transaction) ToTransactionInfoResponse(tagIds []int64) *TransactionInfo
}
}
// TransactionInfoResponseSlice represents the slice data structure of TransactionInfoResponse
type TransactionInfoResponseSlice []*TransactionInfoResponse
// Len returns the count of items
func (c TransactionInfoResponseSlice) Len() int {
return len(c)
}
// Swap swaps two items
func (c TransactionInfoResponseSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
// Less reports whether the first item is less than the second one
func (c TransactionInfoResponseSlice) Less(i, j int) bool {
return c[i].Time < c[j].Time
}