public JsonResult EditTag(VMTagEdit obj) { var msg = new Msg(); if (!TryUpdateModel(obj) || !ModelState.IsValid) { msg.Error = true; msg.Body = ModelErrors(); return(Json(msg)); } var obj1 = Tag.Save(obj); return(Json(obj1)); }
/// <summary> /// save a tag object.Update if exists /// </summary> /// <param name="obj"></param> /// <returns></returns> public static dynamic Save(VMTagEdit obj) { var dbm = default(Tag); obj.Slug = string.IsNullOrEmpty(obj.Slug) ? obj.Name.CHSToPinyin("-").ToLower() : obj.Slug.ToLower(); var msg = new Msg(); //validate name if (!ValidateName(obj.Name, obj.Id.ToString())) { msg.Error = true; msg.Body = string.Format("Name [{0}] exists!Please choose another one!", obj.Name); return(msg); } ; using (var db = new GarfielderEntities()) { if (obj.IsNew)//add new { obj.Id = Guid.NewGuid(); dbm = new Tag(); dbm.Id = obj.Id; dbm.CreatedAt = DateTime.Now; //TODO dbm.CreatedBy = "Sys"; db.AddToTags(dbm); } else { //update dbm = db.Tags.SingleOrDefault(x => x.Id.Equals(obj.Id)); if (dbm == null) { //has been deleted! msg.Error = true; msg.Body = string.Format("Obj {0} has been deleted!", obj.Id); return(msg); } }//if dbm.Name = obj.Name; dbm.Slug = Tag.CheckSlug(db, obj.Slug); db.CommandTimeout = 0; db.SaveChanges(); ClearCache(); };//using return(obj); }
/// <summary> /// add a simple tag /// </summary> /// <param name="name"></param> /// <param name="slug"></param> /// <returns></returns> public static VMTagEdit QuickAddTag(string name, string slug = null, string user = "******") { var retVal = new VMTagEdit(); if (string.IsNullOrWhiteSpace(name)) { retVal.Error = true; retVal.Msg = "Tag name can't be blank"; return(retVal); } ; retVal.Name = name; retVal.Slug = string.IsNullOrWhiteSpace(slug) ? name.CHSToPinyin("-") : slug.CHSToPinyin("-"); retVal.Slug = retVal.Slug.ToLower(); using (var db = new GarfielderEntities()) { var dbm = db.Tags.SingleOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (dbm != null) { retVal.Id = dbm.Id; } else { retVal.Id = Guid.NewGuid(); //add a new tag dbm = new Tag(); dbm.Id = retVal.Id; dbm.Name = retVal.Name; dbm.Slug = CheckSlug(db, retVal.Slug); dbm.CreatedAt = DateTime.Now; //TODO: dbm.CreatedBy = user; db.AddToTags(dbm); //persist db.CommandTimeout = 0; db.SaveChanges(); ClearCache(); }; };//using return(retVal); }