public async Task <ApiResult> CreateCourseAsync(TrainingCourseDTO courseDTO)
        {
            var result = default(ApiResult);
            var course = await courses.GetEntityByConditionAsync(x => x.Title.Equals(courseDTO.Title));

            if (course != null)
            {
                var message       = "Course creation has failed";
                var loggerMessage = $"Course with title {courseDTO.Title} already exists";
                var errors        = new string[] { loggerMessage };
                result = ApiResult.GetErrorResult(ApiResultStatus.BadRequest, loggerMessage, message, errors);
            }
            else
            {
                var image = courseDTO.Cover;
                if (image.Contains("share/img/courses"))
                {
                    courseDTO.Cover = imageWorker.CutImageToName(image);
                }
                course = mapperWrapper.MapEntity(courseDTO);
                await courses.CreateAsync(course);

                result = ApiResult.GetOkResult(ApiResultStatus.Ok, "Course created");
            }

            return(result);
        }
Beispiel #2
0
        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);
        }