Error inserting documents into MongoDB

0

I made these inserts through the mongo shell:

db.Operators.insert([{ 
    Name:'Paulo de Tarso',
    Version:1
}, { 
    Name:'José de Arimatéia',
    Version:1
}, { 
    Name:'Catarina Silva', 
    Version:3
}])

This is my entity in the API

public class Operator : Entity, IAggregateRoot
{
    public string Name { get; protected set; }
    public int Version { get; protected set; }

    public Operator(string name)
    {
        this.Name = name;
    }
}

I have the following scenarios:

When giving a Get by Postman, I get this error:

  

Internal Server Error

This error happens because my request class is empty

My controller

[AllowAnonymous]
[HttpGet(Name ="GetAllOperators")]
public async Task<IActionResult> GetOperators()
{
    List<OperatorResult> operatorResults = await operatorsQueries.GetOperator();
    List<OperatorDetailsModel> operatorDetailsModels = new List<OperatorDetailsModel>();
    operatorResults.ForEach(x => operatorDetailsModels.Add(new OperatorDetailsModel(x.OperatorId, x.Name)));

    var defaultReturn = new DefaultReturn(operatorResults, false, string.Empty);

    return new ObjectResult(defaultReturn);
}

My GetOperator of the operatorsQueries class

public async Task<List<OperatorResult>> GetOperator()
{
    List<Operator> operators = await context.Operators.Find(x => true).ToListAsync();
    List<OperatorResult> operatorResults = resultConverter.Map<List<OperatorResult>>(operators);
    return operatorResults;
}

Lambda picks up here

public void OnException(ExceptionContext context)
{
    DomainException domainException = context.Exception as DomainException;
    if (domainException != null)
    {
        string json = JsonConvert.SerializeObject(domainException.BusinessMessage);
        var defaultReturn = new DefaultReturn(new { }, true, domainException.BusinessMessage);
        context.Result = new BadRequestObjectResult(defaultReturn);
        context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
    }
}

Here's the error, because my domainException is coming NULL and should not.

The other scenario is when I enter Postman in the document. In this case, I do not have this error, but the Version field comes (0) because it is an int.

I just did not test with GUI tools for Mongo, like Robo 3T , for example.

    
asked by anonymous 05.07.2018 / 19:35

0 answers