Exemplo n.º 1
0
        public JsonResult CreateTutorial([DataSourceRequest] DataSourceRequest request, TutorialCreateModel tutorialModel)
        {
            var category = this.db.Categories.FirstOrDefault(c => c.Id == tutorialModel.CategoryId);
            var user = this.db.Users.FirstOrDefault(u => u.UserName == HttpContext.User.Identity.Name);

            TutorialDisplayModel result = new TutorialDisplayModel();

            if (tutorialModel != null &&user != null)
            {
                Tutorial newTutorial = new Tutorial();
                newTutorial.Category = category;
                newTutorial.CompletionTime = tutorialModel.CompletionTime;
                newTutorial.Description = tutorialModel.Description;
                newTutorial.Difficulty = tutorialModel.Difficulty;
                newTutorial.EquipmentUsed = tutorialModel.EquipmentUsed;
                newTutorial.Title = tutorialModel.Title;
                newTutorial.User = user;
                newTutorial.CreatedOn = DateTime.Now;
                newTutorial.Image = this.GetTutorialImage(newTutorial.Title);

                this.db.Tutorials.Add(newTutorial);
                this.db.SaveChanges();

                result.Category = category.Name;
                result.CategoryId = category.Id;
                result.CompletionTime = newTutorial.CompletionTime;
                result.Id = newTutorial.Id;
                result.CreatedOn = newTutorial.CreatedOn;
                result.Description = newTutorial.Description;
                result.Difficulty = newTutorial.Difficulty;
                result.EquipmentUsed = newTutorial.EquipmentUsed;
                result.Title = newTutorial.Title;
                result.User = user.UserName;
            }

            return Json(new[] { result }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
Exemplo n.º 2
0
        public JsonResult UpdateTutorial([DataSourceRequest] DataSourceRequest request, TutorialDisplayModel model)
        {
            var tutorial = this.db.Tutorials.Find(model.Id);

            if (tutorial != null && ModelState.IsValid)
            {
                var category = this.db.Categories.Find(model.CategoryId);

                tutorial.CompletionTime = model.CompletionTime;
                tutorial.Description = model.Description;
                tutorial.Difficulty = model.Difficulty;
                tutorial.EquipmentUsed = model.EquipmentUsed;
                tutorial.Likes = model.Likes;
                tutorial.Title = model.Title;

                var newImage = this.GetTutorialImage(tutorial.Title);

                if (newImage != null)
                {
                    tutorial.Image = newImage;
                }

                tutorial.Category = category;

                this.db.SaveChanges();

                model.Category = category.Name;
                model.CategoryId = category.Id;
            }

            return Json(new[] { model }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }
Exemplo n.º 3
0
        public JsonResult DeleteTutorial([DataSourceRequest] DataSourceRequest request, TutorialDisplayModel model)
        {
            var tutorial = this.db.Tutorials.Include("Steps").Include("Comments").Include("Images").FirstOrDefault(t => t.Id == model.Id);

            if (tutorial != null)
            {
                this.db.Steps.RemoveRange(tutorial.Steps);
                this.db.Comments.RemoveRange(tutorial.Comments);
                this.db.Tutorials.Remove(tutorial);
                this.db.SaveChanges();
            }

            return Json(new[] { model }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
        }