public void TestThatInstitutionSetterRaisesPropertyChanged()
        {
            var fixture = new Fixture();

            var documentAuthor = new DocumentAuthor(fixture.CreateAnonymous <string>(), fixture.CreateAnonymous <string>());

            Assert.That(documentAuthor, Is.Not.Null);

            var eventCalled = false;

            documentAuthor.PropertyChanged += (s, e) =>
            {
                Assert.That(e, Is.Not.Null);
                Assert.That(e.PropertyName, Is.Not.Null);
                Assert.That(e.PropertyName, Is.Not.Empty);
                Assert.That(e.PropertyName, Is.EqualTo("Institution"));
                eventCalled = true;
            };

            documentAuthor.Institution = documentAuthor.Institution;
            Assert.That(eventCalled, Is.False);

            documentAuthor.Institution = fixture.CreateAnonymous <string>();
            Assert.That(eventCalled, Is.True);
        }
        public void TestThatInstitutionSetterThrowsArgumentNullExceptionIfValueIsEmpty()
        {
            var fixture = new Fixture();

            var documentAuthor = new DocumentAuthor(fixture.CreateAnonymous <string>(), fixture.CreateAnonymous <string>());

            Assert.That(documentAuthor, Is.Not.Null);

            Assert.Throws <ArgumentNullException>(() => documentAuthor.Institution = string.Empty);
        }
Esempio n. 3
0
        private static void PopulateDatabase(LoremContext context)
        {
            var author1 = new Author()
            {
                FirstName = "Dr",
                LastName  = "Seuss"
            };
            var author2 = new Author()
            {
                FirstName = "Mr",
                LastName  = "Spock"
            };
            var author3 = new Author()
            {
                FirstName = "Mr",
                LastName  = "Tuvok"
            };

            var document1 = new Document();

            document1.Title = "Green Eggs and Ham";

            var document2 = new Document();

            document2.Title = "Vulcan, A Primer";

            var document3 = new Document();

            document3.Title = "Green Eggs and Vulcans";

            var da1 = new DocumentAuthor()
            {
                Document = document1, Author = author1
            };
            var da2 = new DocumentAuthor()
            {
                Document = document2, Author = author2
            };
            var da3 = new DocumentAuthor()
            {
                Document = document3, Author = author1
            };
            var da4 = new DocumentAuthor()
            {
                Document = document3, Author = author2
            };
            var da5 = new DocumentAuthor()
            {
                Document = document3, Author = author3
            };

            context.AddRange(da1, da2, da3, da4, da5);

            context.SaveChanges();
        }
        public void TestThatConstructorInitializeDocumentAuthorWithoutAuthor()
        {
            var fixture        = new Fixture();
            var institution    = fixture.CreateAnonymous <string>();
            var documentAuthor = new DocumentAuthor(institution);

            Assert.That(documentAuthor, Is.Not.Null);
            Assert.That(documentAuthor.Author, Is.Null);
            Assert.That(documentAuthor.Institution, Is.Not.Null);
            Assert.That(documentAuthor.Institution, Is.Not.Empty);
            Assert.That(documentAuthor.Institution, Is.EqualTo(institution));
        }
Esempio n. 5
0
        private IDocumentAuthor GetAuthor(string value)
        {
            int start, end;

            start = value.IndexOf('[');
            if (start == -1)
            {
                throw new FormatException("属性格式错误,作者解释失败。");
            }
            end = value.IndexOf(']', start + 1);
            if (end == -1)
            {
                throw new FormatException("属性格式错误,作者解释失败。");
            }
            string name = value.Substring(start + 1, end - start - 1);

            start = value.IndexOf('(', end + 1);
            if (start == -1)
            {
                throw new FormatException("属性格式错误,作者解释失败。");
            }
            end = value.IndexOf(')', start + 1);
            if (end == -1)
            {
                throw new FormatException("属性格式错误,作者解释失败。");
            }
            string expression = value.Substring(start + 1, end - start - 1);

            start = expression.IndexOf(" \"");
            string id, link;

            if (start != -1)
            {
                id   = expression.Substring(0, start);
                link = expression.Substring(start + 2, expression.Length - start - 3);
            }
            else
            {
                id   = expression;
                link = null;
            }
            if (_Authors.ContainsKey(id))
            {
                return(_Authors[id]);
            }
            var author = new DocumentAuthor(id, name, null, link);

            _Authors.Add(id, author);
            return(author);
        }
        public void TestThatInstitutionSetterChangeValue()
        {
            var fixture = new Fixture();

            var documentAuthor = new DocumentAuthor(fixture.CreateAnonymous <string>(), fixture.CreateAnonymous <string>());

            Assert.That(documentAuthor, Is.Not.Null);

            var newValue = fixture.CreateAnonymous <string>();

            documentAuthor.Institution = newValue;

            Assert.That(documentAuthor.Institution, Is.Not.Null);
            Assert.That(documentAuthor.Institution, Is.Not.Empty);
            Assert.That(documentAuthor.Institution, Is.EqualTo(newValue));
        }
Esempio n. 7
0
        public async Task <IActionResult> EditDocument(int?id, IndexViewModel viewmodel)
        {
            if (ModelState.IsValid)
            {
                if (id == null)
                {
                    return(StatusCode(StatusCodes.Status400BadRequest));
                }
                var document = context_.Documents.Find(id);
                var oldpath  = document.StoragePath;
                if (document != null)
                {
                    document.DocumentId     = viewmodel.Document.DocumentId;
                    document.DocumentIntro  = viewmodel.Document.DocumentIntro;
                    document.Authority      = viewmodel.Document.Authority;
                    document.StoragePath    = viewmodel.Document.StoragePath;
                    document.LastUpdateTime = DateTime.Now;
                    if (viewmodel.uploadedfile != null)
                    {
                        viewmodel.Document.StoragePath = viewmodel.uploadedfile.FileName;
                        document.StoragePath           = viewmodel.uploadedfile.FileName;
                    }
                    foreach (var record in context_.DocumentAuthors) // remove old
                    {
                        if (record.DocumentId == document.DocumentId)
                        {
                            context_.DocumentAuthors.Remove(record);
                        }
                    }
                    foreach (var record in viewmodel.SelectedAuthors) // add new
                    {
                        var newrecord = new DocumentAuthor();
                        newrecord.DocumentId = document.DocumentId;
                        newrecord.AuthorId   = record;
                        context_.DocumentAuthors.Add(newrecord);
                    }
                    foreach (var record in context_.DocumentCategories) // remove old
                    {
                        if (record.DocumentId == document.DocumentId)
                        {
                            context_.DocumentCategories.Remove(record);
                        }
                    }
                    foreach (var record in viewmodel.SelectedCategories) // add new
                    {
                        var newrecord = new DocumentCategory();
                        newrecord.DocumentId = document.DocumentId;
                        newrecord.CategoryId = record;
                        context_.DocumentCategories.Add(newrecord);
                    }

                    try
                    { //path: ReadIt/ReadIt/xxx.pdf
                        if (viewmodel.Document.StoragePath != null)
                        {
                            HttpClient client = new HttpClient();
                            //find old path
                            HttpResponseMessage resp = await client.GetAsync(baseUrl_).ConfigureAwait(false);

                            var files = new List <string>();
                            var index = 0;
                            if (resp.IsSuccessStatusCode)
                            {
                                var json = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

                                JArray jArr = (JArray)JsonConvert.DeserializeObject(json);
                                foreach (var item in jArr)
                                {
                                    files.Add(item.ToString());
                                }
                            }
                            for (int i = 0; i < files.Count; i++)
                            {
                                if (files[i] == viewmodel.Document.StoragePath)
                                {
                                    index = i;
                                }
                            }

                            if (index != 0 && User.IsInRole("Admin"))
                            {
                                //delete old file
                                await client.DeleteAsync(baseUrl_ + "/" + index).ConfigureAwait(false);
                            }

                            //create new file
                            MultipartFormDataContent multiContent = new MultipartFormDataContent();
                            string uploadpath = Directory.GetCurrentDirectory();
                            uploadpath = uploadpath + "\\BookLibrary\\" + viewmodel.Document.StoragePath;
                            string path = uploadpath;

                            using (var stream = new FileStream(uploadpath, FileMode.Create))
                            {
                                await viewmodel.uploadedfile.CopyToAsync(stream);
                            }

                            // if type is mobi convert file here!
                            string ext = Path.GetExtension(viewmodel.uploadedfile.FileName);
                            if (ext == ".mobi" || ext == ".epub")
                            {
                                uploadpath           = createEpub(path);
                                document.StoragePath = Path.GetFileName(uploadpath);
                                byte[]           data  = System.IO.File.ReadAllBytes(uploadpath);
                                ByteArrayContent bytes = new ByteArrayContent(data);
                                multiContent.Add(bytes, "files", Path.GetFileName(uploadpath));
                                await client.PostAsync(baseUrl_, multiContent);

                                System.IO.File.Delete(uploadpath);
                                System.IO.File.Delete(path);
                            }
                            else
                            {
                                byte[]           data     = System.IO.File.ReadAllBytes(uploadpath);
                                ByteArrayContent bytes    = new ByteArrayContent(data);
                                string           fileName = Path.GetFileName(viewmodel.Document.StoragePath);
                                multiContent.Add(bytes, "files", fileName);
                                await client.PostAsync(baseUrl_, multiContent);

                                System.IO.File.Delete(uploadpath);
                            }
                        }
                    }
                    catch
                    {
                        //fail uploading files!
                    }
                    try
                    {
                        context_.SaveChanges();
                    }
                    catch (Exception)
                    {
                        // do nothing for now
                    }
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                ViewBag.Result = "Invalid Entries, Please Recheck.";
                return(RedirectToAction("EditDocument"));
            }
        }
Esempio n. 8
0
        public async Task <IActionResult> CreateDocument(int id, IndexViewModel viewmodel)
        {
            if (ModelState.IsValid)
            {
                ViewBag.Result = "Form Submitted Successfully.";
                viewmodel.Document.UploadUserId   = User.FindFirstValue(ClaimTypes.NameIdentifier);
                viewmodel.Document.UploadUserName = User.Identity.Name;
                viewmodel.Document.LastUpdateTime = DateTime.Now;
                viewmodel.Document.StoragePath    = viewmodel.uploadedfile.FileName;

                MultipartFormDataContent multiContent = new MultipartFormDataContent();
                string uploadpath = Directory.GetCurrentDirectory();
                uploadpath = uploadpath + "\\BookLibrary\\" + viewmodel.Document.StoragePath;
                string path = uploadpath;

                if (viewmodel.Document.StoragePath != null)
                {
                    using (var stream = new FileStream(uploadpath, FileMode.Create))
                    {
                        await viewmodel.uploadedfile.CopyToAsync(stream);
                    }

                    // if type is mobi convert file here!
                    string ext = Path.GetExtension(viewmodel.uploadedfile.FileName);
                    if (ext == ".mobi" || ext == ".epub")
                    {
                        uploadpath = createEpub(path);
                        byte[]           data  = System.IO.File.ReadAllBytes(uploadpath);
                        ByteArrayContent bytes = new ByteArrayContent(data);
                        multiContent.Add(bytes, "files", Path.GetFileName(uploadpath));
                        HttpClient client = new HttpClient();
                        await client.PostAsync(baseUrl_, multiContent);

                        viewmodel.Document.StoragePath = Path.GetFileName(uploadpath);
                        System.IO.File.Delete(uploadpath);
                        System.IO.File.Delete(path);
                    }
                    else
                    {
                        byte[]           data     = System.IO.File.ReadAllBytes(uploadpath);
                        ByteArrayContent bytes    = new ByteArrayContent(data);
                        string           fileName = Path.GetFileName(viewmodel.Document.StoragePath);
                        multiContent.Add(bytes, "files", fileName);
                        HttpClient client = new HttpClient();
                        await client.PostAsync(baseUrl_, multiContent);

                        System.IO.File.Delete(uploadpath);
                    }
                }

                context_.Documents.Add(viewmodel.Document);
                foreach (var newrecord in viewmodel.SelectedAuthors) // verify authority
                {
                    DocumentAuthor temp = new DocumentAuthor();
                    temp.AuthorId   = newrecord;
                    temp.DocumentId = viewmodel.Document.DocumentId;
                    context_.DocumentAuthors.Add(temp);
                }
                foreach (var newrecord in viewmodel.SelectedCategories)
                {
                    DocumentCategory temp = new DocumentCategory();
                    temp.CategoryId = newrecord;
                    temp.DocumentId = viewmodel.Document.DocumentId;
                    context_.DocumentCategories.Add(temp);
                }
                context_.SaveChanges();
                return(RedirectToAction("Index"));
            }
            else
            {
                ViewBag.Result = "Invalid Entries, Please Recheck.";
            }
            return(RedirectToAction("CreateDocument"));
        }