Exemple #1
0
        public async Task <CourseApiModel> CreateCourse(CourseModel model, int ownerId, IFormFile uploadedFile)
        {
            var entity = new Course();

            try
            {
                var result = new CourseApiModel();

                entity.OwnerId = ownerId;

                entity.CourseImage = UtilCommon.ImageUpload(Constants.UserDataFolderName, uploadedFile, "", false);

                entity = model.ToEntity(entity);

                await _courseRepo.InsertAsync(entity);

                result = entity.ToModel();

                return(result);
            }
            catch (Exception ex)
            {
                if (entity.Id != 0)
                {
                    await _courseRepo.DeleteAsync(entity);
                }
                return(null);
            }
        }
Exemple #2
0
        public async Task <CourseApiModel> EditCourse(CourseModel model, int userId, IFormFile uploadedFile)
        {
            try
            {
                var result = new CourseApiModel();

                var entity = await _courseRepo.GetByIdAsync(model.Id);

                if (entity == null)
                {
                    return(result);
                }

                entity = model.ToEntity(entity);

                entity.CourseImage = UtilCommon.ImageUpload(Constants.UserDataFolderName, uploadedFile, "", false);

                await _courseRepo.UpdateAsync(entity);

                var owner = await _userRepo.GetByIdAsync(userId);

                result = entity.ToModel();
                return(result);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Exemple #3
0
        public static CourseModel ToViewModel(this CourseApiModel model)
        {
            var vm = new CourseModel()
            {
                Description    = model.Description,
                Id             = model.Id,
                Name           = model.Name,
                CourseCategory = model.CourseCategory,
                CourseImage    = model.CourseImage,
                AvailableFrom  = model.AvailableFrom,
                AvailableTo    = model.AvailableTo,
                CourseFee      = model.CourseFee
            };

            return(vm);
        }
        public IHttpActionResult Put(int id, CourseApiModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(ModelState));
            }

            var courseFromDb = this.Data.Courses
                               .Find(id);

            courseFromDb.Name        = model.Name;
            courseFromDb.Description = model.Description;
            courseFromDb.Materials   = model.Materials;

            this.Data.SaveChanges();

            return(this.Ok());
        }
        public IHttpActionResult Post(CourseApiModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(ModelState));
            }

            var courseForDb = new Course()
            {
                Name        = model.Name,
                Description = model.Description,
                Materials   = model.Materials,
                Homeworks   = new List <Homework>(),
                Students    = new List <Student>()
            };

            this.Data.Courses.Add(courseForDb);
            this.Data.SaveChanges();

            return(this.Ok(model));
        }
        public IHttpActionResult Put(int id, CourseApiModel model)
        {
            if (!ModelState.IsValid)
            {
                return this.BadRequest(ModelState);
            }

            var courseFromDb = this.Data.Courses
                .Find(id);

            courseFromDb.Name = model.Name;
            courseFromDb.Description = model.Description;
            courseFromDb.Materials = model.Materials;

            this.Data.SaveChanges();

            return this.Ok();
        }
        public IHttpActionResult Post(CourseApiModel model)
        {
            if (!ModelState.IsValid)
            {
                return this.BadRequest(ModelState);
            }

            var courseForDb = new Course()
            {
                Name = model.Name,
                Description = model.Description,
                Materials = model.Materials,
                Homeworks = new List<Homework>(),
                Students = new List<Student>()
            };

            this.Data.Courses.Add(courseForDb);
            this.Data.SaveChanges();

            return this.Ok(model);
        }