File size: 1,756 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package requests

import (
	"strconv"
	"strings"

	"github.com/NdoleStudio/httpsms/pkg/entities"

	"github.com/NdoleStudio/httpsms/pkg/repositories"

	"github.com/NdoleStudio/httpsms/pkg/services"
)

// MessageIndex is the payload fetching entities.Message sent between 2 numbers
type MessageIndex struct {
	Skip    string `json:"skip" query:"skip"`
	Contact string `json:"contact" query:"contact"`
	Owner   string `json:"owner" query:"owner"`
	Query   string `json:"query" query:"query"`
	Limit   string `json:"limit" query:"limit"`
}

// Sanitize sets defaults to MessageOutstanding
func (input *MessageIndex) Sanitize() MessageIndex {
	if strings.TrimSpace(input.Limit) == "" {
		input.Limit = "20"
	}

	input.Query = strings.TrimSpace(input.Query)

	input.Owner = input.sanitizeAddress(input.Owner)
	input.Contact = input.sanitizeAddress(input.Contact)

	input.Skip = strings.TrimSpace(input.Skip)
	if input.Skip == "" {
		input.Skip = "0"
	}

	return *input
}

// ToGetParams converts request to services.MessageGetParams
func (input *MessageIndex) ToGetParams(userID entities.UserID) services.MessageGetParams {
	return services.MessageGetParams{
		IndexParams: repositories.IndexParams{
			Skip:  input.getInt(input.Skip),
			Query: input.Query,
			Limit: input.getInt(input.Limit),
		},
		UserID:  userID,
		Owner:   input.Owner,
		Contact: input.Contact,
	}
}

// getLimit gets the take as a string
func (input *MessageIndex) sanitizeAddress(value string) string {
	value = strings.TrimRight(value, " ")
	if len(value) > 0 && value[0] == ' ' {
		value = strings.Replace(value, " ", "+", 1)
	}
	return value
}

// getLimit gets the take as a string
func (input *MessageIndex) getInt(value string) int {
	val, _ := strconv.Atoi(value)
	return val
}