Exemple #1
0
 public ActionResult Edit([FromForm] int id, GetPostDto dto)
 {
     if (!ModelState.IsValid)
     {
         ViewBag.Categories = _getCategories.Execute(new GeneralSearchQuery());
         ViewBag.Users      = _getUsers.Execute(new GeneralSearchQuery());
         ViewBag.Tags       = _getTags.Execute(new GeneralSearchQuery());
         return(View(dto));
     }
     try
     {
         // TODO: Add update logic here
         _editPost.Execute(dto);
         return(RedirectToAction(nameof(Index)));
     }
     catch (NotFoundException)
     {
         return(RedirectToAction(nameof(Index)));
     }
     catch (EntityAlreadyExistsException)
     {
         TempData["error"] = "User with the same username already exists.";
         return(View(dto));
     }
     catch (Exception)
     {
         TempData["error"] = "Something went wrong. Please try again.";
         return(View(dto));
     }
 }
Exemple #2
0
        public IActionResult EditPost(GetPostDto post)
        {
            var postData = _mapper.Map <PostDto>(post);

            ViewBag.PostId = post.Id;

            return(View(postData));
        }
Exemple #3
0
        public async Task <Response <GetPostDto> > GetPostAsync(string url)
        {
            var response = new Response <GetPostDto>();

            var post = await _context.Posts.FirstOrDefaultAsync(x => x.Url.Equals(url));

            if (null == post)
            {
                response.Msg = $"URL:{url} 不存在";
                return(response);
            }
            var category = await _context.Categories.FirstOrDefaultAsync(x => x.Id == post.CategoryId);

            var tags = (from post_tags in await _context.PostTags.ToListAsync()
                        join tag in await _context.Tags.ToListAsync()
                        on post_tags.TagId equals tag.Id
                        where post_tags.PostId.Equals(post.Id)
                        select new TagDto
            {
                TagName = tag.TagName,
                DisplayName = tag.DisplayName
            }).ToList();
            var previous = await _context.Posts.Where(x => x.CreationTime > post.CreationTime).Take(1).FirstOrDefaultAsync();

            var next = await _context.Posts.Where(x => x.CreationTime < post.CreationTime).OrderByDescending(x => x.CreationTime).Take(1).FirstOrDefaultAsync();

            var result = new GetPostDto
            {
                Title        = post.Title,
                Author       = post.Author,
                Url          = post.Url,
                Html         = post.Html,
                Markdown     = post.Markdown,
                CreationTime = Convert.ToDateTime(post.CreationTime).ToString("MMMM dd, yyyy HH:mm:ss", new CultureInfo("en-us")),
                Category     = new CategoryDto {
                    CategoryName = category.CategoryName, DisplayName = category.DisplayName
                },
                Tags     = tags,
                Previous = previous == null ? null : new PostForPagedDto {
                    Title = previous.Title, Url = previous.Url
                },
                Next = next == null ? null : new PostForPagedDto {
                    Title = next.Title, Url = next.Url
                }
            };

            response.Result = result;
            return(response);
        }
        public async Task <ActionResult <GetPostDto> > CreatePost(AddPostDto addPostDto)
        {
            GetPostDto getPostDto = null;

            try
            {
                getPostDto = await _postService.AddPostAsync(addPostDto);
            }
            catch
            {
                return(BadRequest());
            }

            return(CreatedAtRoute(nameof(GetPostById), new { id = getPostDto.Id }, getPostDto));
        }
Exemple #5
0
 public IActionResult Put(int id, [FromBody] GetPostDto dto)
 {
     try
     {
         dto.Id = id;
         _editPost.Execute(dto);
         return(StatusCode(204));
     }
     catch (NotFoundException)
     {
         return(NotFound());
     }
     catch (EntityAlreadyExistsException)
     {
         return(StatusCode(422));
     }
 }
Exemple #6
0
        public GetPostDto Execute(int request)
        {
            var post = Context.Posts
                       .Include(u => u.User)
                       .ThenInclude(r => r.Role)
                       .Include(c => c.Category)
                       .Include(i => i.Image)
                       .Include(pt => pt.PostTags)
                       .ThenInclude(t => t.Tag)
                       .Where(p => p.Id == request)
                       .FirstOrDefault();


            if (post == null)
            {
                throw new NotFoundException();
            }

            var dto = new GetPostDto
            {
                Id             = post.Id,
                Title          = post.Title,
                Summary        = post.Summary,
                Text           = post.Text,
                CategoryId     = post.CategoryId,
                Category       = post.Category.Name,
                UserId         = post.UserId,
                FirstName      = post.User.FirstName,
                LastName       = post.User.LastName,
                ImageId        = post.ImageId,
                Image          = post.Image.Path,
                ShowTagInPosts = post.PostTags.Select(t => new ShowTagInPosts
                {
                    TagName = t.Tag.Name
                })
            };

            return(dto);
        }
        public void Execute(GetPostDto request)
        {
            var post = Context.Posts.Find(request.Id);

            if (post == null)
            {
                throw new NotFoundException();
            }

            if (request.Title != post.Title && Context.Posts.Any(p => p.Title == request.Title))
            {
                throw new EntityAlreadyExistsException();
            }

            post.Title      = request.Title;
            post.Summary    = request.Summary;
            post.Text       = request.Text;
            post.UserId     = request.UserId;
            post.CategoryId = request.CategoryId;

            foreach (var p in Context.PostTags.Where(p => p.PostId == request.Id))
            {
                Context.PostTags.Remove(p);
            }

            foreach (var tag in request.TagsInPost)
            {
                Context.PostTags.Add(new Domain.PostTag
                {
                    PostId = post.Id,
                    TagId  = tag
                });
            }

            if (request.ImageFile != null)
            {
                var ext = Path.GetExtension(request.ImageFile.FileName);
                if (!FileUpload.AllowedExtensions.Contains(ext))
                {
                    throw new Exception("File extension is not ok");
                }

                var newFileName = Guid.NewGuid().ToString() + "_" + request.ImageFile.FileName;
                var filePath    = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName);
                request.ImageFile.CopyTo(new FileStream(filePath, FileMode.Create));

                var image = new Domain.Image
                {
                    Alt  = request.Title,
                    Path = newFileName
                };

                Context.Images.Add(image);

                post.Image = image;

                Context.SaveChanges();
            }
            else
            {
                Context.SaveChanges();
            }
        }