MinisterstwoFinansow/KrajowaAdministracjaSkarbowa/ApiWykazuPodatnikowVatData/ApiWykazuPodatnikowVatData.cs
/// <summary>
///     Dodaj lub zaktualizuj rekord w bazie danych
///     Add or update a record in the database
/// </summary>
/// <param name="entityResponse.Result.Subject">
///     Obiekt podmiotu jako Entity
///     The subject object as Entity
/// </param>
/// <returns>
///     Zaktualizowany lub wstawiony obiekt Entity lub przekazany obiekt Entity lub null
///     An updated or inserted Entity or a passed Entity, or null
/// </returns>
Dodaj lub zaktualizuj rekord w bazie danych
Add or update a record in the database
Dodaj lub zaktualizuj rekord w bazie danych Add or update a record in the database
private async Task<Entity> AddOrModifyEntityAsync(RequestAndResponseHistory requestAndResponseHistory,
            Entity entity, DateTime dateOfChecking)
        {
            return await Task.Run(async () =>
            {
                try
                {
                    var isEntitySaveChanges = 0;
                    try
                    {
                        Entity entityWhere = await FindByNipAsync(entity.Nip, dateOfChecking);
                        if (null != entityWhere)
                        {
                            entity.Id = entityWhere.Id;
                            _context.Entry(entityWhere).State = EntityState.Detached;
                        }
                        entity.RequestAndResponseHistoryId = requestAndResponseHistory.Id;
                        entity.RequestAndResponseHistory = requestAndResponseHistory;
                        entity.DateOfChecking = dateOfChecking;
                        entity.DateOfModification = DateTime.Now;
                        EntityState entityState = "00000000-0000-0000-0000-000000000000" != entity.Id.ToString()
                            ? EntityState.Modified
                            : EntityState.Added;
                        _context.Entry(entity).State = entityState;
                        isEntitySaveChanges = await _context.SaveChangesAsync();
#if DEBUG
                        _log4Net.Debug(
                            $"Save Entity Changes Async [{entityState}] to database: [{isEntitySaveChanges}] id: [{entity.Id}]");
#endif
                    }
                    catch (Exception e)
                    {
                        await Task.Run(() =>
                        {
                            _log4Net.Error(
                                $"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                e);
                        });
                    }
                    if (isEntitySaveChanges == 1)
                    {
                        try
                        {
                            var accountNumbersList = (List<string>)entity.AccountNumbers;
                            List<EntityAccountNumber> entityAccountNumbersList =
                                AddOrModifyEntityAccountNumberAsync(accountNumbersList, entity).Result;
                        }
                        catch (Exception e)
                        {
                            await Task.Run(() =>
                            {
                                _log4Net.Error(
                                    $"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                    e);
                            });
                        }
                        try
                        {
                            var entityPersonListRepresentatives = (List<EntityPerson>)entity.Representatives;
                            List<EntityPerson> entityPersonListRepresentative =
                                AddOrModifyEntityPersonAsync(entityPersonListRepresentatives, entity,
                                    "EntityRepresentativeId").Result;
                        }
                        catch (Exception e)
                        {
                            await Task.Run(() =>
                            {
                                _log4Net.Error(
                                    $"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                    e);
                            });
                        }
                        try
                        {
                            var entityPersonListAuthorizedClerks = (List<EntityPerson>)entity.AuthorizedClerks;
                            List<EntityPerson> entityPersonListAuthorizedClerk =
                                AddOrModifyEntityPersonAsync(entityPersonListAuthorizedClerks, entity,
                                    "EntityAuthorizedClerkId").Result;
                        }
                        catch (Exception e)
                        {
                            await Task.Run(() =>
                            {
                                _log4Net.Error(
                                    $"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                    e);
                            });
                        }
                        try
                        {
                            var entityPersonListPartners = (List<EntityPerson>)entity.Partners;
                            List<EntityPerson> entityPersonListPartner =
                                AddOrModifyEntityPersonAsync(entityPersonListPartners, entity, "EntityPartnerId")
                                    .Result;
                        }
                        catch (Exception e)
                        {
                            await Task.Run(() =>
                            {
                                _log4Net.Error(
                                    $"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n",
                                    e);
                            });
                        }
                    }
                }
                catch (Exception e)
                {
                    await Task.Run(() =>
                    {
                        _log4Net.Error(
                            $"\n{e.GetType()}\n{e.InnerException?.GetType()}\n{e.Message}\n{e.StackTrace}\n", e);
                    });
                }
                return await FindByNipAsync(entity.Nip, (DateTime)entity.DateOfChecking) ?? entity;
            });
        }
private async Task < Entity > AddOrModifyEntityAsync ( RequestAndResponseHistory requestAndResponseHistory , Entity entity , DateTime dateOfChecking ) { return await Task . Run ( async ( ) => { try { var isEntitySaveChanges = 0 ; try { Entity entityWhere = await FindByNipAsync ( entity . Nip , dateOfChecking ) ; if ( null != entityWhere ) { entity . Id = entityWhere . Id ; _context . Entry ( entityWhere ) . State = EntityState . Detached ; } entity . RequestAndResponseHistoryId = requestAndResponseHistory . Id ; entity . RequestAndResponseHistory = requestAndResponseHistory ; entity . DateOfChecking = dateOfChecking ; entity . DateOfModification = DateTime . Now ; EntityState entityState = " " != entity . Id . ToString ( ) ? EntityState . Modified : EntityState . Added ; _context . Entry ( entity ) . State = entityState ; isEntitySaveChanges = await _context . SaveChangesAsync ( ) ; if DEBUG _log4Net . Debug ( $" Save Entity Changes Async [ { entityState } ] to database: [ { isEntitySaveChanges } ] id: [ { entity . Id } ] " ) ; endif } catch ( Exception e ) { await Task . Run ( ( ) => { _log4Net . Error ( $" \n { e . GetType ( ) } \n { e . InnerException ? . GetType ( ) } \n { e . Message } \n { e . StackTrace } \n " , e ) ; } ) ; } if ( isEntitySaveChanges == 1 ) { try { var accountNumbersList = ( List < string > ) entity . AccountNumbers ; List < EntityAccountNumber > entityAccountNumbersList = AddOrModifyEntityAccountNumberAsync ( accountNumbersList , entity ) . Result ; } catch ( Exception e ) { await Task . Run ( ( ) => { _log4Net . Error ( $" \n { e . GetType ( ) } \n { e . InnerException ? . GetType ( ) } \n { e . Message } \n { e . StackTrace } \n " , e ) ; } ) ; } try { var entityPersonListRepresentatives = ( List < EntityPerson > ) entity . Representatives ; List < EntityPerson > entityPersonListRepresentative = AddOrModifyEntityPersonAsync ( entityPersonListRepresentatives , entity , " " ) . Result ; } catch ( Exception e ) { await Task . Run ( ( ) => { _log4Net . Error ( $" \n { e . GetType ( ) } \n { e . InnerException ? . GetType ( ) } \n { e . Message } \n { e . StackTrace } \n " , e ) ; } ) ; } try { var entityPersonListAuthorizedClerks = ( List < EntityPerson > ) entity . AuthorizedClerks ; List < EntityPerson > entityPersonListAuthorizedClerk = AddOrModifyEntityPersonAsync ( entityPersonListAuthorizedClerks , entity , " " ) . Result ; } catch ( Exception e ) { await Task . Run ( ( ) => { _log4Net . Error ( $" \n { e . GetType ( ) } \n { e . InnerException ? . GetType ( ) } \n { e . Message } \n { e . StackTrace } \n " , e ) ; } ) ; } try { var entityPersonListPartners = ( List < EntityPerson > ) entity . Partners ; List < EntityPerson > entityPersonListPartner = AddOrModifyEntityPersonAsync ( entityPersonListPartners , entity , " " ) . Result ; } catch ( Exception e ) { await Task . Run ( ( ) => { _log4Net . Error ( $" \n { e . GetType ( ) } \n { e . InnerException ? . GetType ( ) } \n { e . Message } \n { e . StackTrace } \n " , e ) ; } ) ; } } } catch ( Exception e ) { await Task . Run ( ( ) => { _log4Net . Error ( $" \n { e . GetType ( ) } \n { e . InnerException ? . GetType ( ) } \n { e . Message } \n { e . StackTrace } \n " , e ) ; } ) ; } return await FindByNipAsync ( entity . Nip , ( DateTime ) entity . DateOfChecking ) ?? entity ; } ) ; }
Dodaj lub zaktualizuj rekord w bazie danych
Add or update a record in the database
Dodaj lub zaktualizuj rekord w bazie danych Add or update a record in the database
Zaktualizowany lub wstawiony obiekt Entity lub przekazany obiekt Entity lub null
An updated or inserted Entity or a passed Entity, or null
Zaktualizowany lub wstawiony obiekt Entity lub przekazany obiekt Entity lub null An updated or inserted Entity or a passed Entity or null
requestAndResponseHistory
RequestAndResponseHistory
entity
Entity
dateOfChecking
DateTime
entityResponse.Result.Subject
Obiekt podmiotu jako Entity
The subject object as Entity
Obiekt podmiotu jako Entity The subject object as Entity