Ejemplo n.º 1
0
        /// <summary>
        /// Log delete method with clearing statistical units
        /// </summary>
        /// <param name="logId">Id of log</param>
        /// <param name="userId">Id of user</param>
        public async Task DeleteLogById(int logId, string userId)
        {
            var existing = await _dbContext.DataUploadingLogs.FindAsync(logId);

            if (existing == null)
            {
                throw new NotFoundException(nameof(Resource.QueueLogNotFound));
            }

            if (existing.SerializedUnit != null)
            {
                dynamic jsonParsed = JsonConvert.DeserializeObject(existing.SerializedUnit);
                int     unitType   = int.Parse(jsonParsed["unitType"].ToString());

                if (existing.Status == DataUploadingLogStatuses.Done &&
                    existing.StartImportDate != null)
                {
                    var unitTypes = GetUnitTypes(existing.TargetStatId, (StatUnitTypes)unitType);
                    switch (unitType)
                    {
                    case (int)StatUnitTypes.LocalUnit:
                        await _statUnitDeleteService.DeleteLocalUnitFromDb(existing.TargetStatId, userId, existing.StartImportDate);

                        break;

                    case (int)StatUnitTypes.LegalUnit:
                        await _statUnitDeleteService.DeleteLegalUnitFromDb(existing.TargetStatId, userId, existing.StartImportDate);

                        break;

                    case (int)StatUnitTypes.EnterpriseUnit:
                        await _statUnitDeleteService.DeleteEnterpriseUnitFromDb(existing.TargetStatId, userId, existing.StartImportDate);

                        break;

                    default:
                        throw new NotFoundException(nameof(Resource.StatUnitTypeNotFound));
                    }
                }
            }

            _dbContext.DataUploadingLogs.Remove(existing);
            await _dbContext.SaveChangesAsync();
        }