You've already forked focalboard
							
							
				mirror of
				https://github.com/mattermost/focalboard.git
				synced 2025-10-31 00:17:42 +02:00 
			
		
		
		
	Reorganizing app layer and adding a test using mocks
This commit is contained in:
		| @@ -1,15 +1,6 @@ | |||||||
| package app | package app | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"crypto/rand" |  | ||||||
| 	"errors" |  | ||||||
| 	"fmt" |  | ||||||
| 	"io" |  | ||||||
| 	"log" |  | ||||||
| 	"path/filepath" |  | ||||||
| 	"strings" |  | ||||||
|  |  | ||||||
| 	"github.com/mattermost/mattermost-octo-tasks/server/model" |  | ||||||
| 	"github.com/mattermost/mattermost-octo-tasks/server/services/config" | 	"github.com/mattermost/mattermost-octo-tasks/server/services/config" | ||||||
| 	"github.com/mattermost/mattermost-octo-tasks/server/services/store" | 	"github.com/mattermost/mattermost-octo-tasks/server/services/store" | ||||||
| 	"github.com/mattermost/mattermost-octo-tasks/server/ws" | 	"github.com/mattermost/mattermost-octo-tasks/server/ws" | ||||||
| @@ -26,105 +17,3 @@ type App struct { | |||||||
| func New(config *config.Configuration, store store.Store, wsServer *ws.WSServer, filesBackend filesstore.FileBackend) *App { | func New(config *config.Configuration, store store.Store, wsServer *ws.WSServer, filesBackend filesstore.FileBackend) *App { | ||||||
| 	return &App{config: config, store: store, wsServer: wsServer, filesBackend: filesBackend} | 	return &App{config: config, store: store, wsServer: wsServer, filesBackend: filesBackend} | ||||||
| } | } | ||||||
|  |  | ||||||
| func (a *App) GetBlocks(parentID string, blockType string) ([]model.Block, error) { |  | ||||||
| 	if len(blockType) > 0 && len(parentID) > 0 { |  | ||||||
| 		return a.store.GetBlocksWithParentAndType(parentID, blockType) |  | ||||||
| 	} |  | ||||||
| 	if len(blockType) > 0 { |  | ||||||
| 		return a.store.GetBlocksWithType(blockType) |  | ||||||
| 	} |  | ||||||
| 	return a.store.GetBlocksWithParent(parentID) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (a *App) GetParentID(blockID string) (string, error) { |  | ||||||
| 	return a.store.GetParentID(blockID) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (a *App) InsertBlock(block model.Block) error { |  | ||||||
| 	return a.store.InsertBlock(block) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (a *App) InsertBlocks(blocks []model.Block) error { |  | ||||||
| 	var blockIDsToNotify = []string{} |  | ||||||
| 	uniqueBlockIDs := make(map[string]bool) |  | ||||||
|  |  | ||||||
| 	for _, block := range blocks { |  | ||||||
| 		if !uniqueBlockIDs[block.ID] { |  | ||||||
| 			blockIDsToNotify = append(blockIDsToNotify, block.ID) |  | ||||||
| 		} |  | ||||||
| 		if len(block.ParentID) > 0 && !uniqueBlockIDs[block.ParentID] { |  | ||||||
| 			blockIDsToNotify = append(blockIDsToNotify, block.ParentID) |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		err := a.store.InsertBlock(block) |  | ||||||
| 		if err != nil { |  | ||||||
| 			return err |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	a.wsServer.BroadcastBlockChangeToWebsocketClients(blockIDsToNotify) |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (a *App) GetSubTree(blockID string) ([]model.Block, error) { |  | ||||||
| 	return a.store.GetSubTree(blockID) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (a *App) GetAllBlocks() ([]model.Block, error) { |  | ||||||
| 	return a.store.GetAllBlocks() |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (a *App) DeleteBlock(blockID string) error { |  | ||||||
| 	var blockIDsToNotify = []string{blockID} |  | ||||||
| 	parentID, err := a.GetParentID(blockID) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if len(parentID) > 0 { |  | ||||||
| 		blockIDsToNotify = append(blockIDsToNotify, parentID) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	err = a.store.DeleteBlock(blockID) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	a.wsServer.BroadcastBlockChangeToWebsocketClients(blockIDsToNotify) |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (a *App) SaveFile(reader io.Reader, filename string) (string, error) { |  | ||||||
| 	// NOTE: File extension includes the dot |  | ||||||
| 	fileExtension := strings.ToLower(filepath.Ext(filename)) |  | ||||||
| 	if fileExtension == ".jpeg" { |  | ||||||
| 		fileExtension = ".jpg" |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	createdFilename := fmt.Sprintf(`%s%s`, createGUID(), fileExtension) |  | ||||||
|  |  | ||||||
| 	_, appErr := a.filesBackend.WriteFile(reader, createdFilename) |  | ||||||
| 	if appErr != nil { |  | ||||||
| 		return "", errors.New("unable to store the file in the files storage") |  | ||||||
| 	} |  | ||||||
| 	return fmt.Sprintf(`%s/files/%s`, a.config.ServerRoot, createdFilename), nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (a *App) GetFilePath(filename string) string { |  | ||||||
| 	folderPath := a.config.FilesPath |  | ||||||
| 	return filepath.Join(folderPath, filename) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // CreateGUID returns a random GUID |  | ||||||
| func createGUID() string { |  | ||||||
| 	b := make([]byte, 16) |  | ||||||
| 	_, err := rand.Read(b) |  | ||||||
| 	if err != nil { |  | ||||||
| 		log.Fatal(err) |  | ||||||
| 	} |  | ||||||
| 	uuid := fmt.Sprintf("%x-%x-%x-%x-%x", |  | ||||||
| 		b[0:4], b[4:6], b[6:8], b[8:10], b[10:]) |  | ||||||
|  |  | ||||||
| 	return uuid |  | ||||||
| } |  | ||||||
|   | |||||||
							
								
								
									
										73
									
								
								server/app/blocks.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								server/app/blocks.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,73 @@ | |||||||
|  | package app | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"github.com/mattermost/mattermost-octo-tasks/server/model" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func (a *App) GetBlocks(parentID string, blockType string) ([]model.Block, error) { | ||||||
|  | 	if len(blockType) > 0 && len(parentID) > 0 { | ||||||
|  | 		return a.store.GetBlocksWithParentAndType(parentID, blockType) | ||||||
|  | 	} | ||||||
|  | 	if len(blockType) > 0 { | ||||||
|  | 		return a.store.GetBlocksWithType(blockType) | ||||||
|  | 	} | ||||||
|  | 	return a.store.GetBlocksWithParent(parentID) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (a *App) GetParentID(blockID string) (string, error) { | ||||||
|  | 	return a.store.GetParentID(blockID) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (a *App) InsertBlock(block model.Block) error { | ||||||
|  | 	return a.store.InsertBlock(block) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (a *App) InsertBlocks(blocks []model.Block) error { | ||||||
|  | 	var blockIDsToNotify = []string{} | ||||||
|  | 	uniqueBlockIDs := make(map[string]bool) | ||||||
|  |  | ||||||
|  | 	for _, block := range blocks { | ||||||
|  | 		if !uniqueBlockIDs[block.ID] { | ||||||
|  | 			blockIDsToNotify = append(blockIDsToNotify, block.ID) | ||||||
|  | 		} | ||||||
|  | 		if len(block.ParentID) > 0 && !uniqueBlockIDs[block.ParentID] { | ||||||
|  | 			blockIDsToNotify = append(blockIDsToNotify, block.ParentID) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		err := a.store.InsertBlock(block) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	a.wsServer.BroadcastBlockChangeToWebsocketClients(blockIDsToNotify) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (a *App) GetSubTree(blockID string) ([]model.Block, error) { | ||||||
|  | 	return a.store.GetSubTree(blockID) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (a *App) GetAllBlocks() ([]model.Block, error) { | ||||||
|  | 	return a.store.GetAllBlocks() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (a *App) DeleteBlock(blockID string) error { | ||||||
|  | 	var blockIDsToNotify = []string{blockID} | ||||||
|  | 	parentID, err := a.GetParentID(blockID) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if len(parentID) > 0 { | ||||||
|  | 		blockIDsToNotify = append(blockIDsToNotify, parentID) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	err = a.store.DeleteBlock(blockID) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	a.wsServer.BroadcastBlockChangeToWebsocketClients(blockIDsToNotify) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
							
								
								
									
										34
									
								
								server/app/blocks_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								server/app/blocks_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | |||||||
|  | package app | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"errors" | ||||||
|  | 	"testing" | ||||||
|  |  | ||||||
|  | 	"github.com/golang/mock/gomock" | ||||||
|  | 	"github.com/mattermost/mattermost-octo-tasks/server/services/config" | ||||||
|  | 	"github.com/mattermost/mattermost-octo-tasks/server/services/store/mockstore" | ||||||
|  | 	"github.com/mattermost/mattermost-octo-tasks/server/ws" | ||||||
|  | 	"github.com/mattermost/mattermost-server/v5/services/filesstore/mocks" | ||||||
|  | 	"github.com/stretchr/testify/require" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func TestGetParentID(t *testing.T) { | ||||||
|  | 	ctrl := gomock.NewController(t) | ||||||
|  | 	defer ctrl.Finish() | ||||||
|  | 	store := mockstore.NewMockStore(ctrl) | ||||||
|  | 	wsserver := ws.NewWSServer() | ||||||
|  | 	app := New(&config.Configuration{}, store, wsserver, &mocks.FileBackend{}) | ||||||
|  | 	t.Run("success query", func(t *testing.T) { | ||||||
|  | 		store.EXPECT().GetParentID(gomock.Eq("test-id")).Return("test-parent-id", nil) | ||||||
|  | 		result, err := app.GetParentID("test-id") | ||||||
|  | 		require.NoError(t, err) | ||||||
|  | 		require.Equal(t, "test-parent-id", result) | ||||||
|  | 	}) | ||||||
|  | 	t.Run("fail query", func(t *testing.T) { | ||||||
|  | 		store.EXPECT().GetParentID(gomock.Eq("test-id")).Return("", errors.New("block-not-found")) | ||||||
|  | 		_, err := app.GetParentID("test-id") | ||||||
|  | 		require.Error(t, err) | ||||||
|  | 		require.Equal(t, "block-not-found", err.Error()) | ||||||
|  | 	}) | ||||||
|  |  | ||||||
|  | } | ||||||
							
								
								
									
										45
									
								
								server/app/files.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								server/app/files.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,45 @@ | |||||||
|  | package app | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"crypto/rand" | ||||||
|  | 	"errors" | ||||||
|  | 	"fmt" | ||||||
|  | 	"io" | ||||||
|  | 	"log" | ||||||
|  | 	"path/filepath" | ||||||
|  | 	"strings" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func (a *App) SaveFile(reader io.Reader, filename string) (string, error) { | ||||||
|  | 	// NOTE: File extension includes the dot | ||||||
|  | 	fileExtension := strings.ToLower(filepath.Ext(filename)) | ||||||
|  | 	if fileExtension == ".jpeg" { | ||||||
|  | 		fileExtension = ".jpg" | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	createdFilename := fmt.Sprintf(`%s%s`, createGUID(), fileExtension) | ||||||
|  |  | ||||||
|  | 	_, appErr := a.filesBackend.WriteFile(reader, createdFilename) | ||||||
|  | 	if appErr != nil { | ||||||
|  | 		return "", errors.New("unable to store the file in the files storage") | ||||||
|  | 	} | ||||||
|  | 	return fmt.Sprintf(`%s/files/%s`, a.config.ServerRoot, createdFilename), nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (a *App) GetFilePath(filename string) string { | ||||||
|  | 	folderPath := a.config.FilesPath | ||||||
|  | 	return filepath.Join(folderPath, filename) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // CreateGUID returns a random GUID | ||||||
|  | func createGUID() string { | ||||||
|  | 	b := make([]byte, 16) | ||||||
|  | 	_, err := rand.Read(b) | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	uuid := fmt.Sprintf("%x-%x-%x-%x-%x", | ||||||
|  | 		b[0:4], b[4:6], b[6:8], b[8:10], b[10:]) | ||||||
|  |  | ||||||
|  | 	return uuid | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user