Manipulate one item at a time in the API

0

I have an API that does the POST of Item in the database, but before doing POST it makes a GET to get a code for the Item that will be saved.

NOTE: This code can not be repeated in the database, it must be unique.

When sending an array with two items, they are catching the same code, conflicting when writing to DB.

Image: POST being debugged.

The two items pass on line 102, and then the two items go on line 113.

How to make one item at a time get the code and then save it in the bank so the next items do the same process?

    
asked by anonymous 31.12.2015 / 13:06

1 answer

1

The solution was to control sending items to the API, sending one item at a time because the API has no control over how many items are being saved. It follows code that does the POST in the API and sends the item only when the previous one has already been saved.

//  Faz a inclusão do objeto de itemObj no banco de dados.
function incluirItensFunc(tnNrItem) {
    //  Atribui o cdprevenda ao itemObj[].
    $rootScope.itemObj[tnNrItem].Cdprevenda = $rootScope.prevendaSelecionado;
    if (tnNrItem == ($rootScope.itemObj.length - 1)) {
        Prevendas.incluirItemPreVenda($rootScope.itemObj[tnNrItem]).success(function () {
            console.log("incluirItemPreVenda OK");
            $rootScope.itemObj = [];
            calcularTotalPreVendaFunc();
        }).error(function () {
            console.log("incluirItemPreVenda Erro");
        })
    }
    else {
        Prevendas.incluirItemPreVenda($rootScope.itemObj[tnNrItem]).success(function () {
            console.log("incluirItemPreVenda OK");
            //  Chama a função incluirItensFunc novamente passando tnNrItem + 1 como parâmetro.
            incluirItensFunc(tnNrItem + 1);
        }).error(function () {
            console.log("incluirItemPreVenda Erro");
        })
    }
}
    
04.01.2016 / 17:52