base creation
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
package api
|
||||
|
||||
type API struct {
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type ApplicationConfig struct {
|
||||
Port int64
|
||||
}
|
||||
|
||||
func LoadApplicationConfig() (error, *ApplicationConfig) {
|
||||
|
||||
err := godotenv.Load()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
|
||||
err, portString := getVar("PORT", "8080", true)
|
||||
if err != nil {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
port, err := strconv.ParseInt(portString, 10, 64)
|
||||
if err != nil {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
return nil, &ApplicationConfig{
|
||||
Port: port,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func getVar(varName string, defaultValue string, isRequired bool) (error, string) {
|
||||
varValue := os.Getenv(varName)
|
||||
|
||||
if varValue == "" {
|
||||
|
||||
if isRequired {
|
||||
return fmt.Errorf("%s is missing and required : ", varName), ""
|
||||
}
|
||||
|
||||
return nil, defaultValue
|
||||
|
||||
}
|
||||
|
||||
return nil, varValue
|
||||
|
||||
}
|
||||
@@ -1,14 +1,19 @@
|
||||
package main
|
||||
package entities
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Document struct {
|
||||
DocumentUUID *string
|
||||
DocumentName string
|
||||
DocumentType int
|
||||
}
|
||||
|
||||
const (
|
||||
ERROR_DOCUMENT_TYPE = "ERROR_DOCUMENT_TYPE"
|
||||
ERROR_DOCUMENT_MOVE = "ERROR_DOCUMENT_MOVE"
|
||||
ERROR_DOCUMENT_NOT_PERCISTED = "ERROR_DOCUMENT_NOT_PERCISTED"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -0,0 +1 @@
|
||||
package entities
|
||||
@@ -0,0 +1,11 @@
|
||||
package entities
|
||||
|
||||
type User struct {
|
||||
Username string
|
||||
}
|
||||
|
||||
func NewUser(username string) (error, *User) {
|
||||
return nil, &User{
|
||||
Username: username,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
|
||||
// err, config := config.LoadApplicationConfig()
|
||||
|
||||
// if err != nil {
|
||||
// log.Fatalln(err)
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -3,16 +3,18 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/vaultdrop/backend/entities"
|
||||
)
|
||||
|
||||
func TestUserCreation(t *testing.T) {
|
||||
|
||||
err, userAntoine := NewUser("antoine")
|
||||
err, userAntoine := entities.NewUser("antoine")
|
||||
if err != nil {
|
||||
t.Errorf(`Error creating antoine user %v`, err)
|
||||
}
|
||||
|
||||
err, userBob := NewUser("bob")
|
||||
err, userBob := entities.NewUser("bob")
|
||||
if err != nil {
|
||||
t.Errorf(`Error creating bob user %v`, err)
|
||||
}
|
||||
@@ -23,12 +25,12 @@ func TestUserCreation(t *testing.T) {
|
||||
|
||||
func TestCreateDocument(t *testing.T) {
|
||||
|
||||
err, document := NewDocument("paper.pdf", FILE)
|
||||
err, document := entities.NewDocument("paper.pdf", entities.FILE)
|
||||
if err != nil {
|
||||
t.Errorf(`Error creating document %v`, err)
|
||||
}
|
||||
|
||||
err, directory := NewDocument("bob", DIRECTORY)
|
||||
err, directory := entities.NewDocument("bob", entities.DIRECTORY)
|
||||
if err != nil {
|
||||
t.Errorf(`Error creating bob directory %v`, err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/vaultdrop/backend/entities"
|
||||
)
|
||||
|
||||
func (s *Services) MoveDocumentIntoDocument(from *entities.Document, to *entities.Document) error {
|
||||
|
||||
if from.DocumentUUID == nil {
|
||||
return fmt.Errorf(entities.ERROR_DOCUMENT_NOT_PERCISTED)
|
||||
}
|
||||
|
||||
if canEdit, _ := UserCanEditDocument(s.ConnectedUser, from); canEdit == false {
|
||||
return fmt.Errorf("You can not edit this folder")
|
||||
}
|
||||
|
||||
if canEdit, _ := UserCanEditDocument(s.ConnectedUser, to); canEdit == false {
|
||||
return fmt.Errorf("You can not edit this folder")
|
||||
}
|
||||
|
||||
// Check if document exist and move the document into document if is directory
|
||||
|
||||
fmt.Println("To implement move to ", to.DocumentUUID)
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/vaultdrop/backend/entities"
|
||||
)
|
||||
|
||||
func TestMoveDocument(t *testing.T) {
|
||||
|
||||
err, userAntoine := entities.NewUser("antoine")
|
||||
if err != nil {
|
||||
t.Errorf(`Error creating antoine user %v`, err)
|
||||
}
|
||||
|
||||
err, document := entities.NewDocument("test.pdf", entities.FILE)
|
||||
|
||||
err, folder := entities.NewDocument("orga", entities.DIRECTORY)
|
||||
|
||||
service := New(userAntoine)
|
||||
|
||||
err := service.MoveDocumentIntoDocument(document, folder)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package services
|
||||
|
||||
import "github.com/vaultdrop/backend/entities"
|
||||
|
||||
// import "github.com/vaultdrop/backend/entities"
|
||||
|
||||
type Services struct {
|
||||
ConnectedUser *entities.User
|
||||
}
|
||||
|
||||
func New(connectedUser *entities.User) *Services {
|
||||
|
||||
return &Services{
|
||||
ConnectedUser: connectedUser,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func UserCanReadDocument(connectedUser *entities.User, document *entities.Document) (bool, string) {
|
||||
return true, ""
|
||||
}
|
||||
|
||||
func UserCanEditDocument(connectedUser *entities.User, document *entities.Document) (bool, string) {
|
||||
return true, ""
|
||||
}
|
||||
|
||||
func UserCanDeleteDocument(connectedUser *entities.User, document *entities.Document) (bool, string) {
|
||||
return true, ""
|
||||
}
|
||||
|
||||
func UserCanCreateDocument(connectedUser *entities.User, document *entities.Document) (bool, string) {
|
||||
return true, ""
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package services
|
||||
|
||||
func (s *Services) UploadDocument() {
|
||||
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package main
|
||||
|
||||
type User struct {
|
||||
Username string
|
||||
}
|
||||
|
||||
func NewUser(username string) (error, User) {
|
||||
return nil, User{
|
||||
Username: username,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user