Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#65 admin/promo/create #103

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
78 changes: 78 additions & 0 deletions services/admin/promo/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package promo_test

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

"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"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) {
return
}
}

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()},
},
)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add an example in the test, like other endpoints has? That way we always have simple examples for all endpoints that can be copy pasted if someone wants to play around.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have added an example of creating a promo request, following the example.


func ExampleService_Create() {
client, _ := misskey.NewClientWithOptions(misskey.WithSimpleConfig("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN")))

err := client.Admin().Promo().Create(promo.CreateRequest{
NoteID: "8dsk7x47y3",
ExpiresAt: time.Now().Add(24 * time.Hour).Unix(),
})
if err != nil {
log.Printf("[Admin/Promo/Create] %s", err)

return
}
}
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)
}

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