Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3630

It's possible to use JsonPatchDocument with FromForm and IFormFile?

$
0
0

I'm working with a small application just to study .NET Core and C#. I have a model called Movie and one of its fields is a byte array to store an image. I want to know how is it possible to use JsonPatchDocument to update that field.

The Movie model:

public class Movie{    [Key]    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]    public int id_movie { get; set; }    [Required]    public string name_movie { get; set; }    public byte[]? img_movie {  get; set; }    public string? desc_movie { get; set; }    public int duration_movie { get; set; }    public type_class_movie class_movie { get; set; }    public DateTime? created_at_movie { get; set; }    public DateTime? updated_at_movie { get; set; }    public int id_category_movie { get; set; }    [ForeignKey("id_category_movie")]    public Category? Category { get; set; }}public enum type_class_movie { Siete, Trece, Diesciseis, Dieciocho }

The MoviesController:

 [HttpPatch("update_movie/{id_movie:int}", Name = "UpdateMovie")]public IActionResult UpdateMovie(int id_movie, [FromForm] Movie movie, IFormFile imagen){    try    {        if (!_mRepo.MovieExistsById(id_movie))        {            var errorResponse = new            {                StatusCode = 400,                Message = "Movie doesn't exists"            };            return StatusCode(400, errorResponse);        }        using (var memoryStream = new MemoryStream())        {             await imagen.CopyToAsync(memoryStream);             movie.img_movie = memoryStream.ToArray();        }        if (!_mRepo.UpdateMovie(entity))        {            var errorResponse = new            {                StatusCode = 500,                Message = "An error ocurred while processing the request"            };            return StatusCode(500, errorResponse);        }        return StatusCode(200, new { updt_cat = entity });    }     catch (Exception ex)    {        Console.WriteLine(ex.ToString());        var errorResponse = new        {            StatusCode = "Error",            Message = "An error occurred while processing the request."        };        return StatusCode(500, errorResponse);    }}

I like the way the JsonPatchDocument partial update works, but I don't know how to use it to get a partial update of a form that may include an image, any help is appreciated.

UPDATE: This is a screenshot of patchDocument values:enter image description here

Screenshot of request in Potman:

enter image description here


Viewing all articles
Browse latest Browse all 3630

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>