Esempio n. 1
0
        public async Task <ActionResult> UpdateDraftAsync(int id, [FromBody] ModuleInfoDTO moduleInfoDto, CancellationToken cancellationToken = default)
        {
            InitUserInfo();
            if (!AllowCreate && !AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new ModuleInfoFilterSpecification(int.Parse(moduleInfoDto.Id), true);
            var rowCount   = await _moduleInfoService.CountAsync(specFilter, cancellationToken);

            if (rowCount == 0)
            {
                throw new EntityNotFoundException(nameof(ModuleInfo), moduleInfoDto.Id);
            }

            var moduleInfo = _mapper.Map <ModuleInfo>(moduleInfoDto);
            var result     = await _moduleInfoService.PatchDraft(moduleInfo, cancellationToken);

            if (!result)
            {
                AssignToModelState(_moduleInfoService.Errors);
                return(ValidationProblem());
            }

            return(CreatedAtAction(nameof(GetIdAsync), new { id = id }, null));
        }
Esempio n. 2
0
        public async Task <ActionResult> UpdateAsync([FromBody] ModuleInfoDTO moduleInfo, CancellationToken cancellationToken)
        {
            InitUserInfo();
            if (!AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new ModuleInfoFilterSpecification(int.Parse(moduleInfo.Id), true);
            var rowCount   = await _moduleInfoService.CountAsync(specFilter, cancellationToken);

            if (rowCount == 0)
            {
                throw new EntityNotFoundException(nameof(ModuleInfo), moduleInfo.Id);
            }

            // bind to old item


            var objItem = _mapper.Map <ModuleInfo>(moduleInfo);

            // untuk data yang mereference object, perlu di set null agar tidak insert sebagai data baru
            CleanReferenceObject(objItem);

            var result = await _moduleInfoService.UpdateAsync(objItem, cancellationToken);

            if (!result)
            {
                AssignToModelState(_moduleInfoService.Errors);
                return(ValidationProblem());
            }

            return(CreatedAtAction(nameof(GetIdAsync), new { id = objItem.Id }, null));
        }
Esempio n. 3
0
        private async Task <ModuleInfo> GetByIdAsync(int id, bool includeChilds = false, CancellationToken cancellationToken = default)
        {
            var specFilter = new ModuleInfoFilterSpecification(id, true);
            var moduleInfo = await _unitOfWork.ModuleInfoRepository.FirstOrDefaultAsync(specFilter, cancellationToken);

            if (moduleInfo == null || includeChilds == false)
            {
                return(moduleInfo);
            }



            return(moduleInfo);
        }
Esempio n. 4
0
        public async Task <string> GenerateExcel(string excelFilename, int?refId = null,
                                                 int?id = null, List <string> names = null, List <int> parentModuleIds = null,
                                                 Dictionary <string, int> exact      = null,
                                                 CancellationToken cancellationToken = default)
        {
            try
            {
                ModuleInfoFilterSpecification filterSpec = null;
                if (id.HasValue)
                {
                    filterSpec = new ModuleInfoFilterSpecification(id.Value);
                }
                else
                {
                    filterSpec = new ModuleInfoFilterSpecification(exact)
                    {
                        Id              = id,
                        Names           = names,
                        ParentModuleIds = parentModuleIds
                    }
                }.BuildSpecification();

                var results = await this.ListAsync(filterSpec, null, true, cancellationToken);

                cancellationToken.ThrowIfCancellationRequested();

                if (ExcelMapper.WriteToExcel <ModuleInfo>(excelFilename, "moduleInfo.json", results) == false)
                {
                    if (refId.HasValue)
                    {
                        await _downloadProcessService.FailedToGenerate(refId.Value, "Failed to generate excel file");
                    }
                    return("");
                }

                // update database information (if needed)
                if (refId.HasValue)
                {
                    excelFilename = Path.GetFileName(excelFilename);
                    await _downloadProcessService.SuccessfullyGenerated(refId.Value, excelFilename);
                }

                return(excelFilename);
            }