Skip to content

Commit

Permalink
fix(controllers): fix a bug when a fetch job is added before schedule…
Browse files Browse the repository at this point in the history
… was fetched, update error response object in lessons controller
  • Loading branch information
music-soul1-1 committed Jul 25, 2024
1 parent 41c60f2 commit 7188e8c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
29 changes: 16 additions & 13 deletions NureTimetableAPI/Controllers/V2/LessonsController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Microsoft.AspNetCore.Mvc;
using NureTimetableAPI.Repositories;
using NureTimetableAPI.Types;
using NureTimetableAPI.Exceptions;
using Hangfire.Storage;
using Hangfire;
using Asp.Versioning;
using NureTimetableAPI.Repositories;
using NureTimetableAPI.Types;
using NureTimetableAPI.Exceptions;
using NureTimetableAPI.Models.Dto;
using NureTimetableAPI.Jobs;
using NureTimetableAPI.Controllers.Responses;

namespace NureTimetableAPI.Controllers.V2;

Expand Down Expand Up @@ -47,14 +48,6 @@ public async Task<IActionResult> GetLessons(

if (fetchJob == null || lessons == null || lessons.Count < 1)
{
RecurringJob.AddOrUpdate<ScheduleFetch>(
$"update-{type.ToString().ToLower()}-with-id-{id}",
job => job.Execute(id, type),
(lastJob != null && lastJob.NextExecution != null) ?
Cron.Daily(lastJob.NextExecution.Value.Hour, lastJob.NextExecution.Value.Minute + 1) :
Cron.Daily(DateTime.Now.Hour, DateTime.Now.Minute)
);

if (lastJob != null && lastJob.LastExecution != null && lastJob.LastExecution > DateTime.Now.AddSeconds(30))
{
throw new Exception($"Please wait {lastJob.LastExecution.Value.Second} seconds, and then try again");
Expand All @@ -66,7 +59,17 @@ public async Task<IActionResult> GetLessons(
throw new NotFoundException($"Cist lessons not found for {type} with id {id}");
}

return Ok(await repository.FetchSchedule(id, cistLessons, type, startTime, endTime));
lessons = await repository.FetchSchedule(id, cistLessons, type, startTime, endTime);

RecurringJob.AddOrUpdate<ScheduleFetch>(
$"update-{type.ToString().ToLower()}-with-id-{id}",
job => job.Execute(id, type),
(lastJob != null && lastJob.NextExecution != null) ?
Cron.Daily(lastJob.NextExecution.Value.Hour, lastJob.NextExecution.Value.Minute + 1) :
Cron.Daily(DateTime.Now.Hour, DateTime.Now.Minute)
);

return Ok(lessons);
}

return Ok(lessons);
Expand All @@ -77,7 +80,7 @@ public async Task<IActionResult> GetLessons(
}
catch (Exception ex)
{
return StatusCode(500, $"An error occupied while getting lessons for {type} with id {id}. Exception: {ex.Message}");
return StatusCode(500, new ResponseMessage($"An error occupied while getting lessons for {type} with id {id}. Exception: {ex.Message}", 500));
}
}

Expand Down
2 changes: 0 additions & 2 deletions NureTimetableAPI/Models/Cist/CistGroup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Newtonsoft.Json;
using NureTimetableAPI.Models.Domain;
using System.ComponentModel.DataAnnotations;

namespace NureTimetableAPI.Models.Cist;

Expand Down
1 change: 0 additions & 1 deletion NureTimetableAPI/Repositories/CistRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using NureTimetableAPI.Helpers;
using NureTimetableAPI.Models;
using NureTimetableAPI.Models.Cist;
using NureTimetableAPI.Models.Dto;
using NureTimetableAPI.Types;
using System.Net;

Expand Down

0 comments on commit 7188e8c

Please sign in to comment.