예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="updatedPost"></param>
        /// <returns></returns>
        public async Task <bool> UpdatePost(BlogPostInputModel updatedPost)
        {
            var client  = _clientFactory.CreateClient();
            var request = new HttpRequestMessage(HttpMethod.Put, $"{apiRootUrl}BlogPosts/{updatedPost.Id}");

            request.Headers.Add("Accept", "application/json");
            request.Headers.Add("User-Agent", "IthWeb");

            var editedPost = new BlogPostDTO()
            {
                Id            = updatedPost.Id,
                Author        = updatedPost.Author,
                PublishedDate = updatedPost.PublishedDate,
                Title         = updatedPost.Title,
                Text          = updatedPost.Text,
                ImageUrl      = updatedPost.ImageUrl
            };

            if (updatedPost.ImageFile != null)
            {
                if (!string.IsNullOrEmpty(updatedPost.ImageUrl))
                {
                    var imageFileName = updatedPost.ImageUrl.Split('/').LastOrDefault();
                    await _imageFileService.DeleteImage(imageFileName);
                }

                editedPost.ImageUrl = await _imageFileService.SaveImage(updatedPost.ImageFile);
            }

            var postJson = JsonSerializer.Serialize(editedPost);

            request.Content = new StringContent(postJson, Encoding.UTF8, "application/json");

            var response = await client.SendAsync(request);

            return(response.IsSuccessStatusCode);
        }
        public IActionResult DeleteImage(int id)
        {
            string userId;

            try
            {
                userId = User.Claims.First(c => c.Type == "UserID").Value;
            }
            catch
            {
                return(Unauthorized());
            }
            var photo = _imageService.GetSingleByCondition(s => s.PhotoId == id && s.IsDeleted != true, null);

            if (photo == null)
            {
                return(NotFound());
            }
            if (photo.ApplicationUserId != userId)
            {
                return(Unauthorized(new { message = "Not allowed to modify other user's images!!" }));
            }
            var result = _imageFileService.DeleteImage(photo.Name);

            if (result != true)
            {
                return(NotFound());
            }
            else
            {
                photo.IsDeleted = true;
                _imageService.Update(photo);
                _imageService.SaveChanges();
                return(NoContent());
            }
        }