Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with validation for SchedulesEvents #880

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions CharlieBackend.Business/Helpers/ResponseMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
{
public static class ResponseMessages
{
public const string GroupNotFound = "Group not found";
public const string GroupNotFound = "Group not found. ";

public const string GroupHasNotStudents = "Group hasn't any students";
public const string GroupHasNotStudents = "Group hasn't any students. ";

public const string IndexNotValid = "Index cannot be less then 1 and greater than 5";
public const string IndexNotValid = "Index cannot be less then 1 and greater than 5. ";

public static string NotExist(string entityName) => $"{entityName} does not exist ";
public static string NotExist(string entityName) => $"{entityName} does not exist. ";

public static string NotValid(string entityName) => $"{entityName} is not valid ";
public static string NotValid(string entityName) => $"{entityName} is not valid. ";

public static string NotActive(string entityName) => $"{entityName} is not active ";
public static string NotActive(string entityName) => $"{entityName} is not active. ";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<Result<bool>> DeleteAsync(long id)

public async Task<ScheduledEventDTO> UpdateAsync(long id, UpdateScheduledEventDto updatedSchedule)
{
string errorMsg = await ValidateUpdatedScheduleAsync(updatedSchedule);
string errorMsg = await _validator.ValidateUpdatedScheduleAsync(updatedSchedule);

if (!string.IsNullOrEmpty(errorMsg))
{
Expand All @@ -58,36 +58,18 @@ public async Task<ScheduledEventDTO> GetAsync(long id)
return _mapper.Map<ScheduledEventDTO>(await _unitOfWork.ScheduledEventRepository.GetByIdAsync(id));
}

private async Task<string> ValidateUpdatedScheduleAsync(UpdateScheduledEventDto updatedSchedule)
{
if (await _unitOfWork.MentorRepository.GetByIdAsync(updatedSchedule.MentorId.GetValueOrDefault()) is null)
{
return ExceptionsConstants.MentorNotValid;
}
if (await _unitOfWork.ThemeRepository.GetByIdAsync(updatedSchedule.ThemeId.GetValueOrDefault()) is null)
{
return ExceptionsConstants.ThemeNotValid;
}
if (await _unitOfWork.StudentGroupRepository.GetByIdAsync(updatedSchedule.StudentGroupId.GetValueOrDefault()) is null)
{
return ExceptionsConstants.StudentGroupNotValid;
}

return string.Empty;
}

public async Task<Result<ScheduledEventDTO>> ConnectScheduleToLessonById(long eventId, long lessonId)
{
if (await _unitOfWork.ScheduledEventRepository.IsLessonConnectedToSheduledEventAsync(lessonId))
string errorMsg = await _validator.ValidateConnectEventToLessonAsync(eventId, lessonId);

if (!string.IsNullOrEmpty(errorMsg))
{
return Result<ScheduledEventDTO>.GetError(ErrorCode.Conflict, $"Lesson with id={lessonId} is already associated with another ScheduledEvent");
throw new EntityValidationException(errorMsg);
}

var scheduleEntity = await _unitOfWork.ScheduledEventRepository.ConnectEventToLessonByIdAsync(eventId, lessonId);

return scheduleEntity == null ?
Result<ScheduledEventDTO>.GetError(ErrorCode.NotFound, $"Scheduled event with id={eventId} does not exist") :
Result<ScheduledEventDTO>.GetSuccess(_mapper.Map<ScheduledEventDTO>(scheduleEntity));
return Result<ScheduledEventDTO>.GetSuccess(_mapper.Map<ScheduledEventDTO>(scheduleEntity));
}

public async Task<Result<SingleEventDTO>> CreateSingleEventAsync(CreateSingleEventDto createSingleEvent)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be better to use Result instead of throwing an exception, u sure that we need to throw an exception? It`s an additional performance issue, logging this exception and other effects

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface ISchedulesEventsDbEntityVerifier
Task<string> ValidateEventOccuranceId(long id);
Task<string> ValidateGetEventsFilteredRequest(ScheduledEventFilterRequestDTO request);
Task<string> ValidateCreateScheduleEventRequestAsync(CreateSingleEventDto request);
Task<string> ValidateUpdatedScheduleAsync(UpdateScheduledEventDto updatedSchedule);
Task<string> ValidateConnectEventToLessonAsync(long eventId, long lessonId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,45 @@ public async Task<string> ValidateCreateScheduleEventRequestAsync(CreateSingleEv

return error.Length > 0 ? error.ToString() : null;
}

public async Task<string> ValidateUpdatedScheduleAsync(UpdateScheduledEventDto updatedSchedule)
{
StringBuilder error = new StringBuilder();

if (await _unitOfWork.MentorRepository.GetByIdAsync(updatedSchedule.MentorId.GetValueOrDefault()) is null)
{
error.Append(ResponseMessages.NotExist("Mentor"));
}
if (await _unitOfWork.ThemeRepository.GetByIdAsync(updatedSchedule.ThemeId.GetValueOrDefault()) is null)
{
error.Append(ResponseMessages.NotExist("Theme"));
}
if (await _unitOfWork.StudentGroupRepository.GetByIdAsync(updatedSchedule.StudentGroupId.GetValueOrDefault()) is null)
{
error.Append(ResponseMessages.NotExist("Group"));
}

return error.Length > 0 ? error.ToString() : null;
}

public async Task<string> ValidateConnectEventToLessonAsync (long eventId, long lessonId)
{
StringBuilder error = new StringBuilder();

if (await _unitOfWork.ScheduledEventRepository.IsLessonConnectedToSheduledEventAsync(lessonId))
{
error.Append($"Lesson with id={lessonId} is already associated with another ScheduledEvent. ");
}
if(!await _unitOfWork.ScheduledEventRepository.IsEntityExistAsync(eventId))
{
error.Append(ResponseMessages.NotExist("EventID"));
}
if (!await _unitOfWork.LessonRepository.IsEntityExistAsync(lessonId))
{
error.Append(ResponseMessages.NotExist("LessonID"));
}

return error.Length > 0 ? error.ToString() : null;
}
}
}