Esempio n. 1
0
        public async Task <ServiceResponse <PostDto> > UpDatePostAsync(int id, UpdatePostDto dto)
        {
            ServiceResponse <PostDto> response = new ServiceResponse <PostDto>();

            try
            {
                var post = await _db.Posts.Include(c => c.User).FirstOrDefaultAsync(x => x.Id == id);

                if (post.User.Id == GetUserId())
                {
                    post.Title       = dto.Title;
                    post.Description = dto.Description;

                    _db.Posts.Update(post);
                    await _db.SaveChangesAsync();

                    response.Data = _mapper.Map <PostDto>(post);
                }
                else
                {
                    response.Success = false;
                    response.Message = "Post not found";
                }
            }catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }

            return(response);
        }
Esempio n. 2
0
        public async Task <IActionResult> Update([FromForm] UpdatePostDto updatePostDto)
        {
            UpdatePostViewModel updatePostViewModel = CustomMapper.GetUpdatePostViewModel(updatePostDto);

            if (updatePostDto.PhotoFile != null)
            {
                var post = await _postService.GetById(updatePostDto.Id);

                string existingFilePath = post.HeaderPhotoPath;

                try
                {
                    _fileService.UpdateLocalFile(updatePostDto.PhotoFile, existingFilePath);
                }
                catch (IOException) { }
            }

            var result = await _postService.Update(updatePostViewModel);

            if (result.IsValid)
            {
                return(Ok());
            }

            return(BadRequest(result.Errors));
        }
Esempio n. 3
0
        public async Task <IActionResult> SetRevisionToPost(string postId, [FromBody] UpdatePostDto postDto)
        {
            var pId       = Guid.Parse(postId);
            var newStatus = await BlogService.SetRevisionToPost(pId, postDto.Approved, postDto.Username);

            return(Ok(newStatus));
        }
Esempio n. 4
0
        public void UpdatePost(UpdatePostDto updatePost)
        {
            var existingPost = _postRepository.GetById(updatePost.Id);
            var post         = _mapper.Map(updatePost, existingPost);

            _postRepository.Update(post);
        }
Esempio n. 5
0
        public void Update(UpdatePostDto postDto)
        {
            Post postForUpdate = DbSet.Find(postDto.PostId);

            postForUpdate.MessageStringContent = postDto.MessageStringContent;
            postForUpdate.DateUpdate           = DateTime.Now;
        }
Esempio n. 6
0
        public async Task <IActionResult> EditPost(UpdatePostDto dto)
        {
            if (ModelState.IsValid)
            {
                Post postToUpdate = await _db.Posts.FindAsync(dto.PostId);

                if (postToUpdate is null)
                {
                    return(NotFound());
                }
                postToUpdate.Text = dto.Text;

                postToUpdate.Title = dto.Title;

                postToUpdate.CategoryId   = dto.CategoryId;
                postToUpdate.DateModified = DateTimeOffset.Now;
                await _db.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            ViewBag.ParentCategories = await _categoryService.GetParentCategoriesInSelectList();

            ViewBag.ChildCategories = await _categoryService.GetChildCategoriesByParentCategoryIdInSelectList(dto.ParentCategoryId);

            return(View(dto));
        }
Esempio n. 7
0
        public void Execute(UpdatePostDto request)
        {
            var type = Context.Posts.Find(request.Id);

            if (type == null)
            {
                throw new EntityNoFound();
            }

            var id = type.Id;

            var slike = new Domen.Picture
            {
                Id     = request.Id,
                Name   = request.Pictures,
                PostId = id
            };


            type.Pictures.Add(slike);

            type.Id   = request.Id;
            type.Name = request.Name;
            type.Text = request.Text;

            Context.SaveChanges();
        }
Esempio n. 8
0
        public async Task UpdatePostAsync(UpdatePostDto updatePost)
        {
            var existingPost = await _postRepository.GetByIDAsync(updatePost.Id);

            var post = _mapper.Map(updatePost, existingPost);
            await _postRepository.UpdateAsync(post);
        }
Esempio n. 9
0
        public async Task <ServiceResponse <GetPostDto> > UpdatePost(UpdatePostDto editedPost, int id)
        {
            ServiceResponse <GetPostDto> serviceResponse = new ServiceResponse <GetPostDto>();

            try
            {
                Post postToEdit = await _context.Posts.FirstOrDefaultAsync((p) => p.Id == id);

                postToEdit.Title       = editedPost.Title;
                postToEdit.Description = editedPost.Description;
                postToEdit.Date        = DateTime.Now;
                if (editedPost.Image != null)
                {
                    File.Delete("wwwroot\\" + postToEdit.Image);
                    string filePath = Path.Combine("wwwroot", "images",
                                                   DateTime.Now.Ticks + Path.GetRandomFileName() + editedPost.Image.FileName);
                    using (var stream = System.IO.File.Create(filePath))
                    {
                        await editedPost.Image.CopyToAsync(stream);

                        postToEdit.Image = filePath.Substring(8, filePath.Length - 8);
                    }
                }
                _context.Posts.Update(postToEdit);
                await _context.SaveChangesAsync();

                serviceResponse.Data = _mapper.Map <GetPostDto>(postToEdit);
            }
            catch (System.Exception ex)
            {
                serviceResponse.Success = false;
                serviceResponse.Message = "No se pudo encontrar el post a editar";
            }
            return(serviceResponse);
        }
Esempio n. 10
0
 public virtual async Task <PostWithDetailsDto> UpdateAsync(Guid id, UpdatePostDto input)
 {
     return(await RequestAsync <PostWithDetailsDto>(nameof(UpdateAsync), new ClientProxyRequestTypeValue
     {
         { typeof(Guid), id },
         { typeof(UpdatePostDto), input }
     }));
 }
Esempio n. 11
0
        public async Task <ActionResult> OnPost(Guid id, UpdatePostDto post)
        {
            var editedPost = await _postAppService.UpdateAsync(id, post);

            var blog = await _blogAppService.GetAsync(editedPost.BlogId);

            return(Redirect(Url.Content($"~/blog/{blog.ShortName}/{editedPost.Title}")));
        }
Esempio n. 12
0
        public async Task <PostViewModel> UpdatePostAsync(Post post, UpdatePostDto updatePostDto)
        {
            post.Title   = updatePostDto.Title;
            post.Content = updatePostDto.Content;

            await _postRepository.UpdateAsync(post);

            return(_mapper.Map <PostViewModel>(post));
        }
Esempio n. 13
0
        public async Task <IActionResult> UpdatePost([FromBody] UpdatePostDto postDto)
        {
            await _mediator.Send(new UpdatePostCommand
            {
                PostDto = postDto
            });

            return(Ok());
        }
        public async Task <IActionResult> updatePost([FromBody] UpdatePostDto postDto)
        {
            if (!ModelState.IsValid)
            {
                return(Ok());
            }
            postDto.UserId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            return(Ok(await _post.updatePost(postDto)));
        }
Esempio n. 15
0
 public static UpdatePostViewModel GetUpdatePostViewModel(UpdatePostDto updatePostDto)
 {
     return(new UpdatePostViewModel(
                updatePostDto.Id,
                updatePostDto.Title,
                updatePostDto.Content,
                updatePostDto.BlogId,
                updatePostDto.CategoryId,
                updatePostDto.IsActive));
 }
        public async Task UpdatePostAsync(UpdatePostDto updatePostDto)
        {
            UpdatePostValidate(updatePostDto);

            var post = await GetPostAsync(updatePostDto.Id);

            _mapper.Map(updatePostDto, post);
            post.Date = DateTime.Now;
            await _dbContext.SaveChangesAsync();
        }
        public void UpdatePost(UpdatePostDto updatePostDto)
        {
            UpdatePostValidate(updatePostDto);

            var post = GetPost(updatePostDto.Id);

            _mapper.Map(updatePostDto, post);
            post.Date = DateTime.Now;
            _dbContext.SaveChanges();
        }
Esempio n. 18
0
        public async Task <IActionResult> Update([FromBody] UpdatePostDto dto, [FromRoute] int id)
        {
            ServiceResponse <PostDto> response = await _postService.UpDatePostAsync(id, dto);

            if (response.Data is null)
            {
                return(NotFound());
            }
            return(Ok(response));
        }
Esempio n. 19
0
        public async Task <IActionResult> UpdatePost(UpdatePostDto updatePost)
        {
            Post post = await _repository.GetPost(updatePost.Id);

            post.Name       = updatePost.Name;
            post.Content    = updatePost.Content;
            post.ModifiedOn = DateTime.UtcNow;

            _repository.Update(post);
            return(Ok(await _repository.SaveAll()));
        }
Esempio n. 20
0
        public async Task Put(Guid id, [FromBody] UpdatePostDto updatePostDto)
        {
            var updatePostViewModel = new UpdatePostViewModel()
            {
                InitialPostId = id,
                Text          = updatePostDto.Text,
                Title         = updatePostDto.Title
            };

            await _postService.UpdatePostAsync(updatePostViewModel);
        }
Esempio n. 21
0
        public async Task <IActionResult> UpdatePost(string id, [FromBody] UpdatePostDto updatePostDto)
        {
            var result = await Mediator.Send(new UpdatePostCommand
            {
                UpdatePostDto = updatePostDto,
                Id            = id,
                UserId        = AuthorizedUserId
            });

            return(Ok(result));
        }
        private void UpdatePostValidate(UpdatePostDto updatePostDto)
        {
            if (updatePostDto == null)
            {
                throw new ArgumentNullException(nameof(updatePostDto));
            }

            if (!updatePostDto.Validate())
            {
                throw new InvalidOperationException();
            }
        }
Esempio n. 23
0
        public async Task <IActionResult> Update(UpdatePostDto updatePost)
        {
            var userOwnsPost = await _postService.UserOwnsPostAsync(updatePost.Id, User.FindFirstValue(ClaimTypes.NameIdentifier));

            if (!userOwnsPost)
            {
                return(BadRequest(new Response(false, "You do not own this post")));
            }
            await _postService.UpdatePostAsync(updatePost);

            return(NoContent());
        }
Esempio n. 24
0
        public async void Posts_PUT_Returns_400()
        {
            UpdatePostDto post = new UpdatePostDto()
            {
                Text  = "New post",
                Title = string.Empty
            };

            var response = await this.HttpClient.PostAsync($"/api/v1/posts/", new StringContent(JsonConvert.SerializeObject(post), Encoding.UTF8, "application/json"));

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
Esempio n. 25
0
        public async void Posts_PUT_Returns_404()
        {
            UpdatePostDto post = new UpdatePostDto()
            {
                Text  = "Updated post",
                Title = "Updated title"
            };

            var response = await this.HttpClient.PutAsync($"/api/v1/posts/{Guid.NewGuid()}", new StringContent(JsonConvert.SerializeObject(post), Encoding.UTF8, "application/json"));

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }
Esempio n. 26
0
        public async Task <IActionResult> UpdateAsync(UpdatePostDto updatePost)
        {
            var post = await _postService.GetPostByIdAsync(updatePost.Id);

            if (post == null)
            {
                return(NotFound());
            }

            await _postService.UpdatePostAsync(updatePost);

            return(NoContent());
        }
        public async Task <ActionResult> UpdatePost(UpdatePostDto updatePostDto)
        {
            try
            {
                await _postService.UpdatePostAsync(updatePostDto);
            }
            catch
            {
                BadRequest();
            }

            return(NoContent());
        }
Esempio n. 28
0
        public async Task<ActionResult> OnPost()
        {
            var post = new UpdatePostDto
            {
                BlogId = Post.BlogId,
                Title = Post.Title,
                Url = Post.Url,
                Content = Post.Content
            };

            var editedPost = await _postAppService.UpdateAsync(Post.Id, post);
            var blog = await _blogAppService.GetAsync(editedPost.BlogId);

            return Redirect(Url.Content($"~/blog/{blog.ShortName}/{editedPost.Url}"));
        }
Esempio n. 29
0
        public async Task <IActionResult> UpdateCurrentPost(int id, UpdatePostDto updatepost)
        {
            if (ModelState.IsValid)
            {
                var postFromService = await _blogService.GetPostById(id);

                _mapper.Map(updatepost, postFromService);
                if (await _uow.SaveChangesAsync() > 0)
                {
                    return(NoContent());
                }
                throw new System.Exception($"Updating post {id} failed on save");
            }
            return(BadRequest());
        }
Esempio n. 30
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] UpdatePostDto updatePostDto)
        {
            var post = await _postService.GetAsync(id);

            if (post == null)
            {
                return(NotFound());
            }

            var updatePost = _mapper.Map <Post>(updatePostDto);

            await _postService.UpdatePost(id, updatePost);

            return(NoContent());
        }