Exemplo n.º 1
0
        public async Task <IEnumerable <RoleInDepartment> > Get(RoleInDepartmentGetOptions options)
        {
            try
            {
                StringBuilder sql = new StringBuilder();

                _logger.LogInformation("Try to create get departments sql query");

                sql.AppendLine(@"
                    select 
                        rid.Id,
                        r.Id as RoleId,
                        d.Id as DepartmentId,
                        d.Type as DepartmentType,
                        r.Name as RoleName,
                        d.FullName as DepartmentName
                    from RoleInDepartment rid
                    left join Role r on rid.RoleId = r.Id
                    left join Department d on rid.DepartmentId = d.Id
                ");

                int conditionIndex = 0;
                if (options.RoleId.HasValue)
                {
                    sql.AppendLine($"{(conditionIndex++ == 0 ? "where" : "and")} (rid.RoleId = @RoleId)");
                }
                if (options.DeparmtentId.HasValue)
                {
                    sql.AppendLine($"{(conditionIndex++ == 0 ? "where" : "and")} (rid.DepartmentId = @DepartmentId)");
                }
                if (options.RoleIds != null)
                {
                    sql.AppendLine($"{(conditionIndex++ == 0 ? "where" : "and")} (rid.RoleId in @RoleIds)");
                }
                if (options.DepartmentIds != null)
                {
                    sql.AppendLine($"{(conditionIndex++ == 0 ? "where" : "and")} (rid.DepartmentId in @DepartmentIds)");
                }
                _logger.LogInformation($"Sql query successfully created:\n{sql.ToString()}");

                _logger.LogInformation("Try to execute sql get departments query");
                var result = await QueryAsync <RoleInDepartment>(sql.ToString(), options);

                _logger.LogInformation("Sql get departments query successfully executed");
                return(result);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                throw exception;
            }
        }
 public async Task <IEnumerable <RoleInDepartment> > Get(RoleInDepartmentGetOptions options) => await _dao.Get(options);