public void TestNewDocument()
        {
            var writer = ReaderFactory.GetProfilesWriter();
            var doc = new Document
            {
                ProfileId = ProfileIds.Diabetes,
                FileName = Guid.NewGuid().ToString(),
                FileData = new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5 },
                UploadedBy = "Test User",
                UploadedOn = DateTime.Now
            };

            var id = writer.NewDocument(doc);
            Assert.IsTrue(id > 0);
            
        }
        public void TestUploadDocument()
        {
            var reader = ReaderFactory.GetProfilesReader();
            var writer = ReaderFactory.GetProfilesWriter();

            // get copy from database
            var docFromDb = reader.GetDocuments(ProfileIds.Diabetes).First();
           
            // clone and update fields 
            var docToUpdate = new Document
            {
                Id = docFromDb.Id,
                ProfileId = docFromDb.ProfileId,
                FileName =  docFromDb.FileName,
                FileData = new byte[] { 0x5, 0x6, 0x7, 0x8, 0x9 },
                UploadedBy = docFromDb.UploadedBy,
                UploadedOn = DateTime.Now
            };

            // update cloned copy
            writer.UpdateDocument(docToUpdate);

            // get cloned copy from datas
            var updatedDoc = reader.GetDocument(docToUpdate.Id);

            Assert.AreNotEqual(updatedDoc.FileData, docFromDb.FileData);
            Assert.AreNotEqual(updatedDoc.UploadedOn, docFromDb.UploadedOn);
        }
 public void DeleteDocument(Document doc)
 {
     DeleteObject(doc);
 }
 public void UpdateDocument(Document doc)
 {
     UpdateObject(doc);
 }
 public int NewDocument(Document doc)
 {
     var id = SaveNewObject(doc);
     return id;
 }
 private void SaveNewDocument(string name)
 {
     var doc = new Document
     {
         ProfileId = ProfileIds.Diabetes,
         FileName = name,
         FileData = new byte[] {0x1, 0x2, 0x3, 0x4, 0x5},
         UploadedBy = "Doris",
         UploadedOn = DateTime.Now
     };
     Writer().NewDocument(doc);
 }
        public ActionResult Upload(string selectedProfileId)
        {
            if (Request != null)
            {
                int profileId = -1;
                if (!string.IsNullOrEmpty(selectedProfileId))
                {
                    profileId = Convert.ToInt32(selectedProfileId);
                }

                HttpPostedFileBase file = Request.Files["fileToBeUploaded"];
                if (file != null)
                {
                    byte[] uploadedFile = new byte[file.InputStream.Length];
                    file.InputStream.Read(uploadedFile, 0, uploadedFile.Length);

                    if (uploadedFile.Length > MaxFileSizeInBytes)
                    {
                        throw new FpmException("Max file upload size is 50 MB");
                    }

                    var fileName = Path.GetFileName(file.FileName);

                    // check is filename unique.
                    var docs = _reader.GetDocuments(fileName);
                    if (docs.Count > 0)
                    {
                        // now check is this name used with current profile.
                        var docFromDatabase = docs.FirstOrDefault(x => x.ProfileId == profileId);
                        if (docFromDatabase != null)
                        {
                            docFromDatabase.ProfileId = profileId;
                            docFromDatabase.FileName = fileName;                            
                            docFromDatabase.FileData = uploadedFile;
                            docFromDatabase.UploadedBy = new CurrentUser().Name;
                            docFromDatabase.UploadedOn = DateTime.Now;

                            _writer.UpdateDocument(docFromDatabase);
                        }
                    }
                    else
                    {
                        // else overwrite current file for selected profile.
                        var doc = new Document
                        {
                            ProfileId = profileId,
                            FileName = fileName,
                            FileData = uploadedFile,
                            UploadedBy = new CurrentUser().Name,
                            UploadedOn = DateTime.Now
                        };
                        _writer.NewDocument(doc);
                    }
                }
            }

            return RedirectToAction("Index", new { selectedProfile = selectedProfileId });
        }