Пример #1
0
        public async Task <ActionResult> ItemCreate(string collectionId, string Title, string Description, string ShortDescription, string exitionalString, string Tags, bool goToNew, string ImageUrl)
        {
            Collection collection = _collectionContext.Collections
                                    .Where(o => o.Id == collectionId)
                                    .FirstOrDefault();
            User user = await _userManager.FindByNameAsync(collection.UserName);

            Dictionary <string, string[]> CollectionFields = JsonConvert.DeserializeObject <Dictionary <string, string[]> >(collection.Fields);
            Dictionary <string, string>   ItemFields       = new Dictionary <string, string>();

            //var markdown = new MarkdownSharp.Markdown();
            foreach (var colStr in CollectionFields)
            {
                string type = colStr.Key;
                foreach (var colName in colStr.Value)
                {
                    string name  = colName;
                    string value = Request.Form[name];
                    if (value == "" || value == null)
                    {
                        value = "off";
                    }
                    string s = type + "$$$$$" + markdown.Transform(value);
                    ItemFields.Add(markdown.Transform(name), s);
                }
            }

            string html  = markdown.Transform(Description);
            string title = markdown.Transform(Title);

            Item item = new Item
            {
                Id             = Guid.NewGuid().ToString(),
                CollectionId   = collectionId,
                UserName       = collection.UserName,
                Title          = title,
                Description    = html,
                Tags           = Tags,
                CollectionName = collection.Title,
                ImageUrl       = ImageUrl,
                OptionalFields = JsonConvert.SerializeObject(ItemFields),
                nComments      = 0,
                nLikes         = 0,
                Type           = "Item",
                Date           = DateTime.Now
            };
            List <Tag> Tagss = _tagContext.Tags.ToList();

            string[] ListTag = Tags.Split(" ");
            foreach (var tag in ListTag)
            {
                var result = _tagContext.Tags.Where(o => o.Text == tag).SingleOrDefault();
                if (result == null)
                {
                    Tag Tag = new Tag()
                    {
                        Id   = Guid.NewGuid().ToString(),
                        Text = tag
                    };
                    _tagContext.Add(Tag);
                }
                ;
            }

            await _tagContext.SaveChangesAsync();

            _itemContext.Add(item);
            collection.nItems++;
            user.nItems++;
            await _userManager.UpdateAsync(user);

            await _collectionContext.SaveChangesAsync();

            await _itemContext.SaveChangesAsync();

            if (goToNew == true)
            {
                return(RedirectToAction("Create", "Item", new { collectionId = collection.Id }));
            }
            return(RedirectToAction("Collection", "Collection", new { collectionId = collection.Id }));
        }