コード例 #1
0
        public async Task <ActionResult <ImagePostResponse> > Create([FromForm] CreateImagePostRequest imagePostRequest)
        {
            _logger.LogInformation("Received image post create request.");

            // Get the user making the request.
            User user = _currentUserService.GetCurrentUser(HttpContext);

            _logger.LogInformation("Requesting user email: {0}", user.Email);

            IFormFile image = imagePostRequest.Image;

            // Optimize image.
            _logger.LogInformation("Optimizing image.");
            Stream imageStream = _imageOptimizationService.Optimize(image);

            // Store image file.
            _logger.LogInformation("Saving image file with filename '{0}'.", image.FileName);
            string imageUri = await _imageStorage.SaveImage(imageStream, Path.GetExtension(image.FileName));

            _logger.LogInformation("Successfully saved image file at location '{0}'.", imageUri);

            // Save image database record.
            ImagePost newImagePost = new ImagePost()
            {
                UserEmail   = user.Email,
                Description = imagePostRequest.Description,
                ImageUri    = imageUri,
                DateCreated = DateTime.Now,
                Tags        = imagePostRequest.Tags
                              .Select(content => new ImagePostTag()
                {
                    Tag = new Tag()
                    {
                        Content = content
                    }
                }).ToList()
            };

            _logger.LogInformation("Creating image post.");
            newImagePost = await _imagePostService.Create(newImagePost);

            newImagePost.User = user;

            // Map to response object.
            ImagePostResponse imagePostResponse = _mapper.Map <ImagePostResponse>(newImagePost);

            // Publish event.
            _logger.LogInformation("Publishing image post create notification.");
            await _notificationService.Publish(new ImagePostCreatedNotification(imagePostResponse));

            _logger.LogInformation("Successfully created image post with id {0}.", newImagePost.Id);

            return(Created($"/imageposts/{newImagePost.Id}", imagePostResponse));
        }
コード例 #2
0
        public async Task <ActionResult <ImagePostResponse> > Update(int id, [FromBody] UpdateImagePostRequest imagePostRequest)
        {
            _logger.LogInformation("Received image post update request.");

            // Get the user making the request.
            User user = _currentUserService.GetCurrentUser(HttpContext);

            _logger.LogInformation("Requesting user email: {0}", user.Email);
            _logger.LogInformation("Image post id: {0}", id);

            // Check if user owns the post.
            _logger.LogInformation("Verifying user owns image post.");
            bool userOwnsPost = await _imagePostService.IsAuthor(id, user.Email);

            if (!userOwnsPost)
            {
                _logger.LogError("User '{0}' does not own image post with id {1}.", user.Email, id);
                return(Forbid());
            }

            try
            {
                // Update database image post.
                _logger.LogInformation("Updating image post with id {0}.", id);
                ImagePost updatedImagePost = await _imagePostService.Update(id,
                                                                            imagePostRequest.Description,
                                                                            imagePostRequest.Tags.Select(t => new Tag()
                {
                    Content = t
                }));

                updatedImagePost.User = user;

                ImagePostResponse imagePostResponse = _mapper.Map <ImagePostResponse>(updatedImagePost);

                // Publish event.
                _logger.LogInformation("Publishing image post updated notification.");
                await _notificationService.Publish(new ImagePostUpdatedNotification(imagePostResponse));

                _logger.LogInformation("Successfully updated image post with id {0}.", id);

                return(Ok(imagePostResponse));
            }
            catch (EntityNotFoundException)
            {
                _logger.LogError("Image post with id {0} does not exist.", id);
                return(NotFound());
            }
        }
コード例 #3
0
 public ImagePostCreatedNotification(ImagePostResponse imagePostResponse)
 {
     ImagePostResponse = imagePostResponse;
 }