public ActionResult Create(SnippetBindingModel model,string language, string labels)
 {
     if (model != null && this.ModelState.IsValid)
     {
         var userId = this.User.Identity.GetUserId();
         var snippet = new Snippet()
         {
             Title = model.Title,
             Description = model.Description,
             Author = this.Data.Users.Find(userId),
             Language = this.Data.Languages.All().FirstOrDefault(l => l.Name == language),
         };
         if (!labels.IsEmpty())
         {
             var allLabels = labels.Split(';');
             foreach (var l in allLabels)
             {
                 var labelToCheck = l.Trim();
                 var label = this.Data.Labels.All().FirstOrDefault(la => la.Text.ToLower() == labelToCheck.ToLower());
                 if (label == null)
                 {
                     var newLabel = new Label(){Text = l};
                     this.Data.Labels.Add(newLabel);
                     this.Data.SaveChanges();
                 }
                 label = this.Data.Labels.All().FirstOrDefault(la => la.Text == l);
                 snippet.Labels.Add(label);
             }
         }
         this.Data.SaveChanges();
         return RedirectToAction("Details", "Snippets", new { id = snippet.Id });
     }
     return RedirectToAction("Index", "Users");
 }
示例#2
0
        public ActionResult EditSnippet(int id, EditSnippetModel model)
        {
            var snippet = this.Data.Snippets.All()
                .FirstOrDefault(s => s.Id == id);

            snippet.Title = model.SnippetBindingModel.Title;
            snippet.Description = model.SnippetBindingModel.Description;
            snippet.Code = model.SnippetBindingModel.Code;
            snippet.LanguageName = model.SnippetBindingModel.Language;     

            foreach (var label in snippet.Labels.ToList())
            {
                snippet.Labels.Remove(label);
            }

            this.Data.SaveChanges();

            var labels = model.SnippetBindingModel.Labels.Split(';');

            foreach (var labelText in labels)
            {
                var label = this.Data.Labels.All()
                    .FirstOrDefault(l => l.Text == labelText.Trim());

                if (label == null)
                {
                    var newLabel = new Label()
                    {
                        Text = labelText.Trim()
                    };

                    snippet.Labels.Add(newLabel);
                }
                else
                {
                    snippet.Labels.Add(label);
                }
            }

            this.Data.SaveChanges();

            return this.RedirectToAction("Details", "Snippets", new { id = snippet.Id });
        }
示例#3
0
        public ActionResult Create(CreateSnippetModel model)
        {
            string userId = this.User.Identity.GetUserId();

            var newSnippet = new Snippet()
            {
                Title = model.SnippetBindingModel.Title,
                Description = model.SnippetBindingModel.Description,
                Code = model.SnippetBindingModel.Code,
                CreationTime = DateTime.Now,
                LanguageName = model.SnippetBindingModel.Language,
                AuthorId = userId
            };

            this.Data.Snippets.Add(newSnippet);
            this.Data.SaveChanges();

            var snippet = this.Data.Snippets.All()
                .FirstOrDefault(s => s.Id == newSnippet.Id);
            var labels = model.SnippetBindingModel.Labels.Split(';');

            foreach (var labelText in labels)
            {
                var label = this.Data.Labels.All()
                    .FirstOrDefault(l => l.Text == labelText.Trim());

                if (label == null)
                {
                    var newLabel = new Label()
                    {
                        Text = labelText.Trim()
                    };

                    snippet.Labels.Add(newLabel);
                }
                else
                {
                    snippet.Labels.Add(label);
                }
            }

            this.Data.SaveChanges();

            return this.RedirectToAction("Details", "Snippets", new { id = snippet.Id });
        }
        private IEnumerable<Label> GetLabels(string labelsStr)
        {
            var labels = labelsStr.Split(';');
            IList<Label> result = new List<Label>();

            foreach (var label in labels)
            {
                var pureLabel = label.Trim().ToLower();
                var newLabel = this.Data.Labels.GetAll().FirstOrDefault(l => l.Text == pureLabel);
                if (newLabel == null)
                {
                    newLabel = new Label() { Text = label };
                    this.Data.Labels.Add(newLabel);
                }

                result.Add(newLabel);
            }

            this.Data.SaveChanges();
            return result;
        }
示例#5
0
 private void PopulateLabelsToSnippet(Snippet snippet, string[] labels)
 {
     foreach (var labelText in labels)
     {
         if (!this.LabelExists(labelText))
         {
             var label = new Label() { Text = labelText };
             this.Data.Labels.Add(label);
             this.Data.SaveChanges();
             snippet.Labels.Add(label);
         }
         else
         {
             var label = this.Data.Labels.All().FirstOrDefault(x => x.Text == labelText);
             label.Snippets.Add(snippet);
         }
     }
 }
示例#6
0
 private void AttachNewLabels(Snippet snippet, string[] labels)
 {
     foreach (var labelText in labels)
     {
         if (!snippet.Labels.Any(l => l.Text == labelText))
         {
             var label = new Label() { Text = labelText };
             label.Snippets.Add(snippet);
             this.Data.Labels.Add(label);
             this.Data.SaveChanges();
         }
     }
 }
示例#7
0
        public ActionResult Add(SnippetBindingModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                var languagesAll = this.LoadLanguages();
                var snippet = new Snippet()
                {
                    Code = model.Code,
                    Title = model.Title,
                    Description = model.Description,
                    LanguageId = model.LanguageId,
                    Language = this.Data.Languages.Find(model.LanguageId),
                    UserId = this.User.Identity.GetUserId(),
                    CreationTime = DateTime.Now
                };
                this.Data.Snippets.Add(snippet);

                var regex = @"\s*;*([a-zA-Z\-]+);*\s*";
                var labels = Regex.Matches(model.Labels, regex);
                foreach (Match labelFromInput in labels)
                {
                    foreach (Capture text in labelFromInput.Captures)
                    {
                        var label = this.Data.Labels
                        .All()
                        .FirstOrDefault(l => l.Text == text.Value);
                        if (label != null)
                        {
                            snippet.Labels.Add(label);
                            this.Data.SaveChanges();
                        }
                        else
                        {
                            string labelValue = text.Value.Replace(";", "").Replace(" ", "");
                            var newLabel = new Label()
                            {
                                Text = labelValue
                            };
                            this.Data.Labels.Add(newLabel);
                            this.Data.SaveChanges();
                            var newlyCreatedLabel = this.Data.Labels
                            .All()
                            .FirstOrDefault(l => l.Text == text.Value);
                            snippet.Labels.Add(newlyCreatedLabel);
                            this.Data.SaveChanges();
                        }
                    }
                }

                this.Data.SaveChanges();
                this.AddNotification(GlobalConstants.SnippetCreate, NotificationType.SUCCESS);

                return this.RedirectToAction(x => x.Details(snippet.Id));
            }

            this.LoadLanguages();
            return this.View(model);
        }
示例#8
0
        public ActionResult Edit(int id, SnippetBindingModel model)
        {
            var snippetToEdit = this.Data.Snippets.Find(id);
            if (snippetToEdit == null)
            {
                this.AddNotification(GlobalConstants.SnippetMissing, NotificationType.ERROR);
                return this.RedirectToAction("MySnippets");
            }

            if (model != null && this.ModelState.IsValid)
            {
                snippetToEdit.Code = model.Code;
                snippetToEdit.Title = model.Title;
                snippetToEdit.Description = model.Description;
                snippetToEdit.CreationTime = snippetToEdit.CreationTime;
                snippetToEdit.LanguageId = model.LanguageId;

                var regex = @"\s*;*([a-zA-Z\-]+);*\s*";
                var labels = Regex.Matches(model.Labels, regex);
                foreach (Match labelFromInput in labels)
                {
                    foreach (Capture text in labelFromInput.Captures)
                    {
                        var label = this.Data.Labels
                        .All()
                        .FirstOrDefault(l => l.Text == text.Value);
                        if (label != null)
                        {
                            snippetToEdit.Labels.Add(label);
                            this.Data.SaveChanges();
                        }
                        else
                        {
                            string labelValue = text.Value.Replace(";", "").Replace(" ", "");
                            var newLabel = new Label()
                            {
                                Text = labelValue
                            };
                            this.Data.Labels.Add(newLabel);
                            this.Data.SaveChanges();
                            var newlyCreatedLabel = this.Data.Labels
                            .All()
                            .FirstOrDefault(l => l.Text == text.Value);
                            snippetToEdit.Labels.Add(newlyCreatedLabel);
                            this.Data.SaveChanges();
                        }
                    }
                }

                this.Data.SaveChanges();
                this.AddNotification(GlobalConstants.SnippetEdited, NotificationType.SUCCESS);

                return this.RedirectToAction(x => x.Details(snippetToEdit.Id));
            }

            this.LoadLanguages();
            return this.View(model);
        }