public async Task <ActionResult <CourseEditDTO> > PostCourse(CourseEditDTO course)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var dbCourse = courseMapper.MapCourseEditDTO(course);

            var authorization = await authorizationService.AuthorizeAsync(User, dbCourse, AuthorizationConstants.CanCreateCoursePolicy);

            if (!authorization.Succeeded)
            {
                return(Forbid());
            }

            _context.Courses.Add(dbCourse);
            await _context.SaveChangesAsync();

            dbCourse = await _context.Courses
                       .Include(c => c.CourseInfo)
                       .Include(c => c.Professor)
                       .FirstOrDefaultAsync(c => c.Id == dbCourse.Id);

            await cache.ClearCoursesRefCacheAsync();

            return(CreatedAtAction("GetCourse", new { id = dbCourse.Id }, courseMapper.MapCourseEdit(dbCourse)));
        }
Exemple #2
0
        public static Courses EditDto2CourseEntity(this CourseEditDTO dto, int authorId)
        {
            var entity = new Courses
            {
                AuthorUserId              = authorId
                , uid                     = dto.Uid
                , CourseName              = dto.CourseName.TrimString()
                , CourseUrlName           = dto.CourseName.TrimString().OptimizedUrl()
                , StatusId                = (short)dto.Status
                , Description             = dto.CourseDescription
                , OverviewVideoIdentifier = dto.PromoVideoIdentifier.ToString()
                , SmallImage              = dto.ThumbName
                , ClassRoomId             = dto.ClassRoomId
                , MetaTags                = dto.MetaTags
                , Created                 = DateTime.Now
                , LastModified            = DateTime.Now
                , DisplayOtherLearnersTab = dto.DisplayOtherLearnersTab
                                            //default not nullable fields
                , AffiliateCommission = Constants.AFFILIATE_COMMISSION_DEFAULT
                , IsFreeCourse        = false
            };

            if (entity.PublishDate == null && dto.Status == CourseEnums.CourseStatus.Published)
            {
                entity.PublishDate = DateTime.Now;
            }

            return(entity);
        }
 /// <summary>
 /// This will map one to one properties of the Course. The one to many lists must be updated separately.
 /// </summary>
 /// <param name="dto"></param>
 /// <returns></returns>
 public Course MapCourseEditDTO(CourseEditDTO dto)
 {
     return(new Course {
         Id = dto.Id ?? Guid.Empty,
         CourseInfoId = dto.CourseDef.Id,
         ProfessorId = dto.Professor.Id
     });
 }
Exemple #4
0
        public static void UpdateCourseEntity(this Courses entity, CourseEditDTO dto)
        {
            entity.CourseName              = dto.CourseName.TrimString();
            entity.CourseUrlName           = dto.CourseName.OptimizedUrl();
            entity.Description             = dto.CourseDescription;
            entity.OverviewVideoIdentifier = dto.PromoVideoIdentifier.ToString();
            entity.SmallImage              = dto.ThumbName;
            entity.ClassRoomId             = dto.ClassRoomId;
            entity.StatusId                = (short)dto.Status;
            entity.MetaTags                = dto.MetaTags;
            entity.LastModified            = DateTime.Now;
            entity.DisplayOtherLearnersTab = dto.DisplayOtherLearnersTab;

            if (entity.PublishDate == null && dto.Status == CourseEnums.CourseStatus.Published)
            {
                entity.PublishDate = DateTime.Now;
            }
        }
Exemple #5
0
        public static CourseEditDTO CourseEntity2CourseEditDTO(this Courses entity, bool isAnyPrices)
        {
            var token = new CourseEditDTO
            {
                CourseId                  = entity.Id
                , Uid                     = entity.uid
                , AuthorId                = entity.AuthorUserId
                , CourseName              = entity.CourseName
                , CourseDescription       = entity.Description
                , MetaTags                = entity.MetaTags
                , Status                  = Utils.ParseEnum <CourseEnums.CourseStatus>(entity.StatusId.ToString())
                , ThumbName               = entity.SmallImage
                , ThumbUrl                = entity.SmallImage.ToThumbUrl(Constants.ImageBaseUrl)
                , ClassRoomId             = entity.ClassRoomId
                , AffiliateCommission     = entity.AffiliateCommission
                , IsFree                  = entity.IsFreeCourse
                , IsCoursePurchased       = entity.USER_Courses.ToList().Any()
                , HasChapters             = entity.CourseChapters.ToList().Count > 0
                , IsPriceDefined          = entity.IsFreeCourse || isAnyPrices
                , DisplayOtherLearnersTab = entity.DisplayOtherLearnersTab
                , Categories              = new List <int>()
            };


            if (String.IsNullOrEmpty(entity.OverviewVideoIdentifier))
            {
                token.PromoVideoIdentifier = null;
            }
            else
            {
                long bcId;

                var parsed = Int64.TryParse(entity.OverviewVideoIdentifier, out bcId);

                if (parsed)
                {
                    token.PromoVideoIdentifier = bcId;
                }
            }

            return(token);
        }
        public async Task <IActionResult> PutCourse(Guid id, CourseEditDTO course)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != course.Id)
            {
                return(BadRequest("The request id parameter did not match the course id in the request body."));
            }

            var dbCourse = courseMapper.MapCourseEditDTO(course);

            var authorization = await authorizationService.AuthorizeAsync(User, dbCourse, AuthorizationConstants.CanUpdateCoursePolicy);

            if (!authorization.Succeeded)
            {
                return(Forbid());
            }

            _context.Entry(dbCourse).State = EntityState.Modified;

            try {
                await _context.SaveChangesAsync();

                await cache.ClearCourseCacheAsync(id, dbCourse.CourseInfo.Slug);
            } catch (DbUpdateConcurrencyException) {
                if (!CourseExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }