public void Put(int id, IFormFile file, [FromForm] NewsPost model)
        {
            var    existingPost = _context.NewsPosts.Find(id);
            string filePath     = null;

            if (file != null)
            {
                using (var stream = file.OpenReadStream())
                {
                    var connectionString = _configuration.GetConnectionString("StorageConnection");
                    filePath = AzureStorage.AddUpdateFile(file.FileName, stream, connectionString, "CoWork454Container");
                }
            }
            //retain existing photo if none uploaded
            else if (file == null && existingPost.NewsPhoto != null)
            {
                filePath = existingPost.NewsPhoto;
            }

            //if nothing use default image
            else
            {
                filePath = "/images/news_default.jpg";
            }

            existingPost.NewsText       = model.NewsText;
            existingPost.NewsTitle      = model.NewsTitle;
            existingPost.NewsTag        = model.NewsTag;
            existingPost.AuthorId       = Convert.ToInt32(GetEncryptedGenericCookie("USER_ID"));
            existingPost.NewsPhoto      = filePath;
            existingPost.DateTimePosted = DateTimeOffset.Now;

            _context.NewsPosts.Update(existingPost);
            _context.SaveChanges();
        }
Ejemplo n.º 2
0
        public void Put(int id, IFormFile file, [FromForm] Resource model)
        {
            var    existingResource = _context.Resources.Find(id);
            string filePath         = null;

            if (file != null)
            {
                using (var stream = file.OpenReadStream())
                {
                    var connectionString = _configuration.GetConnectionString("StorageConnection");
                    filePath = AzureStorage.AddUpdateFile(file.FileName, stream, connectionString, "CoWork454Container");
                }
            }
            //retain existing photo if none uploaded
            else if (file == null && existingResource.ResourceImage != null)
            {
                filePath = existingResource.ResourceImage;
            }

            //if nothing use default image
            else
            {
                filePath = "/images/resource_default.jpg";
            }

            existingResource.ResourceName        = model.ResourceName;
            existingResource.ResourceDescription = model.ResourceDescription;
            existingResource.ResourceMaxCapacity = model.ResourceMaxCapacity;
            existingResource.ResourceHasVC       = model.ResourceHasVC;
            existingResource.ResourceImage       = filePath;

            _context.Resources.Update(existingResource);

            _context.SaveChanges();
        }
        public IActionResult Index(IFormFile file, [FromForm] BlogPost post)
        {
            var userIdCookie = GetEncryptedUserCookie("USER_ID");

            if (userIdCookie == null)
            {
                //can't Blog without login
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                string filePath = null;
                using (var stream = file.OpenReadStream())
                {
                    var connectionString = _Configuration.GetConnectionString("StorageConnection");
                    filePath = AzureStorage.AddUpdateFile(file.FileName, stream, connectionString, "Team1");
                }

                post.ImageUrl = filePath;
                _CoWork454Context.Add(post);
                _CoWork454Context.SaveChanges();

                return(new JsonResult(post));
            }
        }
        public async Task <ActionResult <BlogPost> > PostBlogPost(IFormFile file, BlogPost blogPost)
        {
            if (file != null)
            {
                string filePath = null;
                using (var stream = file.OpenReadStream())
                {
                    var connectionString = _Configuration.GetConnectionString("StorageConnection");
                    filePath = AzureStorage.AddUpdateFile(file.FileName, stream, connectionString, "Team1");
                }

                blogPost.ImageUrl = filePath;
            }
            _context.BlogPost.Add(blogPost);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetBlogPost", new { id = blogPost.Id }, blogPost));
        }
        //method to set model values for new & updating post
        public void updateModelValues(IFormFile file, NewsPost model)
        {
            string filePath = null;

            if (file != null)
            {
                using (var stream = file.OpenReadStream())
                {
                    var connectionString = _configuration.GetConnectionString("StorageConnection");
                    filePath = AzureStorage.AddUpdateFile(file.FileName, stream, connectionString, "CoWork454Container");
                }
            }
            else
            {
                filePath = "/images/news_default.jpg";
            }

            model.AuthorId       = Convert.ToInt32(GetEncryptedGenericCookie("USER_ID"));
            model.NewsPhoto      = filePath;
            model.DateTimePosted = DateTimeOffset.Now;
        }
Ejemplo n.º 6
0
        public void Post(IFormFile file, [FromForm] Resource model)
        {
            string filePath = null;

            if (file != null)
            {
                using (var stream = file.OpenReadStream())
                {
                    var connectionString = _configuration.GetConnectionString("StorageConnection");
                    filePath = AzureStorage.AddUpdateFile(file.FileName, stream, connectionString, "CoWork454Container");
                }
            }
            else
            {
                filePath = "/images/resource_default.jpg";
            }

            model.ResourceImage = filePath;

            _context.Add(model);
            _context.SaveChanges();
        }
        public async Task <IActionResult> PutBlogPost(IFormFile file, int id, BlogPost blogPost)
        {
            if (id != blogPost.Id)
            {
                return(BadRequest());
            }

            if (file != null)
            {
                string filePath = null;
                using (var stream = file.OpenReadStream())
                {
                    var connectionString = _Configuration.GetConnectionString("StorageConnection");
                    filePath = AzureStorage.AddUpdateFile(file.FileName, stream, connectionString, "Team1");
                }

                blogPost.ImageUrl = filePath;
            }

            _context.Entry(blogPost).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BlogPostExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }