Skip to content

Commit

Permalink
yitsushi#65 add admin/promo/create endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Anderson committed Oct 7, 2023
1 parent 51b8fd6 commit 18a5be5
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
37 changes: 37 additions & 0 deletions services/admin/promo/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package promo

import (
"github.com/yitsushi/go-misskey/core"
)

// CreateRequest is the request object for a Create request.
type CreateRequest struct {
NoteID string `json:"noteId"` // required: <misskey:id>
ExpiresAt int64 `json:"expiresAt"` // required: time.Now().Add(0,86400000,0).Unix()
}

// Validate the request.
func (r CreateRequest) Validate() error {
if r.NoteID == "" {
return core.RequestValidationError{
Request: r,
Message: core.UndefinedRequiredField,
Field: "noteId",
}
}

return nil
}

// Create is the endpoint to create a promo.
func (s *Service) Create(request CreateRequest) error {
err := s.Call(
&core.JSONRequest{
Request: &request,
Path: "/admin/promo/create",
},
&core.EmptyResponse{},
)

return err
}
67 changes: 67 additions & 0 deletions services/admin/promo/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package promo_test

import (
"log"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/services/admin/promo"
"github.com/yitsushi/go-misskey/test"
)

func TestService_Create(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/admin/promo/create",
RequestData: &promo.CreateRequest{},
ResponseFile: "create.json",
StatusCode: http.StatusOK,
})

err := client.Admin().Promo().Create(promo.CreateRequest{
NoteID: "noteID",
ExpiresAt: time.Now().Add(86400 * 24 * time.Hour).Unix(),
})
if !assert.NoError(t, err) {
return
}
}

func TestService_Create_Error(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/admin/promo/create",
RequestData: &promo.CreateRequest{},
ResponseFile: "create.json",
StatusCode: http.StatusOK,
})

// success
err := client.Admin().Promo().Create(promo.CreateRequest{
NoteID: "noteID",
ExpiresAt: time.Now().Add(86400 * 24 * time.Hour).Unix(),
})
if !assert.NoError(t, err) {
log.Printf("[Admin/Promos/Create] %s", err)

return
}

// error

}

func TestPromoRequest_Validate(t *testing.T) {
test.ValidateRequests(
t,
[]core.BaseRequest{
promo.CreateRequest{},
},
[]core.BaseRequest{
promo.CreateRequest{NoteID: "8zwxx3cpy7", ExpiresAt: 0},
promo.CreateRequest{NoteID: "8zwxx3cpy8", ExpiresAt: 0},
promo.CreateRequest{NoteID: "8zwxx3cpy9", ExpiresAt: time.Now().Add(86400 * 24 * time.Hour).Unix()},
},
)
}
1 change: 1 addition & 0 deletions services/admin/promo/fixtures/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
15 changes: 15 additions & 0 deletions services/admin/promo/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package promo

import (
"github.com/yitsushi/go-misskey/core"
)

// Service is the base for all the endpoints on this service.
type Service struct {
Call core.RequestHandlerFunc
}

// NewService creates a new Service instance.
func NewService(requestHandler core.RequestHandlerFunc) *Service {
return &Service{Call: requestHandler}
}
6 changes: 6 additions & 0 deletions services/admin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/yitsushi/go-misskey/services/admin/logs"
"github.com/yitsushi/go-misskey/services/admin/moderation"
"github.com/yitsushi/go-misskey/services/admin/moderators"
"github.com/yitsushi/go-misskey/services/admin/promo"
"github.com/yitsushi/go-misskey/services/admin/queue"
"github.com/yitsushi/go-misskey/services/admin/relays"
"github.com/yitsushi/go-misskey/services/admin/users"
Expand Down Expand Up @@ -73,3 +74,8 @@ func (s *Service) Moderators() *moderators.Service {
func (s *Service) Relays() *relays.Service {
return relays.NewService(s.Call)
}

// Propmo contains all endpoints for promos.
func (s *Service) Promo() *promo.Service {
return promo.NewService(s.Call)
}

0 comments on commit 18a5be5

Please sign in to comment.