public async Task <string> Validate(RoleGetOptions options) { try { _logger.LogInformation("Start role validating."); string result = ValidationUtilities.ValidateRoleName(options.Name); if (!string.IsNullOrEmpty(result)) { return(result); } if (!options.Id.HasValue) { var models = await _dao.Get(options); if (models.Count() > 0) { string message = "Role with same user name have been already created. Please try another."; _logger.LogInformation(message); return(message); } } _logger.LogInformation("Role successfuly validated."); return(null); } catch (Exception exception) { _logger.LogError(exception.Message); throw exception; } }
public async Task <IEnumerable <Role> > Get(RoleGetOptions options) { try { StringBuilder sql = new StringBuilder(); _logger.LogInformation("Try to create get roles sql query"); sql.AppendLine(@" select Id, Name, CanTeach, MinLoad, MaxLoad from Role "); int conditionIndex = 0; if (options.Id.HasValue) { sql.AppendLine($"{(conditionIndex++ == 0 ? "where" : "and")} (Id = @id)"); } if (options.Ids != null && options.Ids.Count > 0) { sql.AppendLine($"{(conditionIndex++ == 0 ? "where" : "and")} (Id in @ids)"); } if (!string.IsNullOrEmpty(options.NormalizedSearch)) { sql.AppendLine($@" {(conditionIndex++ == 0 ? "where" : "and")} (lower(Name) like lower(@NormalizedSearch)) "); } if (!string.IsNullOrEmpty(options.Name)) { sql.AppendLine($@" {(conditionIndex++ == 0 ? "where" : "and")} (Name = @Name) "); } _logger.LogInformation($"Sql query successfully created:\n{sql.ToString()}"); _logger.LogInformation("Try to execute sql get roles query"); var result = await QueryAsync <Role>(sql.ToString(), options); _logger.LogInformation("Sql get roles query successfully executed"); return(result); } catch (Exception exception) { _logger.LogError(exception.Message); throw exception; } }
public async Task <IEnumerable <Role> > Get(RoleGetOptions options) { var roles = await _dao.Get(options); var rolesDepartments = await _roleInDepartmentService.Get(new RoleInDepartmentGetOptions { RoleIds = roles.Select(o => o.Id).ToList() }); foreach (var role in roles) { role.RoleDepartments = rolesDepartments.Where(o => o.RoleId == role.Id).ToList(); } return(roles); }
public async Task <IActionResult> Get([FromQuery] RoleGetOptions options) { return(Ok(await _service.Get(options))); }