Simultaneous rescue of related entities

Asked

Viewed 25 times

1

Good afternoon. I am having a difficulty that I believe is something very simple but as I am new in the area I am facing an absurd problem. I have 2 entities (at the example level), one called AIRCRAFT and the other called AERONAVE_DOCUMENTO, which have one-to-many relationships (1 aircraft has several documents and 1 document only one aircraft). All right:

FOLLOWS THE ENTITIES:

public class Aeronave : Entity
{
    protected Aeronave()
    {
    }

    public Aeronave(string matricula, string fabricante, string categoria, string modelo)
    {
        Matricula = matricula;
        Fabricante = fabricante;
        Categoria = categoria;
        Modelo = modelo;
    }

    public string Matricula { get; set; }                       // OBRIGATÓRIO (5)
    public string Fabricante { get; set; }                      // OBRIGATÓRIO (50)
    public string Categoria { get; set; }                       // OBRIGATÓRIO (20)
    public string Modelo { get; set; }                          // OBRIGATÓRIO (30)
    
    // RELATIONSHIP
    public IEnumerable<AeronaveDocumento> AeronavesDocumentos { get; set; }
}

public class AeronaveDocumento : Entity
{
    protected AeronaveDocumento()
    {
    }

    public AeronaveDocumento(string titulo, DateTime? dataEmissao, DateTime dataValidade, string arquivo, Guid aeronaveId)
    {
        Titulo = titulo;
        DataEmissao = dataEmissao;
        DataValidade = dataValidade;
        Arquivo = arquivo;
        AeronaveId = aeronaveId;
    }

    public string Titulo { get; set; } // OBRIGATÓRIO (1,50)
    public DateTime? DataEmissao { get; set; } // OPCIONAL
    public DateTime DataValidade { get; set; } // OBRIGATÓRIO
    public string Arquivo { get; set; } // OPCIONAL

    // RELATIONSHIPS
    public Aeronave Aeronave { get; set; }
    public Guid AeronaveId { get; set; } // OBRIGATÓRIO
}

The problem is this: On my front, in the AIRCRAFT creation form I created a formarray so that the person could already add whatever documents he wants from that aircraft. Well, in the back is getting there in the controller the aircraft entity along with the list, HOWEVER, at the time I call the repository of the aircraft to save, it saves only the aircraft and disregards the documents, it is not doing the rescue of the documents already direct. I know it’s my mistake and possibly bullshit, but I don’t know how to fix it.

FOLLOWS THE AIRCRAFT CONTROLLER (INSERT METHOD):

[HttpPost]
    public IActionResult Post([FromBody] AeronaveEditViewModel model) // nessa model ele está recebendo a coleção de documentos, mas não os salva. Assim como na entidade, nessa viewmodel também está presente a coleção de AeronaveDocumentoViewModel
    {
        var aeronave = new Aeronave(model.Matricula, model.Fabricante, model.Categoria, model.Modelo);
        _aeronaveService.Insert(aeronave, true);
        return Ok();
    }
  • You receive the documents via POST and do what with them? there where you instance a new Aircraft in the manufacturer, we do not understand that you also assign the documents.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.