1// Copyright 2015 The Gogs Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package gogs
6
7import (
8	"bytes"
9	"encoding/json"
10	"fmt"
11)
12
13type Organization struct {
14	ID          int64  `json:"id"`
15	UserName    string `json:"username"`
16	FullName    string `json:"full_name"`
17	AvatarUrl   string `json:"avatar_url"`
18	Description string `json:"description"`
19	Website     string `json:"website"`
20	Location    string `json:"location"`
21}
22
23func (c *Client) ListMyOrgs() ([]*Organization, error) {
24	orgs := make([]*Organization, 0, 5)
25	return orgs, c.getParsedResponse("GET", "/user/orgs", nil, nil, &orgs)
26}
27
28func (c *Client) ListUserOrgs(user string) ([]*Organization, error) {
29	orgs := make([]*Organization, 0, 5)
30	return orgs, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/orgs", user), nil, nil, &orgs)
31}
32
33func (c *Client) GetOrg(orgname string) (*Organization, error) {
34	org := new(Organization)
35	return org, c.getParsedResponse("GET", fmt.Sprintf("/orgs/%s", orgname), nil, nil, org)
36}
37
38type CreateOrgOption struct {
39	UserName    string `json:"username" binding:"Required"`
40	FullName    string `json:"full_name"`
41	Description string `json:"description"`
42	Website     string `json:"website"`
43	Location    string `json:"location"`
44}
45
46type EditOrgOption struct {
47	FullName    string `json:"full_name"`
48	Description string `json:"description"`
49	Website     string `json:"website"`
50	Location    string `json:"location"`
51}
52
53func (c *Client) CreateOrg(opt CreateOrgOption) (*Organization, error) {
54	body, err := json.Marshal(&opt)
55	if err != nil {
56		return nil, err
57	}
58	org := new(Organization)
59	return org, c.getParsedResponse("POST", "/user/orgs", jsonHeader, bytes.NewReader(body), org)
60}
61
62func (c *Client) EditOrg(orgname string, opt EditOrgOption) error {
63	body, err := json.Marshal(&opt)
64	if err != nil {
65		return err
66	}
67	_, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname), jsonHeader, bytes.NewReader(body))
68	return err
69}
70