diff --git a/app/object-storage/object-storage.go b/app/object-storage/object-storage.go index f4a4528843292fbcfc0d8256c95cfe9a215b7b59..74f3db7e0382b624e2883df60524bf3a132dbdf0 100644 --- a/app/object-storage/object-storage.go +++ b/app/object-storage/object-storage.go @@ -11,12 +11,19 @@ import ( "log" "math/rand" . "node/types" + . "node/utilities" "os" "strconv" "time" ) -func addTransactionToBlobStorage(transaction Transaction, database Database, os Blob) Database { +// AddTransactionToBlobStorage Add transaction to blob storage if it isn't in the database already and returns the database +func AddTransactionToBlobStorage(transaction Transaction, database Database, os Blob) Database { + for _, transactionInDatabase := range database { + if transactionInDatabase.Id == transaction.Id { + return database + } + } database = append(database, transaction) writeDatabaseToBlobStorage(database, os) return database @@ -33,7 +40,7 @@ func fakeTransaction(transactionToFake Transaction, fakeTransaction Transaction, return database } -func readDatabaseFromBlobStorage(blob Blob) Database { +func ReadDatabaseFromBlobStorage(blob Blob) Database { ctx := context.Background() // Download the blob @@ -208,18 +215,6 @@ func ListBlobs(blob Blob) { } } -func PrintingDatabaseToConsole(database Database) { - for i, trans := range database { - fmt.Println("--------------------") - fmt.Printf("\nListing transactions number %d in database\n", i) - fmt.Printf("Transaction id: %s\n", trans.Id) - fmt.Printf("Transaction sender: %s\n", trans.Sender) - fmt.Printf("Transaction receiver: %s\n", trans.Receiver) - fmt.Printf("Transaction amount: %s\n", trans.Amount) - fmt.Println("--------------------") - } -} - func TestObjectStorage() { ctx := context.Background() // url := "https://hepiadistributedsystems.blob.core.windows.net/" //replace <StorageAccountName> with your Azure storage account name @@ -240,11 +235,11 @@ func TestObjectStorage() { Receiver: "1", Amount: "1000", } - db = addTransactionToBlobStorage(t1, db, objectStorage) - db = addTransactionToBlobStorage(t2, db, objectStorage) + db = AddTransactionToBlobStorage(t1, db, objectStorage) + db = AddTransactionToBlobStorage(t2, db, objectStorage) fmt.Println("Listing files in container") - db1 := readDatabaseFromBlobStorage(objectStorage) + db1 := ReadDatabaseFromBlobStorage(objectStorage) PrintingDatabaseToConsole(db1) fmt.Println("Press enter to continue") bufio.NewReader(os.Stdin).ReadBytes('\n') @@ -259,7 +254,7 @@ func TestObjectStorage() { db = fakeTransaction(t1, t3, db) writeDatabaseToBlobStorage(db, objectStorage) fmt.Println("Listing files in container") - db2 := readDatabaseFromBlobStorage(objectStorage) + db2 := ReadDatabaseFromBlobStorage(objectStorage) PrintingDatabaseToConsole(db2) fmt.Println("Press enter to continue") bufio.NewReader(os.Stdin).ReadBytes('\n')