Ejemplo n.º 1
0
        public IActionResult ChooseNewMainPhoto(int photoId, int postId)
        {
            var newPhoto = _context.Photo.Where(x => x.Id == photoId).FirstOrDefault();

            _photoService.ChangeMainPhotoToFalse(postId);

            newPhoto.IsMain = true;

            _context.Photo.Update(newPhoto);
            _context.SaveChanges();

            return(RedirectToAction("ViewAllPhotos", new { id = postId }));
        }
Ejemplo n.º 2
0
        public IActionResult AddPost(PostViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            try
            {
                var post = new Post();
                if (model.Id == 0)
                {
                    post = new Post
                    {
                        Id               = model.Id,
                        Title            = model.Title,
                        ShortDescription = model.ShortDescription,
                        LongDescription  = model.LongDescription,
                        UserId           = _userManager.GetUserId(User),
                        PublishDate      = DateTime.UtcNow
                    };
                    _context.Add(post);
                    _context.SaveChanges();
                    foreach (var item in model.Categories)
                    {
                        _postService.AddPostCategory(post.Id, item);
                    }
                    if (model.MainPhoto != null && model.MainPhoto.Length > 0)
                    {
                        _postService.UploadImage(model.MainPhoto, post.Id);
                    }
                }
                else
                {
                    post                  = _postService.GetPost(model.Id);
                    post.Title            = model.Title;
                    post.ShortDescription = model.ShortDescription;
                    post.LongDescription  = model.LongDescription;
                    post.PublishDate      = model.PublishDate;

                    var newPost = new List <PostsCategory>();

                    for (int i = 0; i < model.Categories.Count; i++)
                    {
                        newPost.Add(new PostsCategory
                        {
                            PostId     = post.Id,
                            CategoryId = model.Categories[i]
                        });
                    }

                    post.PostsCategory = newPost;
                    _context.Update(post);
                    _context.SaveChanges();
                    if (model.MainPhoto != null && model.MainPhoto.Length > 0)
                    {
                        _photoService.ChangeMainPhotoToFalse(post.Id);
                        _postService.UploadImage(model.MainPhoto, post.Id);
                    }
                }

                if (model.Photos != null)
                {
                    _postService.UploadImage(model.Photos, post.Id);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(Redirect(Url.Action("UserProfile", "User")));
        }