C
C#5w ago
JAAAAO v2

I'm having problem making a post in swagger. Its a one to many relationship where "Produto" is one.

I have the "Produto", and i shouldn't need any "Material" or "MovimentacaoMaterial", but in swagger i got this huge request body:
{
"produtoId": 0,
"nome": "string",
"codigoDeBarras": 0,
"qrCode": 0,
"localizacaoAtual": "string",
"materiais": [
{
"materialId": 0,
"nome": "string",
"codigoDeBarras": 0,
"qrCode": 0,
"localizacaoAtual": "string",
"produtoId": 0,
"produto": "string",
"movimentacoesMateriais": [
{
"moviMaterialId": 0,
"data": "2024-06-06T14:08:31.194Z",
"localAnteriorMaterial": "string",
"localAtualMaterial": "string",
"materialId": 0,
"material": "string"
}
]
}
],
"movimentacoesProdutos": [
{
"moviProdutoId": 0,
"data": "2024-06-06T14:08:31.194Z",
"localAnteriorProduto": "string",
"localAtualProduto": "string",
"materialId": 0,
"material": {
"materialId": 0,
"nome": "string",
"codigoDeBarras": 0,
"qrCode": 0,
"localizacaoAtual": "string",
"produtoId": 0,
"produto": "string",
"movimentacoesMateriais": [
{
"moviMaterialId": 0,
"data": "2024-06-06T14:08:31.194Z",
"localAnteriorMaterial": "string",
"localAtualMaterial": "string",
"materialId": 0,
"material": "string"
}
]
}
}
]
}
{
"produtoId": 0,
"nome": "string",
"codigoDeBarras": 0,
"qrCode": 0,
"localizacaoAtual": "string",
"materiais": [
{
"materialId": 0,
"nome": "string",
"codigoDeBarras": 0,
"qrCode": 0,
"localizacaoAtual": "string",
"produtoId": 0,
"produto": "string",
"movimentacoesMateriais": [
{
"moviMaterialId": 0,
"data": "2024-06-06T14:08:31.194Z",
"localAnteriorMaterial": "string",
"localAtualMaterial": "string",
"materialId": 0,
"material": "string"
}
]
}
],
"movimentacoesProdutos": [
{
"moviProdutoId": 0,
"data": "2024-06-06T14:08:31.194Z",
"localAnteriorProduto": "string",
"localAtualProduto": "string",
"materialId": 0,
"material": {
"materialId": 0,
"nome": "string",
"codigoDeBarras": 0,
"qrCode": 0,
"localizacaoAtual": "string",
"produtoId": 0,
"produto": "string",
"movimentacoesMateriais": [
{
"moviMaterialId": 0,
"data": "2024-06-06T14:08:31.194Z",
"localAnteriorMaterial": "string",
"localAtualMaterial": "string",
"materialId": 0,
"material": "string"
}
]
}
}
]
}
My produto Class uses
public ICollection<Material>? Materiais { get; set; } = new List<Material>();
public ICollection<MovimentacaoProduto>? MovimentacoesProdutos { get; set; } = new List<MovimentacaoProduto>();
public ICollection<Material>? Materiais { get; set; } = new List<Material>();
public ICollection<MovimentacaoProduto>? MovimentacoesProdutos { get; set; } = new List<MovimentacaoProduto>();
And i dont really know what is the difference between the ICOllection and just creating a List to reference the classes. I'm facing this problem for a while now, any help will be very much appreciated! Tanks
10 Replies
JAAAAO v2
JAAAAO v25w ago
The problem is that i cant just pass :
{
"produtoId": 1,
"nome": "Cell",
"codigoDeBarras": 110,
"qrCode": 110,
"localizacaoAtual": "Storage",
}
{
"produtoId": 1,
"nome": "Cell",
"codigoDeBarras": 110,
"qrCode": 110,
"localizacaoAtual": "Storage",
}
As a request body to make a post
gio735
gio7355w ago
You should request only what's needed, make a new model for such cases
JAAAAO v2
JAAAAO v25w ago
using System.ComponentModel.DataAnnotations;

namespace micro_crud_be.Models
{
public class Produto
{
[Key]
public int ProdutoId { get; set; }
public string Nome { get; set; }
public int CodigoDeBarras { get; set; }
public int QRCode { get; set; }
public string LocalizacaoAtual { get; set; }
public ICollection<Material>? Materiais { get; set; } = new List<Material>();
public ICollection<MovimentacaoProduto>? MovimentacoesProdutos { get; set; } = new List<MovimentacaoProduto>();

}
}
using System.ComponentModel.DataAnnotations;

namespace micro_crud_be.Models
{
public class Produto
{
[Key]
public int ProdutoId { get; set; }
public string Nome { get; set; }
public int CodigoDeBarras { get; set; }
public int QRCode { get; set; }
public string LocalizacaoAtual { get; set; }
public ICollection<Material>? Materiais { get; set; } = new List<Material>();
public ICollection<MovimentacaoProduto>? MovimentacoesProdutos { get; set; } = new List<MovimentacaoProduto>();

}
}
But this model doesnt request all those things right?
gio735
gio7355w ago
I'd assume ICollection<MovimentacaoProduto>? Means that it's part of it Also $codegif
gio735
gio7355w ago
As for ICollection it's just an interface so it doesn't restrict you only to list and can use other classes which extend ICollection<T>
JAAAAO v2
JAAAAO v25w ago
cool, first time using disc to ask programming questions, will use this for now on but doesnt it mean that the body request will not need those other classes to make a postw
gio735
gio7355w ago
It's not about the class of the container there, I'm a bit lost with that response If you want to pass that collection then just passing an empty one should fix your problem, but if you are not expecting it at all I believe it's better to make a new model for request which doesn't contain it at all I'm not the most experienced about that so someone else might have a better solution
JAAAAO v2
JAAAAO v25w ago
Its the first time creating a SQL table thats one to many and im a realy lost, i do not have experience creating one to one either, so yeah. I just wanted to post a "Produto", without needing to create a "Material" or "Movimentacao", but the Swagger API gives me a body request whith all of these.
public ICollection<Material>? Materiais { get; set; } = null;
public ICollection<MovimentacaoProduto>? MovimentacoesProdutos { get; set; } = null;
public ICollection<Material>? Materiais { get; set; } = null;
public ICollection<MovimentacaoProduto>? MovimentacoesProdutos { get; set; } = null;
tried to create the class like this, but swagger still have the same request body
JAAAAO v2
JAAAAO v25w ago
https://www.youtube.com/watch?v=GF4Yag8mytc found this video that helped me if someone finds the same problem
Naveen Bommidi Tech Seeker
YouTube
Part-3| .NET 7 Web API CRUD Operation Using One-Many Relationship T...
Hi everyone, This video is about - .NET 7 Web API CRUD Operation Using One-Many Relationship Tables - Implement Creat Operation The entire session was made as a blog: https://www.learmoreseekmore.com/2023/03/dotnet7-webapi-crud-operation-using-ont-to-many-relationship-tables.html Video 1 https://youtu.be/_N_5zxlfrgw Video 2 https://youtu.be/2...