Commit e4d81f9a authored by Katarína Pitoňáková's avatar Katarína Pitoňáková
Browse files

minor changes, basic endpoint for changing storage path

parent 13864d4e
Loading
Loading
Loading
Loading
+29 −6
Original line number Diff line number Diff line
@@ -16,7 +16,8 @@ public class FileManagerTests

    private readonly HttpClient _client = new();

    private const string SourceUrl = "https://ark-funds.com/wp-content/uploads/funds-etf-csv/ARK_INNOVATION_ETF_ARKK_HOLDINGS.csv";
    private const string SourceUrl =
        "https://ark-funds.com/wp-content/uploads/funds-etf-csv/ARK_INNOVATION_ETF_ARKK_HOLDINGS.csv";

    private static void CleanDirectory(string path)
    {
@@ -28,6 +29,14 @@ public class FileManagerTests
        }
    }

    private static void RemoveDirectory(string path)
    {
        if (Directory.Exists(path))
        {
            Directory.Delete(path);
        }
    }

    [Test]
    public void Test_When_EmptyStorage_Then_NoRecordsFound()
    {
@@ -89,4 +98,18 @@ public class FileManagerTests
        }

    }

    [Test]
    public void Test_When_StoragePathIsChanged_Then_CorrespondingDirectoryExists()
    {
        var manager = new FileManager(FewSources, _client, SourceUrl);

        const string location = "newLocation";
        manager.SetStorageLocation(location);
        
        DirectoryAssert.Exists(location);
        
        RemoveDirectory(location);
    }

}
 No newline at end of file
+7 −0
Original line number Diff line number Diff line
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using StockAdvisoryModels;
@@ -56,4 +57,10 @@ public class StockParserTests
        Assert.AreEqual(parsedEntries.Count, 2);
        CollectionAssert.AreEquivalent(expectedRecords, parsedEntries);
    }

    [Test]
    public void Test_When_FileNotFound_Then_ExceptionIsThrown()
    {
        Assert.Throws<FileNotFoundException>(() => _stockParser.Parse("csv/non-existing.csv"));
    }
}
 No newline at end of file
+10 −5
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ namespace StockAdvisory;

public class FileManager : IFileManager
{
    public string StoragePath { get; }
    private string _storagePath;

    private readonly string _sourceUrl;
    
@@ -14,8 +14,7 @@ public class FileManager : IFileManager

    public FileManager(string storagePath, HttpClient downloader, string sourceUrl)
    {
        StoragePath = Path.GetFullPath(storagePath);
        Directory.CreateDirectory(StoragePath);
        SetStorageLocation(storagePath);
        _sourceUrl = sourceUrl;
        _httpClient = downloader;
        _httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("StockAdvisory/0.1");
@@ -23,14 +22,14 @@ public class FileManager : IFileManager

    private string ConstructFilePath(string fileName, string ext)
    {
        return StoragePath + Path.DirectorySeparatorChar + fileName + ext;
        return _storagePath + Path.DirectorySeparatorChar + fileName + ext;
    }

    private IEnumerable<string> GetFilesFromStorage()
    {
        try
        {
            return Directory.GetFiles(StoragePath);
            return Directory.GetFiles(_storagePath);
        }         
        catch (IOException e)
        {
@@ -95,4 +94,10 @@ public class FileManager : IFileManager
        var files = GetFilesFromStorage();
        return !files.Contains(filePath) ? null : filePath;
    }

    public void SetStorageLocation(string location)
    {
        _storagePath = Path.GetFullPath(location);
        Directory.CreateDirectory(_storagePath);
    }
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ namespace StockAdvisory;

public interface IFileManager
{
    string StoragePath { get; }
    void SetStorageLocation(string location);
    Task<(string filePath, DateTime dateTime)> DownloadLatest();
    ICollection<DateTime> ListAvailableRecords();
    string? GetRecordFilePath(DateTime record);
+37 −29
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@ namespace StockAdvisory;
public class StockParser : IStockParser
{
    public ICollection<StockEntryRecord> Parse(string filePath)
    {
        try
        {
            using var reader = new StreamReader(filePath);
            using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
@@ -38,11 +40,17 @@ public class StockParser : IStockParser
                    }
                    catch (Exception e)
                    {
                    // Change later to log the exception instead of just writing it to the console
                    Console.WriteLine(e);
                        Console.Error.WriteLine(e);
                    }
                }

                return records;
            }
        }
        catch (FileNotFoundException e)
        {
            Console.Error.WriteLine(e);
            throw;
        }
    }
}
 No newline at end of file
Loading