public async Task <ApiResult> DeleteAsync(int courseId, int userId) { var result = default(ApiResult); var message = "Unsabscribe has failed"; var errors = new string[] { $"{message}. Contact administrator, please" }; var loggerMessage = $"{message}. CourseId = {courseId}, UserId = {userId}"; var courseJobs = await courseJobUser.GetEntitiesByCondition(x => x.CourseId.Equals(courseId) && x.UserId.Equals(userId)) .ToArrayAsync(); if (!courseJobs.Any()) { result = ApiResult.GetErrorResult(ApiResultStatus.BadRequest, loggerMessage, message, errors); } else { var removeJobResult = false; foreach (var courseJob in courseJobs) { removeJobResult = backgroundJobClient.Delete(courseJob.JobId); if (removeJobResult) { await courseJobUser.DeleteAsync(courseJob); } else { result = ApiResult.GetErrorResult(ApiResultStatus.BadRequest, loggerMessage, message, errors); return(result); } } var userCourse = await usersCourses.GetEntityByConditionAsync(x => x.SystemUserId.Equals(userId) && x.TrainingCourseId.Equals(courseId)); await usersCourses.DeleteAsync(userCourse); result = ApiResult.GetOkResult(ApiResultStatus.Ok); } return(result); }
public async Task <ApiResult> RemoveCourseAsync(int id) { var result = default(ApiResult); var course = await courses.GetByIdAsync(id); if (course == null) { var message = "Remove course error"; var loggerMessage = $"Course id = {id} was not found in DELETE"; var errors = new string[] { "Course was not found" }; result = ApiResult.GetErrorResult(ApiResultStatus.NotFound, loggerMessage, message, errors); } else { await courses.DeleteAsync(course); result = ApiResult.GetOkResult(ApiResultStatus.Ok, "Course was deleted!"); } return(result); }