public async Task <IActionResult> EditTree(TreeBindingModel model)
        {
            if (ModelState.IsValid)
            {
                ITreeCatalogResponsitory tree = new TreeCatalogResponsitory(_context);
                string url       = model.Url.Substring(8);
                var    file      = model.File;
                var    uploads   = Path.Combine(_hostingEnvironment.WebRootPath, "images");
                var    extension = Path.GetExtension(file.FileName);
                if (System.IO.File.Exists(Path.Combine(uploads, url)))
                {
                    System.IO.File.Delete(Path.Combine(uploads, url));
                }
                var id = Guid.NewGuid().ToString();
                using (var fs = new FileStream(Path.Combine(uploads, id + extension), FileMode.Create))
                {
                    file.CopyTo(fs);
                }
                model.Url = $"//images/{id + extension}".Substring(1);
                await tree.EditAsync(model);

                return(Redirect("/Trees"));
            }
            return(Ok());
        }
        public IActionResult GetInfo(string username)
        {
            ITreeCatalogResponsitory tree = new TreeCatalogResponsitory(_context);
            var t  = tree.GetTreeCatalogFromName(username);
            var tr = new TreeBindingModel
            {
                TreeName       = t.Name,
                ScientificName = t.ScientificName,
                Description    = t.Description,
                Url            = t.Url
            };

            return(Ok(tr));
        }
Exemple #3
0
        public async Task EditAsync(TreeBindingModel treeCatalog)
        {
            var item = await _ProjectContext.TreeCatalog.FindAsync(treeCatalog.ScientificName);

            if (item != null)
            {
                item.Name           = treeCatalog.TreeName;
                item.ScientificName = treeCatalog.ScientificName;
                item.Description    = treeCatalog.Description;
                item.Url            = treeCatalog.Url;
                _ProjectContext.TreeCatalog.Update(item);
                await _ProjectContext.SaveChangesAsync();
            }
        }
Exemple #4
0
        public async Task AddAsync(TreeBindingModel newTreeCatalog)
        {
            var item = _ProjectContext.TreeCatalog.FirstOrDefault(x => x.ScientificName == newTreeCatalog.ScientificName);

            if (item == null)
            {
                TreeCatalog tree = new TreeCatalog
                {
                    Name           = newTreeCatalog.TreeName,
                    ScientificName = newTreeCatalog.ScientificName,
                    Url            = newTreeCatalog.Url
                };
                _ProjectContext.TreeCatalog.Add(tree);
                await _ProjectContext.SaveChangesAsync();
            }
        }
        public async Task <IActionResult> UploadAsync(TreeBindingModel model)
        {
            if (ModelState.IsValid)
            {
                ITreeCatalogResponsitory tree = new TreeCatalogResponsitory(_context);
                var file = model.File;
                if (file.Length > 0)
                {
                    string extension = Path.GetExtension(file.FileName);
                    string path      = Path.Combine(_hostingEnvironment.WebRootPath, "images");
                    var    id        = Guid.NewGuid().ToString();
                    using (var fs = new FileStream(Path.Combine(path, id + extension), FileMode.Create))
                    {
                        file.CopyTo(fs);
                    }
                    model.Url = $"//images/{id + extension}".Substring(1);
                    await tree.AddAsync(model);

                    return(Redirect("User/Trees"));
                }
            }
            return(BadRequest());
        }