public IActionResult Put(int id, [FromForm] UploadPostDto UPDto, [FromServices] IUpdatePostCommand command, [FromServices] PostDto dto)
        {
            if (id == 0)
            {
                return(StatusCode(StatusCodes.Status422UnprocessableEntity, new
                {
                    message = "Validation error",
                    Errors = "Id is required!"
                }));
            }
            if (UPDto.Image == null)
            {
                return(StatusCode(StatusCodes.Status422UnprocessableEntity, new
                {
                    message = "Validation error",
                    Errors = "Image is required!"
                }));
            }
            var guid      = Guid.NewGuid();
            var extension = Path.GetExtension(UPDto.Image.FileName);

            if (extension != ".jpg" && extension != ".jpeg" && extension != ".png")
            {
                return(StatusCode(StatusCodes.Status422UnprocessableEntity, new
                {
                    message = "Validation error",
                    Errors = "Only jpg, jpeg and png images are allowed!"
                }));
            }
            var FileName = guid + extension;
            var path     = Path.Combine("wwwroot", "images", FileName);

            using (var fileStream = new FileStream(path, FileMode.Create))
            {
                UPDto.Image.CopyTo(fileStream);
            }
            dto.Id        = id;
            dto.Title     = UPDto.Title;
            dto.Content   = UPDto.Content;
            dto.PhotoPath = "images/" + FileName;
            dto.TopicId   = UPDto.TopicId;
            _executor.ExecuteCommand(command, dto);
            return(NoContent());
        }
Example #2
0
 public IActionResult Put(int id, [FromBody] PostDto dto, [FromServices] IUpdatePostCommand command)
 {
     dto.Id = id;
     executor.ExecuteCommand(command, dto);
     return(StatusCode(StatusCodes.Status204NoContent));
 }