Skip to content

Commit

Permalink
Merge pull request #1568 from leotsarev/bug/schedule-api-repair
Browse files Browse the repository at this point in the history
Repair some small bugs in scheduler API
  • Loading branch information
leotsarev authored Oct 16, 2021
2 parents b760557 + 8b6cda0 commit 28603e7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/JoinRpg.Dal.Impl/Repositories/ProjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Task<Project> GetProjectWithDetailsAsync(int project)
.Include(p => p.ProjectAcls.Select(a => a.User))
.SingleOrDefaultAsync(p => p.ProjectId == project);

public Task<Project> GetProjectWithFieldsAsync(int project)
public Task<Project?> GetProjectWithFieldsAsync(int project)
=> AllProjects
.Include(p => p.Details)
.Include(p => p.ProjectAcls.Select(a => a.User))
Expand Down
2 changes: 1 addition & 1 deletion src/JoinRpg.Data.Interfaces/IProjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Task<IReadOnlyCollection<ProjectWithClaimCount>> GetArchivedProjectsWithClaimCou
Task<IEnumerable<Project>> GetActiveProjectsWithSchedule();
Task<Project> GetProjectAsync(int project);
Task<Project> GetProjectWithDetailsAsync(int project);
Task<Project> GetProjectWithFieldsAsync(int project);
Task<Project?> GetProjectWithFieldsAsync(int project);

[NotNull, ItemCanBeNull]
Task<CharacterGroup> GetGroupAsync(int projectId, int characterGroupId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ public class ProjectIdExtractorTest
[Fact]
public void ShouldParsePath() => new PathString("/100/dfsfdfsd").TryExtractFromPath().ShouldBe(100);

[Fact]
public void ShouldFindIfApi() => new PathString("/x-game-api/100/dfsfdfsd").TryExtractFromPath().ShouldBe(100);

[Fact]
public void ShouldNotFoundAfterAnything() => new PathString("/something/100/dfsfdfsd").TryExtractFromPath().ShouldBeNull();

[Fact]
public void ShouldIgnoreGarbageAfterApi() => new PathString("/x-game-api/x100/dfsfdfsd").TryExtractFromPath().ShouldBeNull();

[Fact]
public void ShouldIgnoreEmpty() => new PathString("").TryExtractFromPath().ShouldBeNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,31 @@ public class ProjectScheduleController : XGameApiController

[HttpGet]
[Route("all")]
[ProducesResponseType(410)]
[ProducesResponseType(403)]
[ProducesResponseType(200)]
public async Task<ActionResult<List<ProgramItemInfoApi>>> GetSchedule([FromRoute]
int projectId)
{
var project = await ProjectRepository.GetProjectWithFieldsAsync(projectId);

if (project is null)
{
return Problem(statusCode: 410);
}

var check = await Manager.CheckScheduleConfiguration();
if (check.Contains(ScheduleConfigProblemsViewModel.NoAccess))
{
return Forbid();
}
if (check.Any())
{
throw new Exception($"Error {check.Select(x => x.ToString()).JoinStrings(" ,")}");
return Problem(detail: check.Select(x => x.ToString()).JoinStrings(" ,"), statusCode: 400);
}

var project = await ProjectRepository.GetProjectWithFieldsAsync(projectId);
var characters = await ProjectRepository.GetCharacters(projectId);

var scheduleBuilder = new ScheduleBuilder(project, characters);
var result = scheduleBuilder.Build().AllItems.Select(slot =>
new ProgramItemInfoApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ internal static class ProjectIdExtractor

public static int? TryExtractFromPath(this PathString path)
{
if (!path.HasValue)
if (path.Value is null)
{
return null;
}
var parts = path.Value.Split('/');
var projectIdAsString = parts.Skip(1).FirstOrDefault();
if (projectIdAsString != null && int.TryParse(projectIdAsString, out var projectId))
var secondPart = parts.Skip(1).FirstOrDefault();
if (secondPart != null && int.TryParse(secondPart, out var projectId))
{
return projectId;
}
else if (parts.Skip(2).FirstOrDefault() is string thirdPart && secondPart == "x-game-api" && int.TryParse(thirdPart, out var projectIdSecond))
{
return projectIdSecond;
}
else
{
return null;
Expand Down
11 changes: 8 additions & 3 deletions src/JoinRpg.WebPortal.Managers/Schedule/SchedulePageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public async Task<IReadOnlyCollection<ScheduleConfigProblemsViewModel>> CheckSch
{
var project = await Project.GetProjectWithFieldsAsync(CurrentProject.ProjectId);

if (project is null)
{
return new[] { ScheduleConfigProblemsViewModel.ProjectNotFound };
}

bool HasAccess(ProjectField roomField)
{
if (roomField.IsPublic)
Expand Down Expand Up @@ -249,8 +254,8 @@ IEnumerable<ScheduleConfigProblemsViewModel> Impl()
return Impl().ToList();
}

public IProjectRepository Project { get; }
public ICurrentProjectAccessor CurrentProject { get; }
public ICurrentUserAccessor CurrentUserAccessor { get; }
private IProjectRepository Project { get; }
private ICurrentProjectAccessor CurrentProject { get; }
private ICurrentUserAccessor CurrentUserAccessor { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ namespace JoinRpg.Web.Models.Schedules
{
public enum ScheduleConfigProblemsViewModel
{
//TODO поменять сообщение, когда сделаем настроечный экран
[Description("Расписание не настроено для этого проекта, обратитесь в техподдержку")]
[Description("Расписание не настроено для этого проекта. Вам необходимо добавить поля для помещения и расписания.")]
FieldsNotSet,
[Description("У полей, привязанных к расписанию, разная видимость. Измените настройки видимости полей (публичные/игрокам/мастерам) на одинаковые")]
[Description("У полей, привязанных к расписанию, разная видимость. Измените настройки видимости полей (публичные/игрокам/мастерам) на одинаковые.")]
InconsistentVisibility,
[Description("У вас нет доступа к расписанию данного проекта")]
NoAccess,
Expand All @@ -16,5 +15,7 @@ public enum ScheduleConfigProblemsViewModel
NoRooms,
[Description("Не настроено ни одного тайм-слота")]
NoTimeSlots,
[Description("Проект не найден")]
ProjectNotFound,
}
}

0 comments on commit 28603e7

Please sign in to comment.