Example #1
0
        public int CreateTag(Tags tags)
        {
            db.Tags.Add(tags);
            db.SaveChanges();

            return tags.Id;
        }
Example #2
0
        public ActionResult Create([Bind(Include = "Id,Title,Image,Time,Description,Tags,Id_Category,DisabledComments,Visits")] Post post, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (Directory.Exists(Server.MapPath("~/Files/")) == false) Directory.CreateDirectory(Server.MapPath("~/Files/"));

                //Guardar la Imagen
                if (file != null)
                {
                    string fileNameOP = string.Empty;
                    if (file.FileName.LastIndexOf("\\") > 0)
                        fileNameOP = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);
                    else
                        fileNameOP = file.FileName;

                    string filename = Path.GetFileName(DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + "_" + fileNameOP);
                    file.SaveAs(Server.MapPath("~/Files/") + filename);
                    post.Image = filename;
                }

                post.DisabledComments = false;
                post.Visits = 0;
                post.Time = DateTime.Now;

                db.Post.Add(post);
                db.SaveChanges();

                int id_post = post.Id;

                string[] tag = post.Tags.Split(new string[] { "," }, StringSplitOptions.None);
                Post_Tags postTags = new Post_Tags();

                TagsController tagController = new TagsController();
                Tags tags = new Tags();
                int id_tag = 0;

                for (int i = 0; i < tag.Length; i++)
                {
                    int id_registro = VerificarRegistro(tag[i]);
                    //Verificar si el registro existe
                    if (id_registro == 0) {
                        //Crear Tags
                        tags.Name = tag[i];
                        id_tag = tagController.CreateTag(tags);
                        //Crear PostTags
                        CreateTagPost(id_post, id_tag);
                    }
                    else
                    {
                        CreateTagPost(id_post, id_registro);
                    }
                }

                return RedirectToAction("Index");
            }

            ViewBag.Category = new SelectList(db.Category, "Id", "Name", post.Category);
            return View(post);
        }