Skip to content

Commit

Permalink
feat: sync memo
Browse files Browse the repository at this point in the history
  • Loading branch information
namnhce committed Apr 16, 2024
1 parent 76e6b0d commit 231951f
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/adapter/fortress/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ type FortressAdapter interface {

CheckWithdrawCondition(discordID string) (rs *model.AdapterCheckWithdrawCondition, err error)
GetBanks(id, bin, swiftCode string) (banks *model.AdapterBank, err error)

SyncMemoLogs() (memos *model.MemoLogsResponse, err error)
GetMemoLogs() (memos *model.MemoLogsResponse, err error)
}
62 changes: 62 additions & 0 deletions pkg/adapter/fortress/memo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package fortress

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"

"github.com/dwarvesf/fortress-discord/pkg/model"
)

func (f *Fortress) SyncMemoLogs() (memos *model.MemoLogsResponse, err error) {
req, err := f.makeReq("/api/v1/memos/sync", http.MethodPost, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
var errMsg ErrorMessage
if err := json.NewDecoder(resp.Body).Decode(&errMsg); err != nil {
return nil, errors.New("[SyncMemo] invalid decoded, error " + err.Error())
}
return nil, errors.New("[SyncMemo] invalid call, code " + strconv.Itoa(resp.StatusCode) + " " + errMsg.Message)
}

if err := json.NewDecoder(resp.Body).Decode(&memos); err != nil {
return nil, fmt.Errorf("[GetMemoLogs] invalid decoded, error %v", err.Error())
}

return memos, nil
}

func (f *Fortress) GetMemoLogs() (memos *model.MemoLogsResponse, err error) {
req, err := f.makeReq("/api/v1/memos", http.MethodGet, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("[GetMemoLogs] invalid call, code %v", resp.StatusCode)
}

if err := json.NewDecoder(resp.Body).Decode(&memos); err != nil {
return nil, fmt.Errorf("[GetMemoLogs] invalid decoded, error %v", err.Error())
}

return memos, nil
}
6 changes: 4 additions & 2 deletions pkg/discord/command/memo/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func (e *Memo) Execute(message *model.DiscordMessage) error {
// handle command for 2 args input from user, e.g `?earn list`
switch message.ContentArgs[1] {
case "list":
return e.List(message)
return e.ListMemoLogs(message)
case "sync":
return e.Sync(message)
}

return nil
Expand All @@ -33,7 +35,7 @@ func (e *Memo) Help(message *model.DiscordMessage) error {
}

func (e *Memo) DefaultCommand(message *model.DiscordMessage) error {
return e.List(message)
return e.ListMemoLogs(message)
}

func (e *Memo) PermissionCheck(message *model.DiscordMessage) (bool, []string) {
Expand Down
1 change: 1 addition & 0 deletions pkg/discord/command/memo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type MemoCommander interface {
base.TextCommander

List(message *model.DiscordMessage) error
Sync(message *model.DiscordMessage) error
}
24 changes: 24 additions & 0 deletions pkg/discord/command/memo/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,27 @@ func (t *Memo) List(message *model.DiscordMessage) error {
// 2. render
return t.view.Memo().List(message, data)
}

func (t *Memo) ListMemoLogs(message *model.DiscordMessage) error {
// 1. get data from service
data, err := t.svc.Memo().GetMemoLogs()
if err != nil {
t.L.Error(err, "can't get list of Memo")
return err
}

// 2. render
return t.view.Memo().ListMemoLogs(message, data)
}

func (t *Memo) Sync(message *model.DiscordMessage) error {
// 1. sync
data, err := t.svc.Memo().SyncMemoLogs()
if err != nil {
t.L.Error(err, "can't sync Memo")
return err
}

// 2. render
return t.view.Memo().Sync(message, data)
}
2 changes: 2 additions & 0 deletions pkg/discord/service/memo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import "github.com/dwarvesf/fortress-discord/pkg/model"

type MemoServicer interface {
GetMemos() ([]*model.Memo, error)
SyncMemoLogs() ([]model.MemoLog, error)
GetMemoLogs() ([]model.MemoLog, error)
}
25 changes: 25 additions & 0 deletions pkg/discord/service/memo/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,28 @@ func (e *Memo) GetMemos() ([]*model.Memo, error) {

return memos, nil
}

func (e *Memo) SyncMemoLogs() ([]model.MemoLog, error) {
// sync memos from fortress
memoLogs, err := e.adapter.Fortress().SyncMemoLogs()
if err != nil {
e.l.Error(err, "can't sync memo logs")
return nil, err
}

// normalized into in-app model

return memoLogs.Data, nil

}

func (e *Memo) GetMemoLogs() ([]model.MemoLog, error) {
// get response from fortress
adapterMemoLogs, err := e.adapter.Fortress().GetMemoLogs()
if err != nil {
e.l.Error(err, "can't get memo logs from fortress")
return nil, err
}

return adapterMemoLogs.Data, nil
}
2 changes: 2 additions & 0 deletions pkg/discord/view/memo/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import (

type MemoViewer interface {
List(original *model.DiscordMessage, subs []*model.Memo) error
ListMemoLogs(original *model.DiscordMessage, subs []model.MemoLog) error
Sync(original *model.DiscordMessage, subs []model.MemoLog) error
}
16 changes: 16 additions & 0 deletions pkg/discord/view/memo/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,19 @@ func (e *Memo) List(original *model.DiscordMessage, memos []*model.Memo) error {

return base.SendEmbededMessage(e.ses, original, msg)
}

func (e *Memo) ListMemoLogs(original *model.DiscordMessage, memos []model.MemoLog) error {
var content string
for i := range memos {
if i <= 10 {
content += fmt.Sprintf("%d ・ [%s](%s)\n", i+1, memos[i].Title, memos[i].URL)
}
}

msg := &discordgo.MessageEmbed{
Title: "<:pepeyes:885513213431648266> Latest Memos <:pepeyes:885513213431648266> \n",
Description: content,
}

return base.SendEmbededMessage(e.ses, original, msg)
}
25 changes: 25 additions & 0 deletions pkg/discord/view/memo/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package memo

import (
"fmt"

"github.com/bwmarrin/discordgo"
"github.com/dwarvesf/fortress-discord/pkg/discord/view/base"
"github.com/dwarvesf/fortress-discord/pkg/model"
)

func (e *Memo) Sync(original *model.DiscordMessage, memos []model.MemoLog) error {
var content string
for i := range memos {
if i <= 10 {
content += fmt.Sprintf("%d ・ [%s](%s)\n", i+1, memos[i].Title, memos[i].URL)
}
}

msg := &discordgo.MessageEmbed{
Title: "<:pepeyes:885513213431648266> New Memo <:pepeyes:885513213431648266> \n",
Description: content,
}

return base.SendEmbededMessage(e.ses, original, msg)
}
46 changes: 45 additions & 1 deletion pkg/model/memos.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package model

import "time"
import (
"time"

"github.com/shopspring/decimal"
)

// AdapterMemo is a struct response from adapter, before process to in-app model
type AdapterMemo struct {
Expand All @@ -14,3 +18,43 @@ type Memo struct {
Name string `json:"name"`
CreatedAt *time.Time `json:"created_at"`
}

type CreateMemoRequest struct {
Data []CreateMemoItem `json:"data"`
}

type CreateMemoItem struct {
Title string `json:"title"`
URL string `json:"url"`
Author []Author `json:"author"`
Tags []string `json:"tags"`
PublishedAt string `json:"publishedAt"`
Reward decimal.Decimal `json:"reward"`
}

type Author struct {
GithubID string `json:"githubID"`
DiscordID string `json:"discordID"`
}

type MemoLog struct {
ID string `json:"id"`
Title string `json:"title"`
URL string `json:"url"`
Authors []MemoLogAuthor `json:"authors"`
Description string `json:"description"`
PublishedAt *time.Time `json:"publishedAt"`
Reward decimal.Decimal `json:"reward"`
}

// MemoLogAuthor is the author of the memo log
type MemoLogAuthor struct {
EmployeeID string `json:"employeeID"`
GithubID string `json:"githubID"`
DiscordID string `json:"discordID"`
}

// MemoLogsResponse response for memo logs
type MemoLogsResponse struct {
Data []MemoLog `json:"data"`
}

0 comments on commit 231951f

Please sign in to comment.