Add functions for model POST API (#4)

django
Etzelia 3 years ago committed by Gitea
parent 2bd753efa7
commit 0ae5f0c704
  1. 22
      internal/common.go
  2. 13
      model/alert.go
  3. 13
      model/application.go
  4. 13
      model/ip.go
  5. 36
      model/model.go
  6. 13
      model/note.go
  7. 13
      model/player.go
  8. 13
      model/ticket.go
  9. 13
      model/ticketnote.go
  10. 13
      model/usersettings.go
  11. 14
      web/alert.go
  12. 14
      web/discord.go

@ -1,11 +1,17 @@
package internal
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
type Status struct {
Success bool `json:"success"`
Message string `json:"message"`
}
func ResponseGet(endpoint string) ([]byte, error) {
resp, err := http.Get(endpoint)
if err != nil {
@ -36,7 +42,17 @@ func ResponsePost(endpoint string, form url.Values) ([]byte, error) {
return body, err
}
type Status struct {
Success bool `json:"success"`
Message string `json:"message"`
func ResponseStatus(endpoint string, form url.Values) (*Status, error) {
resp, err := ResponsePost(endpoint, form)
if err != nil {
return nil, err
}
status := &Status{}
err = json.Unmarshal(resp, &status)
if err != nil {
return nil, err
}
return status, nil
}

@ -13,10 +13,12 @@ type Alert struct {
Seen bool `json:"seen"`
Date string `json:"date"`
Link string `json:"link"`
model *Model `json:"-"`
}
func (q *Model) Alert(builder *django.Builder) ([]*Alert, error) {
endpoint := q.endpoint("alert", builder.QueryString())
endpoint := q.queryEndpoint("alert", builder.QueryString())
alerts := make([]*Alert, 0)
resp, err := internal.ResponseGet(endpoint)
@ -29,5 +31,14 @@ func (q *Model) Alert(builder *django.Builder) ([]*Alert, error) {
return nil, err
}
for _, alert := range alerts {
alert.model = q
}
return alerts, nil
}
func (a *Alert) Save() (*internal.Status, error) {
endpoint := a.model.endpoint("alert")
return internal.ResponseStatus(endpoint, a.model.Values(a))
}

@ -18,10 +18,12 @@ type Application struct {
Accepted bool `json:"accepted"`
Date string `json:"date"`
Link string `json:"link"`
model *Model `json:"-"`
}
func (q *Model) Application(builder *django.Builder) ([]*Application, error) {
endpoint := q.endpoint("application", builder.QueryString())
endpoint := q.queryEndpoint("application", builder.QueryString())
applications := make([]*Application, 0)
resp, err := internal.ResponseGet(endpoint)
@ -34,5 +36,14 @@ func (q *Model) Application(builder *django.Builder) ([]*Application, error) {
return nil, err
}
for _, application := range applications {
application.model = q
}
return applications, nil
}
func (a *Application) Save() (*internal.Status, error) {
endpoint := a.model.endpoint("application")
return internal.ResponseStatus(endpoint, a.model.Values(a))
}

@ -11,10 +11,12 @@ type IP struct {
PlayerID int64 `json:"player_id"`
LastUsed string `json:"last_used"`
Link string `json:"link"`
model *Model `json:"-"`
}
func (q *Model) IP(builder *django.Builder) ([]*IP, error) {
endpoint := q.endpoint("ip", builder.QueryString())
endpoint := q.queryEndpoint("ip", builder.QueryString())
ips := make([]*IP, 0)
resp, err := internal.ResponseGet(endpoint)
@ -27,5 +29,14 @@ func (q *Model) IP(builder *django.Builder) ([]*IP, error) {
return nil, err
}
for _, ip := range ips {
ip.model = q
}
return ips, nil
}
func (i *IP) Save() (*internal.Status, error) {
endpoint := i.model.endpoint("ip")
return internal.ResponseStatus(endpoint, i.model.Values(i))
}

@ -3,6 +3,8 @@ package model
import (
"fmt"
"go.etztech.xyz/go-mcm/model/django"
"net/url"
"reflect"
"strings"
)
@ -22,9 +24,39 @@ func (q *Model) NewDjangoBuilder() *django.Builder {
return django.NewBuilder(q.Token)
}
func (q *Model) endpoint(model, query string) string {
func (q *Model) endpoint(model string) string {
return fmt.Sprintf("%s/%s/", q.URL, model)
}
func (q *Model) queryEndpoint(model, query string) string {
if !strings.HasPrefix(query, "?") {
query = "?" + query
}
return fmt.Sprintf("%s/%s%s", q.URL, model, query)
return fmt.Sprintf("%s%s", q.endpoint(model), query)
}
func (q *Model) Values(i interface{}) url.Values {
values := url.Values{}
values.Set("api", q.Token)
iVal := reflect.ValueOf(i).Elem()
typ := iVal.Type()
for i := 0; i < iVal.NumField(); i++ {
tag := typ.Field(i).Tag.Get("json")
if tag != "-" {
values.Set(strings.ToLower(tag), PyConvert(iVal.Field(i)))
}
}
return values
}
func PyConvert(value reflect.Value) string {
switch value.Kind() {
case reflect.Bool:
if fmt.Sprint(value) == "true" {
return "True"
}
return "False"
default:
return fmt.Sprint(value)
}
}

@ -22,10 +22,12 @@ type Note struct {
StaffID int64 `json:"staff_id"`
Date string `json:"date"`
Link string `json:"link"`
model *Model `json:"-"`
}
func (q *Model) Note(builder *django.Builder) ([]*Note, error) {
endpoint := q.endpoint("note", builder.QueryString())
endpoint := q.queryEndpoint("note", builder.QueryString())
notes := make([]*Note, 0)
resp, err := internal.ResponseGet(endpoint)
@ -38,5 +40,14 @@ func (q *Model) Note(builder *django.Builder) ([]*Note, error) {
return nil, err
}
for _, note := range notes {
note.model = q
}
return notes, nil
}
func (n *Note) Save() (*internal.Status, error) {
endpoint := n.model.endpoint("note")
return internal.ResponseStatus(endpoint, n.model.Values(n))
}

@ -15,10 +15,12 @@ type Player struct {
FirstSeen string `json:"first_seen"`
LastSeen string `json:"last_seen"`
Link string `json:"link"`
model *Model `json:"-"`
}
func (q *Model) Player(builder *django.Builder) ([]*Player, error) {
endpoint := q.endpoint("player", builder.QueryString())
endpoint := q.queryEndpoint("player", builder.QueryString())
players := make([]*Player, 0)
resp, err := internal.ResponseGet(endpoint)
@ -31,5 +33,14 @@ func (q *Model) Player(builder *django.Builder) ([]*Player, error) {
return nil, err
}
for _, player := range players {
player.model = q
}
return players, nil
}
func (p *Player) Save() (*internal.Status, error) {
endpoint := p.model.endpoint("player")
return internal.ResponseStatus(endpoint, p.model.Values(p))
}

@ -35,10 +35,12 @@ type Ticket struct {
Z string `json:"Z"`
Date string `json:"date"`
Link string `json:"link"`
model *Model `json:"-"`
}
func (q *Model) Ticket(builder *django.Builder) ([]*Ticket, error) {
endpoint := q.endpoint("ticket", builder.QueryString())
endpoint := q.queryEndpoint("ticket", builder.QueryString())
tickets := make([]*Ticket, 0)
resp, err := internal.ResponseGet(endpoint)
@ -51,5 +53,14 @@ func (q *Model) Ticket(builder *django.Builder) ([]*Ticket, error) {
return nil, err
}
for _, ticket := range tickets {
ticket.model = q
}
return tickets, nil
}
func (t *Ticket) Save() (*internal.Status, error) {
endpoint := t.model.endpoint("ticket")
return internal.ResponseStatus(endpoint, t.model.Values(t))
}

@ -14,10 +14,12 @@ type TicketNote struct {
LastUpdate string `json:"last_update"`
Date string `json:"date"`
Link string `json:"link"`
model *Model `json:"-"`
}
func (q *Model) TicketNote(builder *django.Builder) ([]*TicketNote, error) {
endpoint := q.endpoint("ticketnote", builder.QueryString())
endpoint := q.queryEndpoint("ticketnote", builder.QueryString())
ticketNotes := make([]*TicketNote, 0)
resp, err := internal.ResponseGet(endpoint)
@ -30,5 +32,14 @@ func (q *Model) TicketNote(builder *django.Builder) ([]*TicketNote, error) {
return nil, err
}
for _, ticketNote := range ticketNotes {
ticketNote.model = q
}
return ticketNotes, nil
}
func (t *TicketNote) Save() (*internal.Status, error) {
endpoint := t.model.endpoint("ticketnote")
return internal.ResponseStatus(endpoint, t.model.Values(t))
}

@ -35,10 +35,12 @@ type UserSettings struct {
ShowTimestampChat bool `json:"show_timestamp_chat"`
LastIP string `json:"last_ip"`
Link string `json:"link"`
model *Model `json:"-"`
}
func (q *Model) UserSettings(builder *django.Builder) ([]*UserSettings, error) {
endpoint := q.endpoint("usersettings", builder.QueryString())
endpoint := q.queryEndpoint("usersettings", builder.QueryString())
userSettingss := make([]*UserSettings, 0)
resp, err := internal.ResponseGet(endpoint)
@ -51,5 +53,14 @@ func (q *Model) UserSettings(builder *django.Builder) ([]*UserSettings, error) {
return nil, err
}
for _, userSetting := range userSettingss {
userSetting.model = q
}
return userSettingss, nil
}
func (u *UserSettings) Save() (*internal.Status, error) {
endpoint := u.model.endpoint("usersettings")
return internal.ResponseStatus(endpoint, u.model.Values(u))
}

@ -1,14 +1,12 @@
package web
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"net/url"
)
func (web *Web) Alert(message string, ping bool) (*internal.Status, error) {
endpoint := web.endpoint("alert")
status := &internal.Status{}
form := url.Values{}
form.Add("api", web.Token)
@ -17,15 +15,5 @@ func (web *Web) Alert(message string, ping bool) (*internal.Status, error) {
form.Add("ping", "True")
}
resp, err := internal.ResponsePost(endpoint, form)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &status)
if err != nil {
return nil, err
}
return status, nil
return internal.ResponseStatus(endpoint, form)
}

@ -1,14 +1,12 @@
package web
import (
"encoding/json"
"go.etztech.xyz/go-mcm/internal"
"net/url"
)
func (web *Web) Discord(message string, ping bool) (*internal.Status, error) {
endpoint := web.endpoint("discord")
status := &internal.Status{}
form := url.Values{}
form.Add("api", web.Token)
@ -17,15 +15,5 @@ func (web *Web) Discord(message string, ping bool) (*internal.Status, error) {
form.Add("ping", "True")
}
resp, err := internal.ResponsePost(endpoint, form)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &status)
if err != nil {
return nil, err
}
return status, nil
return internal.ResponseStatus(endpoint, form)
}

Loading…
Cancel
Save