Beispiel #1
0
        public async Task <UserInfo> AddAsync(UserInfo entity, CancellationToken cancellationToken = default)
        {
            var filterSpec = new UserInfoFilterSpecification(entity.UserName, string.Empty, string.Empty);
            var rowCount   = await _unitOfWork.UserInfoRepository.CountAsync(filterSpec, cancellationToken);

            if (rowCount > 0)
            {
                throw new EntityAlreadyExistsException();
            }

            AssignCreatorAndCompany(entity);
            await _unitOfWork.UserInfoRepository.AddAsync(entity);

            await _unitOfWork.CommitAsync(cancellationToken);

            return(entity);
        }
Beispiel #2
0
        public async Task <ActionResult> UpdateItemAsync([FromBody] UserInfoDTO userInfo, CancellationToken cancellationToken)
        {
            var specFilter = new UserInfoFilterSpecification(userInfo.UserName);
            var rowCount   = await _userInfoService.CountAsync(specFilter, cancellationToken);

            if (rowCount == 0)
            {
                throw new EntityNotFoundException(nameof(userInfo), userInfo.UserName);
            }

            var item = _mapper.Map <UserInfo>(userInfo);

            var result = await _userInfoService.UpdateAsync(item, cancellationToken);

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

            return(CreatedAtAction(nameof(ItemByIdAsync), new { id = item.UserName }, null));
        }
Beispiel #3
0
        protected void LoadIdentity()
        {
            if (!string.IsNullOrEmpty(_userName) && _userInfoService != null)
            {
                var filterSpec = new UserInfoFilterSpecification(_userName, string.Empty, string.Empty);
                _user = _userInfoService.FirstOrDefaultAsync(filterSpec).Result;

                var roleService = new RoleService(_userInfoService.UnitOfWork);
                var role        = roleService.GetUserRole(_userName).Result;
                if (role != null)
                {
                    _role.Clear();
                    foreach (var item in role.RoleDetails)
                    {
                        if (_role.ContainsKey(item.FunctionInfoId))
                        {
                            continue;
                        }
                        _role.Add(item.FunctionInfoId, item);
                    }
                }
            }
        }
Beispiel #4
0
        public async Task GenerateExcel(int?refId = null, string firstName = "", string lastName = "", CancellationToken cancellationToken = default)
        {
            try
            {
                string excelDestinationPath = Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    "DownloadMonitor",
                    DateTime.Now.ToString("yyyy"),
                    DateTime.Now.ToString("MM")
                    );

                var filterSpec = new UserInfoFilterSpecification(string.Empty, firstName, lastName);
                var results    = await _unitOfWork.UserInfoRepository.ListAsync(filterSpec, null, cancellationToken);

                cancellationToken.ThrowIfCancellationRequested();

                using (var package = new ExcelPackage())
                {
                    var ws = package.Workbook.Worksheets.Add("Sheet 1");

                    // add header
                    ws.Cells[1, 1].Value = "User Name";
                    ws.Cells[1, 2].Value = "First Name";
                    ws.Cells[1, 3].Value = "Last Name";

                    // format header
                    using (var range = ws.Cells[1, 1, 1, 4])
                    {
                        range.Style.Font.Bold        = true;
                        range.Style.Fill.PatternType = ExcelFillStyle.Solid;
                        range.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.DarkBlue);
                        range.Style.Font.Color.SetColor(System.Drawing.Color.White);
                    }

                    if (results?.Count > 0)
                    {
                        // write excel content
                        int row = 2;
                        foreach (var item in results)
                        {
                            ws.Cells[row, 1].Value = item.UserName;
                            ws.Cells[row, 2].Value = item.FirstName;
                            ws.Cells[row, 3].Value = item.LastName;
                            row++;
                        }
                    }

                    ws.Cells.AutoFitColumns(0);

                    string fileName = DateTime.Now.ToString("yyyyMMdd_hhmmss_") + Guid.NewGuid().ToString() + ".xlsx";
                    FileOutputUtil.OutputDir = new DirectoryInfo(excelDestinationPath);
                    var xFile = FileOutputUtil.GetFileInfo(fileName);
                    package.SaveAs(xFile);

                    // update database information (if needed)
                    if (refId.HasValue)
                    {
                        await _downloadProcessService.SuccessfullyGenerated(refId.Value, fileName);
                    }
                }
            }
            catch (Exception ex)
            {
                if (refId.HasValue)
                {
                    await _downloadProcessService.FailedToGenerate(refId.Value, ex.Message);
                }

                throw;
            }
        }