Skip to content

Commit

Permalink
Ran csharpier
Browse files Browse the repository at this point in the history
  • Loading branch information
noremacskich committed Oct 21, 2024
1 parent 860650e commit e90b6a2
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public class PotentialParentsDto
public int Id { get; set; }
public string Name { get; set; }
public List<PotentialParentsDto> SubSections { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ public async Task<Result> DeleteExpressionTextSectionAsync(int id)

public async Task<Result<int>> GetExpressionId(string expressionName)
{
var expression = await context.Expressions
.FirstOrDefaultAsync(x => x.Name.ToLower() == expressionName.ToLower());

if(expression is null)
var expression = await context.Expressions.FirstOrDefaultAsync(x =>
x.Name.ToLower() == expressionName.ToLower()
);

if (expression is null)
return Result.Fail(new NotFoundFailure("Expression"));

return expression.Id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public class AvailableParentsDto
public int Id { get; set; }
public string Name { get; set; }
public List<AvailableParentsDto> SubSections { get; set; } = new();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public class SectionTypeDto
public int Id { get; set; }
public string Name { get; set; } = null!;
public string? Description { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,105 +24,133 @@ internal static void AddExpressionSubsectionEndpoints(this WebApplication app)
endpointGroup
.MapGet(
"{name}",
async Task<Results<NotFound, Ok<ExpressionBaseResponse>>>
(string name, HttpContext httpContext, IExpressionTextSectionRepository repository) =>
async Task<Results<NotFound, Ok<ExpressionBaseResponse>>> (
string name,
HttpContext httpContext,
IExpressionTextSectionRepository repository
) =>
{
var expressionIdResult = await repository.GetExpressionId(name);
if (expressionIdResult.HasNotFound(out var notFound))
return notFound;
expressionIdResult.ThrowIfErrorNotHandled();

var sections = await repository.GetExpressionTextSections(expressionIdResult.Value);


var sections = await repository.GetExpressionTextSections(
expressionIdResult.Value
);

var hasEditPolicy = await httpContext.UserHasPolicyAsync(
Policies.ExpressionEditorPolicy
);

return TypedResults.Ok( new ExpressionBaseResponse()
{
ExpressionId = expressionIdResult.Value,
ExpressionSections = ExpressionHelpers.BuildExpressionPage(sections),
CanEditPolicy = hasEditPolicy
});

return TypedResults.Ok(
new ExpressionBaseResponse()
{
ExpressionId = expressionIdResult.Value,
ExpressionSections = ExpressionHelpers.BuildExpressionPage(sections),
CanEditPolicy = hasEditPolicy
}
);
}
)
.RequireAuthorization();

endpointGroup
.MapGet(
"{expressionId}/{sectionId}",
async Task<Results<NotFound, Ok<EditExpressionSectionResponse>>>
(int expressionId, int sectionId, IExpressionTextSectionRepository repository) =>
async Task<Results<NotFound, Ok<EditExpressionSectionResponse>>> (
int expressionId,
int sectionId,
IExpressionTextSectionRepository repository
) =>
{
var sectionResult = await repository.GetExpressionTextSection(sectionId);

if (sectionResult.HasNotFound(out var sectionNotFound))
return sectionNotFound;
sectionResult.ThrowIfErrorNotHandled();

return TypedResults.Ok(new EditExpressionSectionResponse()
{
Name = sectionResult.Value.Name,
Content = sectionResult.Value.Content,
ParentId = sectionResult.Value.ParentId,
SectionTypeId = sectionResult.Value.SectionTypeId
});

return TypedResults.Ok(
new EditExpressionSectionResponse()
{
Name = sectionResult.Value.Name,
Content = sectionResult.Value.Content,
ParentId = sectionResult.Value.ParentId,
SectionTypeId = sectionResult.Value.SectionTypeId
}
);
}
)
.RequirePolicyAuthorization(Policies.ExpressionEditorPolicy);

endpointGroup
.MapGet(
"{expressionId}/{sectionId}/options",
async Task<Results<NotFound, Ok<ExpressionSectionOptionsResponse>>>
(int expressionId, int sectionId, IExpressionTextSectionRepository repository) =>
async Task<Results<NotFound, Ok<ExpressionSectionOptionsResponse>>> (
int expressionId,
int sectionId,
IExpressionTextSectionRepository repository
) =>
{
var optionsResult = await repository.GetExpressionTextSectionOptions(new GetExpressionTestSectionOptionsDto()
{
ExpressionId = expressionId,
SectionId = sectionId == 0 ? null : sectionId // Handle Create (0 = null)
});

var optionsResult = await repository.GetExpressionTextSectionOptions(
new GetExpressionTestSectionOptionsDto()
{
ExpressionId = expressionId,
SectionId = sectionId == 0 ? null : sectionId // Handle Create (0 = null)
}
);

if (optionsResult.HasNotFound(out var notFound))
return notFound;
optionsResult.ThrowIfErrorNotHandled();

return TypedResults.Ok(new ExpressionSectionOptionsResponse()
{
SectionTypes = optionsResult.Value.ExpressionSectionTypes.Select(x => new SectionTypeDto()

return TypedResults.Ok(
new ExpressionSectionOptionsResponse()
{
Id = x.Id,
Name = x.Name,
Description = x.Description
}).ToList(),
AvailableParents = ExpressionHelpers.BuildAvailableParentTree(optionsResult.Value.AvailableParents)

});
SectionTypes = optionsResult
.Value.ExpressionSectionTypes.Select(x => new SectionTypeDto()
{
Id = x.Id,
Name = x.Name,
Description = x.Description
})
.ToList(),
AvailableParents = ExpressionHelpers.BuildAvailableParentTree(
optionsResult.Value.AvailableParents
)
}
);
}
)
.RequirePolicyAuthorization(Policies.ExpressionEditorPolicy);

endpointGroup
.MapPut(
"{expressionId}/{sectionId}",
async Task<Results<NotFound, ValidationProblem, NoContent>>
(int expressionId, int sectionId, EditExpressionSubSectionTextRequest request, IExpressionTextSectionRepository repository) =>
async Task<Results<NotFound, ValidationProblem, NoContent>> (
int expressionId,
int sectionId,
EditExpressionSubSectionTextRequest request,
IExpressionTextSectionRepository repository
) =>
{
var results = await repository.EditExpressionTextSectionAsync(new EditExpressionTextSectionDto()
{
Id = sectionId,
ExpressionId = expressionId,
Name = request.Name,
Content = request.Content,
SectionTypeId = request.SectionTypeId
});

var results = await repository.EditExpressionTextSectionAsync(
new EditExpressionTextSectionDto()
{
Id = sectionId,
ExpressionId = expressionId,
Name = request.Name,
Content = request.Content,
SectionTypeId = request.SectionTypeId
}
);

if (results.HasNotFound(out var notFound))
return notFound;
if (results.HasValidationError(out var validationProblem))
return validationProblem;
results.ThrowIfErrorNotHandled();

return TypedResults.NoContent();
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ List<ExpressionSectionDto> dbSections

return sections;
}

public static List<AvailableParentsDto> BuildAvailableParentTree(
List<PotentialParentsDto> dbSections
)
Expand All @@ -36,11 +36,7 @@ List<PotentialParentsDto> dbSections

foreach (var dbSection in dbSections)
{
var dto = new AvailableParentsDto()
{
Name = dbSection.Name,
Id = dbSection.Id
};
var dto = new AvailableParentsDto() { Name = dbSection.Name, Id = dbSection.Id };

dto.SubSections = BuildAvailableParentTree(dbSection.SubSections);

Expand All @@ -49,4 +45,4 @@ List<PotentialParentsDto> dbSections

return sections;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public class EditExpressionSubSectionTextRequest
public string Name { get; set; } = null!;
public string Content { get; set; } = null!;
public int SectionTypeId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public class EditExpressionSectionResponse
public string Content { get; set; }
public int? ParentId { get; set; }
public int SectionTypeId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public class ExpressionBaseResponse
public int ExpressionId { get; set; }
public List<ExpressionSectionDTO> ExpressionSections { get; set; }
public bool CanEditPolicy { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public class ExpressionSectionOptionsResponse
{
public List<AvailableParentsDto> AvailableParents { get; set; }
public List<SectionTypeDto> SectionTypes { get; set; }
}
}

0 comments on commit e90b6a2

Please sign in to comment.