Home > Software engineering >  How to create a object with postman?
How to create a object with postman?

Time:01-05

How to create a clothes object using postman ? when i try to do it postman says that field Designer_Clothes and FashionHouse is required how to solve it?

MY MODELS:

Clothes:


 public class Clothes:IEntityBase
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public double Price { get; set; }
        public string ImageUrl { get; set; }
        public ClothesCategory ClothesCategory { get; set; }

        public List<Designer_Clothes> Designer_Clothes { get; set; }

        public int FashionHouseId { get; set; }
        [ForeignKey("FashionHouseId")]
        public FashionHouse FashionHouse { get; set; }
    }

Designer:


 public class Designer:IEntityBase
    {
        [HiddenInput]
        [Key]
        public int Id { get; set; }
        [Display(Name = "Profile Picture")]
        [Required(ErrorMessage ="Profile picture is required")]
        public string ProfilePictureUrl { get; set; }
        [Display(Name = "Full Name")]
        [Required(ErrorMessage ="Full name is required")]
        [StringLength(25, MinimumLength =3,ErrorMessage ="Full name must be between 3 and 25 chars")]
        public string FullName { get; set; }
        [Display(Name = "Bio")]
        [Required(ErrorMessage ="Biography is required")]
        public string Bio { get; set; }
        public List<Designer_Clothes>? Designer_Clothes { get; set; }
    }

Designer_Clothes

public class Designer_Clothes
    {
        public int ClothesId { get; set; }
        public Clothes Clothes { get; set; }
        public int DesignerId { get; set; }
        public Designer Designer { get; set; }
    }

FashionHouse


public class FashionHouse:IEntityBase
    {
        [Key]
        public int Id { get; set; }
        [Display(Name ="Profile picture")]
        [Required(ErrorMessage ="Profile picture is required")]
        public string ProfilePictureUrl { get; set; }
        [Display(Name ="Full Name")]
        [StringLength(50,MinimumLength =3,ErrorMessage ="Full name must be between 3 and 50 chars")]
        [Required(ErrorMessage = "Full name is required")]
        public string FullName { get; set; }
        [Display(Name = "Bio")]
        [Required(ErrorMessage = "Biography is required")]
        public string Bio { get; set; }

        public List<Clothes>? Clothes { get; set; }
    }

And this is my Post method in the controller



[HttpPost]
        public async Task<IActionResult> Create([FromBody] Clothes clothes)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            await _service.AddAsync(clothes);
            return Created($"/api/book/{clothes.Id}", clothes);
        }

I'am trying post json like this

{
        "name": "Second item ",
        "description": "Description of second item test",
        "price": 50,
        "imageUrl": "sdfdsfsdfsf",
        "clothesCategory": 3,
        "designer_Clothes": [{
            "ClothesId":1,
            "DesignerId":2
            
        }],
        "fashionHouseId": 2,
        "fashionHouse": {
            "Bio":"TEST",
            "FullName":"TESTE",
            "ProfilePictureUrl":"ASdASd"
        }
    }

but it doesn't work. Anybody knows why ?

CodePudding user response:

The class Designer_Clothes has the property Clothes that is required (not nullable). But the json missing this data :

{
    ...
    "name": "Second item ",
    "designer_Clothes": [{
        "ClothesId":1,
        "DesignerId":2
        // Miss > "Clothes": { ... }
    }],
    ...
}

But the clothe is also the root object. This is a reference loop :

{
    ...
    "name": "Second item ",
    "designer_Clothes": [{
        "ClothesId":1,
        "DesignerId":2
        "Clothes": { 
            ...
            "name": "Second item ",
            "designer_Clothes": [{
                "ClothesId":1,
                "DesignerId":2
                "Clothes": { REFERENCE LOOP }
            }],
            ...
        }
    }],
    ...
}

It's possible to adapt the model that will work... but as the API model is also the entities models, that will impact also the DB model.

Maybe you can consider to separate the API's model and the DB's model. Then you can declare a class like :

namespace MyApi.Model;

public class ClotheCreating
{
    public string Name { get; set; }
    public string Description { get; set; }
    public double Price { get; set; }
    public string ImageUrl { get; set; }
    public ClothesCategory ClothesCategory { get; set; }
    public List<int> DesignerIds { get; set; }
    public int FashionHouseId { get; set; }
}
  • Related