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

abc #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

abc #70

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
Binary file added cmd/bot/__debug_bin
Binary file not shown.
4 changes: 1 addition & 3 deletions internal/app/commands/demo/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ type DemoCommander struct {
subdomainCommander Commander
}

func NewDemoCommander(
bot *tgbotapi.BotAPI,
) *DemoCommander {
func NewDemoCommander(bot *tgbotapi.BotAPI) *DemoCommander {
return &DemoCommander{
bot: bot,
// subdomainCommander
Expand Down
4 changes: 1 addition & 3 deletions internal/app/commands/demo/subdomain/commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ type DemoSubdomainCommander struct {
subdomainService *subdomain.Service
}

func NewDemoSubdomainCommander(
bot *tgbotapi.BotAPI,
) *DemoSubdomainCommander {
func NewDemoSubdomainCommander(bot *tgbotapi.BotAPI) *DemoSubdomainCommander {
subdomainService := subdomain.NewService()

return &DemoSubdomainCommander{
Expand Down
46 changes: 46 additions & 0 deletions internal/app/commands/education/commander.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package education

import (
"log"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/ozonmp/omp-bot/internal/app/commands/education/subject"
"github.com/ozonmp/omp-bot/internal/app/path"
)

type Commander interface {
HandleCallback(callback *tgbotapi.CallbackQuery, callbackPath path.CallbackPath)
HandleCommand(msg *tgbotapi.Message, commandPath path.CommandPath)
}
// ...
type EducationCommander struct {
bot *tgbotapi.BotAPI
// subjectCommander router.Commander
subjectCommander Commander
}

func NewEducationCommander(bot *tgbotapi.BotAPI) *EducationCommander {
commander := subject.NewSubjectCommander(bot)
return &EducationCommander{
bot: bot,
subjectCommander: commander,
}
}

func (c *EducationCommander) HandleCallback(callback *tgbotapi.CallbackQuery, callbackPath path.CallbackPath) {
switch callbackPath.Subdomain {
case "subject":
c.subjectCommander.HandleCallback(callback, callbackPath)
default:
log.Printf("EducationCommander.HandleCallback: unknown subdomain - %s", callbackPath.Subdomain)
}
}

func (c *EducationCommander) HandleCommand(msg *tgbotapi.Message, commandPath path.CommandPath) {
switch commandPath.Subdomain {
case "subject":
c.subjectCommander.HandleCommand(msg, commandPath)
default:
log.Printf("EducationCommander.HandleCommand: unknown subdomain - %s", commandPath.Subdomain)
}
}
40 changes: 40 additions & 0 deletions internal/app/commands/education/subject/callback_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package subject

import (
"encoding/json"
"fmt"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/ozonmp/omp-bot/internal/app/path"
)

func (c *SubjectCommander) CallbackList(callback *tgbotapi.CallbackQuery, callbackPath path.CallbackPath) {
chatID := callback.Message.Chat.ID
var cursorData CursorData
if err := json.Unmarshal([]byte(callbackPath.CallbackData), &cursorData); err != nil {
msg := tgbotapi.NewMessage(
chatID,
fmt.Sprintf("Cannot parse cursor data from user message: %v", err),
)
c.sendMessage(msg)
return
}

msgText, keyboard, err := getPaginatedMessage(c.subjectService, cursorData.Cursor, DefaultSubjectPerPage)
if err != nil {
msg := tgbotapi.NewMessage(
chatID,
fmt.Sprintf("Error when getting paginated data: %v", err),
)
c.sendMessage(msg)
return
}

msg := tgbotapi.NewEditMessageText(
chatID,
callback.Message.MessageID,
msgText,
)
msg.ReplyMarkup = keyboard
c.editMessage(msg)
}
15 changes: 15 additions & 0 deletions internal/app/commands/education/subject/command_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package subject

import (
"fmt"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)

func (c *SubjectCommander) Default(inputMessage *tgbotapi.Message) {
msg := tgbotapi.NewMessage(
inputMessage.Chat.ID,
fmt.Sprintf("Unknown command %s, type /help__education_subject for help", inputMessage.Text),
)
c.sendMessage(msg)
}
32 changes: 32 additions & 0 deletions internal/app/commands/education/subject/command_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package subject

import (
"fmt"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)

func (c *SubjectCommander) Delete(inputMessage *tgbotapi.Message) {
chatID := inputMessage.Chat.ID
commandArgs := inputMessage.CommandArguments()
subjectID, err := c.parseIdOrSendError(commandArgs, chatID, "to delete subject by")
if err != nil {
return
}

_, err = c.subjectService.Remove(subjectID)
if err != nil {
msg := tgbotapi.NewMessage(
chatID,
fmt.Sprintf("cannot delete subject by id %v: %v", subjectID, err),
)
c.sendMessage(msg)
return
}

msg := tgbotapi.NewMessage(
chatID,
"Successfully removed subject",
)
c.sendMessage(msg)
}
51 changes: 51 additions & 0 deletions internal/app/commands/education/subject/command_edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package subject

import (
"fmt"
"strings"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/ozonmp/omp-bot/internal/service/education/subject"
)

func (c *SubjectCommander) Edit(inputMessage *tgbotapi.Message) {
chatID := inputMessage.Chat.ID
commandArgs := strings.Split(inputMessage.CommandArguments(), " ")

if !c.validateArgumentsCountOrSendError(
commandArgs,
4,
chatID,
"<id> <owner_id> <subject_id> <title>",
) {
return
}

subjectID, err := c.parseIdOrSendError(commandArgs[0], chatID, "to edit subject by")
if err != nil {
return
}
ownerID, err := c.parseIdOrSendError(commandArgs[1], chatID, "of owner")
if err != nil {
return
}
educationID, err := c.parseIdOrSendError(commandArgs[2], chatID, "of subject")
if err != nil {
return
}

newTitle := commandArgs[3]
newSubject := subject.NewSubject(subjectID, ownerID, educationID, newTitle)

err = c.subjectService.Update(subjectID, *newSubject)
if err != nil {
msg := tgbotapi.NewMessage(
chatID,
fmt.Sprintf("Cannot edit subject by id %v: %v", subjectID, err),
)
c.sendMessage(msg)
return
}
msg := tgbotapi.NewMessage(chatID, "Successfully updated subject")
c.sendMessage(msg)
}
33 changes: 33 additions & 0 deletions internal/app/commands/education/subject/command_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package subject

import (
"fmt"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)

func (c *SubjectCommander) Get(inputMessage *tgbotapi.Message) {
chatID := inputMessage.Chat.ID
commandArgs := inputMessage.CommandArguments()

subjectID, err := c.parseIdOrSendError(commandArgs, chatID, "to get subject by id")
if err != nil {
return
}

subject, err := c.subjectService.Describe(subjectID)
if err != nil {
msg := tgbotapi.NewMessage(
chatID,
fmt.Sprintf("Cannot describe subject by id, id: %v, err: %v", subjectID, err),
)
c.sendMessage(msg)
return
}

msg := tgbotapi.NewMessage(
chatID,
subject.String(),
)
c.sendMessage(msg)
}
16 changes: 16 additions & 0 deletions internal/app/commands/education/subject/command_help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package subject

import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"

func (c *SubjectCommander) Help(inputMessage *tgbotapi.Message) {
msg := tgbotapi.NewMessage(
inputMessage.Chat.ID,
"/help__education_subject - get help\n"+
"/new__education__subject <owner_id> <subject_id> <title> - create new subject and receive its id\n"+
"/get__education__subject <id> - get subject by id\n"+
"/edit__education__subject <id> <owner> <subject_id> <title> - edit subject by id\n"+
"/delete__education__subject <id> - delete subject by id\n"+
"/list__education__subject - list all subject with pagination",
)
c.sendMessage(msg)
}
23 changes: 23 additions & 0 deletions internal/app/commands/education/subject/command_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package subject

import (
"fmt"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)

func (c *SubjectCommander) List(inputMessage *tgbotapi.Message) {
chatID := inputMessage.Chat.ID
msgText, keyboard, err := getPaginatedMessage(c.subjectService, 0, DefaultSubjectPerPage)
if err != nil {
msg := tgbotapi.NewMessage(
chatID,
fmt.Sprintf("Cannot /list command: %v", err),
)
c.sendMessage(msg)
return
}
msg := tgbotapi.NewMessage(chatID, msgText)
msg.ReplyMarkup = keyboard
c.sendMessage(msg)
}
51 changes: 51 additions & 0 deletions internal/app/commands/education/subject/command_new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package subject

import (
"fmt"
"strings"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/ozonmp/omp-bot/internal/service/education/subject"
)

func (c *SubjectCommander) New(inputMessage *tgbotapi.Message) {
chatID := inputMessage.Chat.ID
commandArgs := strings.Split(inputMessage.CommandArguments(), " ")

if !c.validateArgumentsCountOrSendError(
commandArgs,
3,
chatID,
"<owner_id> <subject_id> <title>",
) {
return
}

ownerID, err := c.parseIdOrSendError(commandArgs[0], chatID, "of owner")
if err != nil {
return
}
subjectID, err := c.parseIdOrSendError(commandArgs[1], chatID, "of subject")
if err != nil {
return
}

title := commandArgs[2]

newSubject := subject.NewSubject(0, ownerID, subjectID, title)
newID, err := c.subjectService.Create(*newSubject)
if err != nil {
msg := tgbotapi.NewMessage(
chatID,
fmt.Sprintf("Couldn't create new subject: %v", err),
)
c.sendMessage(msg)
return
}

msg := tgbotapi.NewMessage(
chatID,
fmt.Sprintf("Successfully created new subject, ID is %v", newID),
)
c.sendMessage(msg)
}
59 changes: 59 additions & 0 deletions internal/app/commands/education/subject/commander.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package subject

import (
"log"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/ozonmp/omp-bot/internal/app/path"
"github.com/ozonmp/omp-bot/internal/service/education/subject"
)

type SubjectService interface {
Describe(subjectID uint64) (*subject.Subject, error)
List(cursor uint64, limit uint64) ([]subject.Subject, error)
Create(subject.Subject) (uint64, error)
Update(subjectID uint64, subject subject.Subject) error
Remove(subjectID uint64) (bool, error)
SubjectsCount() uint64
}

type SubjectCommander struct {
bot *tgbotapi.BotAPI
subjectService SubjectService
}

func NewSubjectCommander(bot *tgbotapi.BotAPI) *SubjectCommander {
service := subject.NewDummyService()
return &SubjectCommander{
bot: bot,
subjectService: service,
}
}

func (commander *SubjectCommander) HandleCallback(callback *tgbotapi.CallbackQuery, callbackPath path.CallbackPath) {
switch callbackPath.CallbackName {
case "list":
commander.CallbackList(callback, callbackPath)
default:
log.Printf("EducationCommander.HandleCallback: unknown callback name: %s", callbackPath.CallbackName)
}
}

func (commander *SubjectCommander) HandleCommand(msg *tgbotapi.Message, commandPath path.CommandPath) {
switch commandPath.CommandName {
case "help":
commander.Help(msg)
case "get":
commander.Get(msg)
case "new":
commander.New(msg)
case "edit":
commander.Edit(msg)
case "delete":
commander.Delete(msg)
case "list":
commander.List(msg)
default:
commander.Default(msg)
}
}
Loading