public ICSVFileStream GetCSVFile(DateTime timestamp)
        {
            using (var dbContext = new AllocationContext(_dbOptions))
            {
                CSVFileStorage csvfilestorage = dbContext.CSVFileStorages.Single(c => c.CSVTimeStamp == timestamp);
                if (csvfilestorage == null)
                {
                    return(null);
                }

                return(new MemoryCSVFileStream(csvfilestorage.CSVFileData, csvfilestorage.CSVTimeStamp, csvfilestorage.CSVFileFormat));
            }
        }
        public ICSVFileStream GetMostRecentCSVFile()
        {
            using (var dbContext = new AllocationContext(_dbOptions))
            {
                CSVFileStorage csvfilestorage = dbContext.CSVFileStorages.OrderBy(c => c.CSVTimeStamp).Last();
                if (csvfilestorage == null)
                {
                    return(null);
                }

                return(new MemoryCSVFileStream(csvfilestorage.CSVFileData, csvfilestorage.CSVTimeStamp, csvfilestorage.CSVFileFormat));
            }
        }
 public ICSVFileStream GetCSVFile(int id)
 {
     using (var dbContext = new AllocationContext(_dbOptions))
     {
         try
         {
             CSVFileStorage csvfilestorage = dbContext.CSVFileStorages.Single(c => c.CSVFileID == id);
             return(new MemoryCSVFileStream(csvfilestorage.CSVFileData, csvfilestorage.CSVTimeStamp, csvfilestorage.CSVFileFormat));
         }
         catch
         {
             return(null);
         }
     }
 }
        /// <summary>
        /// List the TimeStamps of all the stored csv files in the database
        /// </summary>

        /* public IReadOnlyList<long> StoredCSVFiles
         * {
         *   get
         *   {
         *       using (var dbContext = new AllocationContext(_dbOptions))
         *       {
         *           return dbContext.CSVFileStorage.Select(c => c.TimeStamp).ToList();
         *       }
         *   }
         * }*/

        /// <summary>
        /// Store a csv file in the database
        /// </summary>
        /// <param name="csvfileData"> the csvfile data stream to store csvfile data from</param>
        /// <returns>the database TimeStamp of the newly stored file</returns>
        public CSVFileStorage CreateCSVFile(ICSVFileStream csvfileData)
        {
            //SKData cv = EncodeFile(csvfileData);
            if (csvfileData == null)
            {
                throw new ArgumentException("Invalid file or unsupported format");
            }

            //byte[] fileBytes = new byte[csvfileData.Length];
            //FileStream filestream = csvfileData as FileStream;
            //filestream.Read(fileBytes, 0, fileBytes.Length);
            //csvfileData.

            byte[] fileBytes = new byte[csvfileData.Length];

            if (csvfileData.Length > 0)
            {
                using (var ms = new MemoryStream())
                {
                    csvfileData.File.CopyTo(ms);
                    fileBytes = ms.ToArray();
                }
            }

            var csvfilestorage = new CSVFileStorage()
            {
                CSVFileData = fileBytes, CSVTimeStamp = csvfileData.TimeStamp, CSVFileFormat = csvfileData.Format
            };

            using (var dbContext = new AllocationContext(_dbOptions))
            {
                dbContext.CSVFileStorages.Add(csvfilestorage);
                dbContext.SaveChanges();

                return(csvfilestorage);

                //return csvfileData.TimeStamp;
            }
        }
        public IActionResult UploadCSVFile(IFormFile file)
        {
            if (file == null)
            {
                return(BadRequest(new ErrorResponse("Bad request format.")));
            }

            ICSVFileStream csvfile = HttpCSVFileStream.CreateFromIFormFile(file);

            if (csvfile == null)
            {
                return(BadRequest(new ErrorResponse("Invalid CSVFile or unsupported format")));
            }

            try
            {
                CSVFileStorage savedCSV = _csvfileProvider.CreateCSVFile(csvfile);
                return(Json(new CSVFileIdResponse(savedCSV.CSVFileID)));
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(new ErrorResponse(ex.Message)));
            }
        }