Beispiel #1
0
        public async Task <IActionResult> CreateLocalUnit([FromBody] LocalUnitCreateM data)
        {
            var result = await _createService.CreateLocalUnit(data, User.GetUserId());

            if (result != null && result.ContainsKey(nameof(UserAccess.UnauthorizedAccess)))
            {
                return(Forbid());
            }
            return(result == null ? (IActionResult)NoContent() : BadRequest(result));
        }
Beispiel #2
0
        public async Task <Dictionary <string, string[]> > UpdateLog(int logId, string data, string userId)
        {
            var logEntry = await _dbContext.DataUploadingLogs
                           .Include(l => l.DataSourceQueue)
                           .ThenInclude(q => q.DataSource)
                           .FirstOrDefaultAsync(l => l.Id == logId);

            if (logEntry == null)
            {
                throw new BadRequestException(nameof(Resource.UploadLogNotFound));
            }

            var type = logEntry.DataSourceQueue.DataSource.StatUnitType;
            var definitionWithRegId = new { RegId = 0 };
            var hasId = JsonConvert.DeserializeAnonymousType(data, definitionWithRegId).RegId > 0;

            Task <Dictionary <string, string[]> > task;

            switch (type)
            {
            case StatUnitTypes.LocalUnit:
                task = hasId
                        ? _editSvc.EditLocalUnit(ParseModel <LocalUnitEditM>(), userId)
                        : _createSvc.CreateLocalUnit(ParseModel <LocalUnitCreateM>(), userId);
                break;

            case StatUnitTypes.LegalUnit:
                task = hasId
                        ? _editSvc.EditLegalUnit(ParseModel <LegalUnitEditM>(), userId)
                        : _createSvc.CreateLegalUnit(ParseModel <LegalUnitCreateM>(), userId);
                break;

            case StatUnitTypes.EnterpriseUnit:
                task = hasId
                        ? _editSvc.EditEnterpriseUnit(ParseModel <EnterpriseUnitEditM>(), userId)
                        : _createSvc.CreateEnterpriseUnit(ParseModel <EnterpriseUnitCreateM>(), userId);
                break;

            default:
                throw new ArgumentOutOfRangeException(
                          $"Parameter `{nameof(data)}`: value of type `{type.ToString()}` is not supported.");
            }

            var errors = await task;

            if (errors != null && errors.Any())
            {
                return(errors);
            }

            logEntry.Status = DataUploadingLogStatuses.Done;
            await _dbContext.SaveChangesAsync();

            return(null);

            T ParseModel <T>() where T : StatUnitModelBase
            {
                var result = JsonConvert.DeserializeObject <T>(data);

                result.DataSource = logEntry.DataSourceQueue.DataSource.Name;
                return(result);
            }
        }