summaryrefslogtreecommitdiff
path: root/src/cardmarket/article.go
blob: bc8ae21ba62605ae5b0b823e2a11d80cefdac7ff (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
package cardmarket

import (
	"bytes"
	"context"
	"encoding/json"
)

type ArticleCondition = string

const (
	Mint        ArticleCondition = "MT"
	NearMint    ArticleCondition = "NM"
	Excellent   ArticleCondition = "EX"
	Good        ArticleCondition = "GD"
	LightPlayed ArticleCondition = "LP"
	Played      ArticleCondition = "PL"
	Poor        ArticleCondition = "PO"
)

type LanguageID = uint8

const (
	English LanguageID = iota + 1
	French
	German
	Spanish
	Italian
	SimplifiedChinese
	Japanese
	Portuguese
	Russian
	Korean
	TraditionalChinese
)

type Article struct {
	ProductID  uint16
	Count      uint8
	LanguageID LanguageID
	Comments   []byte
	Price      float32
	Condition  ArticleCondition
	IsFoil     bool
	IsSigned   bool
	IsAltered  bool
}

func (c *ApiClient) CreateArticle(
	ctx context.Context,
	productID uint16,
	count uint8,
	languageID LanguageID,
	comments []byte,
	price float32,
	condition ArticleCondition,
	isFoil *bool,
	isSigned *bool,
	isAltered *bool,
) (*Article, error) {
	type requestBodyArticle struct {
		ProductID  uint16           `json:"idProduct"`
		Count      uint8            `json:"count"`
		LanguageID LanguageID       `json:"idLanguage"`
		Comments   []byte           `json:"comments"`
		Price      float32          `json:"price"`
		Condition  ArticleCondition `json:"condition"`
		IsFoil     *bool            `json:"isFoil,omitempty"`
		IsSigned   *bool            `json:"isSigned,omitempty"`
		IsAltered  *bool            `json:"isAltered,omitempty"`
	}
	reqBody := []requestBodyArticle{
		{
			ProductID:  productID,
			Count:      count,
			LanguageID: languageID,
			Comments:   comments,
			Price:      price,
			Condition:  condition,
			IsFoil:     isFoil,
			IsSigned:   isSigned,
			IsAltered:  isAltered,
		},
	}

	reqBodyBuf, err := json.Marshal(reqBody)
	if err != nil {
		return nil, err
	}

	resp, err := c.httpClient.PostWithContext(
		ctx,
		c.baseURL+"/stock",
		"application/json",
		bytes.NewReader(reqBodyBuf),
	)
	if err != nil {
		return nil, err
	}

	respBodyBuf := make([]byte, 1024)
	_, err = resp.Body.Read(respBodyBuf)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	type responseBody struct {
		Inserted map[string]string
		Error    string `json:"error"`
	}
	respBody := new(responseBody)
	if err = json.Unmarshal(respBodyBuf, respBody); err != nil {
		return nil, err
	}

	return &Article{}, nil
}